From 926089a3b589dff64cc2d0d4e30005cc0c8a17a1 Mon Sep 17 00:00:00 2001 From: Evan Morris Date: Mon, 4 Nov 2024 14:13:53 -0500 Subject: [PATCH] upgrading otel implementation, using OTLP instead of jaeger client JaegerExporter is deprecated. This would send traces using the OTLP over gRPC. --- requirements.txt | 8 ++++---- src/otel_config.py | 26 ++++++++++---------------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/requirements.txt b/requirements.txt index 42f2acc..a25bb51 100644 --- a/requirements.txt +++ b/requirements.txt @@ -16,9 +16,9 @@ redis~=3.5.3 requests==2.28.1 uvicorn==0.17.6 uvloop==0.19.0 -opentelemetry-sdk==1.16.0 -opentelemetry-instrumentation-fastapi==0.37b0 -opentelemetry-exporter-jaeger==1.16.0 -opentelemetry-instrumentation-httpx==0.37b0 +opentelemetry-sdk==1.25.0 +opentelemetry-instrumentation-fastapi==0.48b0 +opentelemetry-exporter-otlp-proto-grpc==1.27.0 +opentelemetry-instrumentation-httpx==0.48b0 fakeredis<=2.10.2 networkx==3.2.1 diff --git a/src/otel_config.py b/src/otel_config.py index a55cc7c..227925f 100644 --- a/src/otel_config.py +++ b/src/otel_config.py @@ -8,7 +8,7 @@ def configure_otel(service_name, APP): from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor from opentelemetry import trace - from opentelemetry.exporter.jaeger.thrift import JaegerExporter + from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter from opentelemetry.sdk.resources import SERVICE_NAME as telemetery_service_name_key, Resource from opentelemetry.sdk.trace import TracerProvider from opentelemetry.sdk.trace.export import BatchSpanProcessor @@ -19,20 +19,14 @@ def configure_otel(service_name, APP): # these supresses such warnings. logging.captureWarnings(capture=True) warnings.filterwarnings("ignore",category=ResourceWarning) - jaeger_host = os.environ.get('JAEGER_HOST', 'jaeger-otel-agent') - jaeger_port = int(os.environ.get('JAEGER_PORT', '6831')) - trace.set_tracer_provider( - TracerProvider( - resource=Resource.create({telemetery_service_name_key: service_name}) - ) + jaeger_host = os.environ.get('JAEGER_HOST', 'jaeger-otel-collector') + jaeger_port = int(os.environ.get('JAEGER_PORT', '4317')) + jaeger_endpoint = f'{jaeger_host}:{jaeger_port}' + otlp_exporter = OTLPSpanExporter(endpoint=jaeger_endpoint) + tracer_provider = TracerProvider( + resource=Resource.create({telemetery_service_name_key: service_name}) ) - jaeger_exporter = JaegerExporter( - agent_host_name=jaeger_host, - agent_port=jaeger_port, - ) - trace.get_tracer_provider().add_span_processor( - BatchSpanProcessor(jaeger_exporter) - ) - tracer = trace.get_tracer(__name__) - FastAPIInstrumentor.instrument_app(APP, tracer_provider=trace, excluded_urls="docs,openapi.json") + tracer_provider.add_span_processor(BatchSpanProcessor(otlp_exporter)) + trace.set_tracer_provider(tracer_provider) + FastAPIInstrumentor.instrument_app(APP, tracer_provider=tracer_provider, excluded_urls="docs,openapi.json") HTTPXClientInstrumentor().instrument()