From 3c3377055f3954e417e9e24deb6c5d0cb16be7a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9mence=20Lesn=C3=A9?= Date: Fri, 13 Dec 2024 13:22:01 +0100 Subject: [PATCH] ops: Add service name and version in OTEL telemetry --- app/helpers/monitoring.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/app/helpers/monitoring.py b/app/helpers/monitoring.py index dc47e93..b394e32 100644 --- a/app/helpers/monitoring.py +++ b/app/helpers/monitoring.py @@ -6,6 +6,7 @@ from opentelemetry.instrumentation.aiohttp_client import AioHttpClientInstrumentor from opentelemetry.instrumentation.httpx import HTTPXClientInstrumentor from opentelemetry.metrics._internal.instrument import Counter, Gauge +from opentelemetry.semconv.attributes import service_attributes from opentelemetry.trace.span import INVALID_SPAN from opentelemetry.util.types import AttributeValue from structlog.contextvars import bind_contextvars, get_contextvars @@ -104,14 +105,19 @@ def gauge( e, ) +# Attributes +_default_attributes = { + service_attributes.SERVICE_NAME: MODULE_NAME, + service_attributes.SERVICE_VERSION: VERSION, +} + # Create a tracer and meter that will be used across the application tracer = trace.get_tracer( - instrumenting_library_version=VERSION, + attributes=_default_attributes, instrumenting_module_name=MODULE_NAME, ) meter = metrics.get_meter( name=MODULE_NAME, - version=VERSION, ) # Init metrics @@ -132,7 +138,12 @@ def gauge_set( """ metric.set( amount=value, - attributes=get_contextvars(), + attributes={ + # First, set default attributes + **_default_attributes, + # Then, set context attributes, they can override default attributes + **get_contextvars(), + }, ) @@ -145,5 +156,10 @@ def counter_add( """ metric.add( amount=value, - attributes=get_contextvars(), + attributes={ + # First, set default attributes + **_default_attributes, + # Then, set context attributes, they can override default attributes + **get_contextvars(), + }, )