From 06c72646a76f3970dc0defc173858de2061ab33c Mon Sep 17 00:00:00 2001 From: Idriss Neumann Date: Sun, 10 Mar 2024 21:03:31 +0100 Subject: [PATCH] Add labels to prometheus counter --- VERSION | 2 +- src/routes/api_health.py | 5 ++--- src/routes/api_manifest.py | 3 +-- src/routes/api_metrics.py | 3 +-- src/routes/api_root.py | 3 +-- src/utils/counter.py | 10 ++++++---- 6 files changed, 12 insertions(+), 14 deletions(-) diff --git a/VERSION b/VERSION index 4a788a0..0f44168 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.6.3 +3.6.4 diff --git a/src/routes/api_health.py b/src/routes/api_health.py index 8befbd8..a48caaa 100644 --- a/src/routes/api_health.py +++ b/src/routes/api_health.py @@ -1,6 +1,5 @@ from fastapi import APIRouter -from utils.cid import get_current_cid from utils.counter import create_counter, increment_counter from utils.otel import get_otel_tracer from utils.health import health @@ -11,11 +10,11 @@ @router.get("") def get_health(): with get_otel_tracer().start_as_current_span("imalive-health-get-route"): - increment_counter(_counter, get_current_cid()) + increment_counter(_counter) return health() @router.post("") def post_health(): with get_otel_tracer().start_as_current_span("imalive-health-post-route"): - increment_counter(_counter, get_current_cid()) + increment_counter(_counter) return health() diff --git a/src/routes/api_manifest.py b/src/routes/api_manifest.py index eb47ba4..6190c80 100644 --- a/src/routes/api_manifest.py +++ b/src/routes/api_manifest.py @@ -1,7 +1,6 @@ from fastapi import APIRouter from fastapi.responses import JSONResponse -from utils.cid import get_current_cid from utils.counter import create_counter, increment_counter from utils.otel import get_otel_tracer from utils.manifests import get_manifest_as_dict @@ -13,7 +12,7 @@ def get_manifest(): with get_otel_tracer().start_as_current_span("imalive-manifest-route"): manifest = get_manifest_as_dict() - increment_counter(_counter, get_current_cid()) + increment_counter(_counter) if manifest['status'] == 'error': return JSONResponse(content=manifest, status_code=500) else: diff --git a/src/routes/api_metrics.py b/src/routes/api_metrics.py index 2248b38..024870c 100644 --- a/src/routes/api_metrics.py +++ b/src/routes/api_metrics.py @@ -1,6 +1,5 @@ from fastapi import APIRouter -from utils.cid import get_current_cid from utils.counter import create_counter, increment_counter from utils.otel import get_otel_tracer from utils.metrics import all_metrics @@ -11,5 +10,5 @@ @router.get("") def get_metrics(): with get_otel_tracer().start_as_current_span("imalive-metrics-route"): - increment_counter(_counter, get_current_cid()) + increment_counter(_counter) return all_metrics() diff --git a/src/routes/api_root.py b/src/routes/api_root.py index c0d67c5..3371e3e 100644 --- a/src/routes/api_root.py +++ b/src/routes/api_root.py @@ -3,7 +3,6 @@ from utils.otel import get_otel_tracer from utils.health import health from utils.counter import create_counter, increment_counter -from utils.cid import get_current_cid router = APIRouter() _counter = create_counter("root_api_counter", "Root API counter") @@ -11,5 +10,5 @@ @router.get("/") def get_root(): with get_otel_tracer().start_as_current_span("imalive-root-route"): - increment_counter(_counter, get_current_cid()) + increment_counter(_counter) return health() diff --git a/src/utils/counter.py b/src/utils/counter.py index e796d52..e0e50d9 100644 --- a/src/utils/counter.py +++ b/src/utils/counter.py @@ -1,5 +1,6 @@ from prometheus_client import Counter +from utils.cid import get_current_cid from utils.otel import get_otel_meter def create_counter(name, description): @@ -9,9 +10,10 @@ def create_counter(name, description): description = description, unit = "1" ), - 'prom': Counter(name, description) + 'prom': Counter(name, description, ['cid']) } -def increment_counter(counter, tid): - counter['otel'].add(1, {"tid": tid}) - counter['prom'].inc() +def increment_counter(counter): + cid = get_current_cid() + counter['otel'].add(1, {"cid": cid}) + counter['prom'].labels(cid).inc()