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

@@ -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"}