Skip to content

Commit

Permalink
Add labels to prometheus counter
Browse files Browse the repository at this point in the history
  • Loading branch information
idrissneumann committed Mar 10, 2024
1 parent dd53ea0 commit 06c7264
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 14 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.6.3
3.6.4
5 changes: 2 additions & 3 deletions src/routes/api_health.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
3 changes: 1 addition & 2 deletions src/routes/api_manifest.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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:
Expand Down
3 changes: 1 addition & 2 deletions src/routes/api_metrics.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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()
3 changes: 1 addition & 2 deletions src/routes/api_root.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
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")

@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()
10 changes: 6 additions & 4 deletions src/utils/counter.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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()

0 comments on commit 06c7264

Please sign in to comment.