add custom metric

This commit is contained in:
2026-02-06 22:02:22 +04:00
parent 55b8e51279
commit c3a29e2a16
2 changed files with 33 additions and 2 deletions

View File

@@ -16,7 +16,8 @@ spec:
- name: fastapi-app-container - name: fastapi-app-container
image: igit.danilkolesnikov.ru/danil/playground_app image: igit.danilkolesnikov.ru/danil/playground_app
ports: ports:
- containerPort: 8000 - name: web
containerPort: 8000
--- ---
apiVersion: v1 apiVersion: v1
@@ -30,3 +31,14 @@ spec:
- protocol: TCP - protocol: TCP
port: 8000 port: 8000
targetPort: 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

View File

@@ -1,8 +1,27 @@
from typing import Callable
from fastapi import FastAPI 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() app = FastAPI()
Instrumentator().add(http_requested_languages_total()).instrument(app).expose(app)
@app.get("/") @app.get("/")
async def read_root(): async def read_root(label=None):
if label:
REQUEST_COUNT.labels(label).inc()
return {"Hello": "World"} return {"Hello": "World"}