Skip to content

Commit

Permalink
ops: Add service name and version in OTEL telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
clemlesne committed Dec 13, 2024
1 parent 34ca384 commit 3c33770
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions app/helpers/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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(),
},
)


Expand All @@ -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(),
},
)

0 comments on commit 3c33770

Please sign in to comment.