add custom metric
This commit is contained in:
14
k8s/app.yaml
14
k8s/app.yaml
@@ -16,7 +16,8 @@ spec:
|
||||
- name: fastapi-app-container
|
||||
image: igit.danilkolesnikov.ru/danil/playground_app
|
||||
ports:
|
||||
- containerPort: 8000
|
||||
- name: web
|
||||
containerPort: 8000
|
||||
|
||||
---
|
||||
apiVersion: v1
|
||||
@@ -30,3 +31,14 @@ spec:
|
||||
- protocol: TCP
|
||||
port: 8000
|
||||
targetPort: 8000
|
||||
---
|
||||
apiVersion: monitoring.coreos.com/v1
|
||||
kind: PodMonitor
|
||||
metadata:
|
||||
name: fastapi-app-pod-monitor
|
||||
spec:
|
||||
selector:
|
||||
matchLabels:
|
||||
app: fastapi-app
|
||||
podMetricsEndpoints:
|
||||
- port: web
|
||||
|
||||
21
src/main.py
21
src/main.py
@@ -1,8 +1,27 @@
|
||||
from typing import Callable
|
||||
|
||||
from fastapi import FastAPI
|
||||
from prometheus_client import Counter
|
||||
from prometheus_fastapi_instrumentator import Instrumentator
|
||||
from prometheus_fastapi_instrumentator.metrics import Info
|
||||
|
||||
REQUEST_COUNT = Counter(
|
||||
"label_counter", "How many times called with specific label", labelnames=("label",)
|
||||
)
|
||||
|
||||
|
||||
def http_requested_languages_total() -> Callable[[Info], None]:
|
||||
def instrumentation(info: Info) -> None: ...
|
||||
|
||||
return instrumentation
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
Instrumentator().add(http_requested_languages_total()).instrument(app).expose(app)
|
||||
|
||||
|
||||
@app.get("/")
|
||||
async def read_root():
|
||||
async def read_root(label=None):
|
||||
if label:
|
||||
REQUEST_COUNT.labels(label).inc()
|
||||
return {"Hello": "World"}
|
||||
|
||||
Reference in New Issue
Block a user