sbt-idp/cope2n-api/fwd/opentelemetry_config.py

34 lines
1.5 KiB
Python
Raw Normal View History

2024-10-29 07:41:35 +00:00
from opentelemetry import metrics, trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import \
OTLPSpanExporter
from opentelemetry.exporter.otlp.proto.http.metric_exporter import \
OTLPMetricExporter
from opentelemetry.instrumentation.django import DjangoInstrumentor
2024-10-31 06:06:44 +00:00
from opentelemetry.instrumentation.celery import CeleryInstrumentor
2024-10-29 07:41:35 +00:00
from opentelemetry.sdk.metrics import MeterProvider
from opentelemetry.sdk.metrics.export import PeriodicExportingMetricReader
from opentelemetry.sdk.resources import SERVICE_NAME, Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
2024-10-29 07:43:48 +00:00
def setup_tracing(tracer_endpoint, service_name):
2024-10-31 06:06:44 +00:00
# Instrument Django
DjangoInstrumentor().instrument()
CeleryInstrumentor().instrument()
2024-10-29 07:41:35 +00:00
# Set up a tracer provider
2024-10-29 07:43:48 +00:00
span_exporter = OTLPSpanExporter(endpoint=f"{tracer_endpoint}/v1/traces")
processor = BatchSpanProcessor(span_exporter=span_exporter)
2024-10-31 06:06:44 +00:00
2024-10-29 07:43:48 +00:00
attributes = {SERVICE_NAME: service_name}
resource = Resource(attributes=attributes)
trace_provider = TracerProvider(resource=resource)
trace_provider.add_span_processor(span_processor=processor)
trace.set_tracer_provider(tracer_provider=trace_provider)
2024-10-29 07:41:35 +00:00
2024-10-29 07:43:48 +00:00
reader = PeriodicExportingMetricReader(
OTLPMetricExporter(endpoint=f"{tracer_endpoint}/v1/metrics"))
meter_provider = MeterProvider(resource=resource, metric_readers=[reader])
metrics.set_meter_provider(meter_provider=meter_provider)
2024-10-29 07:41:35 +00:00
2024-10-31 06:06:44 +00:00