add custom metric
This commit is contained in:
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