77
88if not IN_TEST :
99 from opentelemetry import trace
10+ from opentelemetry .exporter .jaeger .thrift import JaegerExporter
1011 from opentelemetry .exporter .otlp .proto .grpc .trace_exporter import (
1112 OTLPSpanExporter as OTLPSpanExporterGRPC ,
1213 )
1516 )
1617 from opentelemetry .instrumentation .fastapi import FastAPIInstrumentor
1718 from opentelemetry .instrumentation .logging import LoggingInstrumentor
19+ from opentelemetry .sdk .resources import Resource
1820 from opentelemetry .sdk .trace import TracerProvider
19- from opentelemetry .sdk .trace .export import BatchSpanProcessor
21+ from opentelemetry .sdk .trace .export import BatchSpanProcessor , ConsoleSpanExporter
22+ from opentelemetry .semconv .resource import ResourceAttributes
2023else :
21- # Create dummy classes for test environment
24+
2225 class DummyTracerProvider :
2326 def add_span_processor (self , processor ):
2427 pass
@@ -43,53 +46,85 @@ def instrument_app(app, **kwargs):
4346 FastAPIInstrumentor = DummyInstrumentor
4447 LoggingInstrumentor = DummyInstrumentor
4548
46- OTLP_MODE = os .environ .get ("OTLP_MODE" , "otlp-grpc" )
47- OTLP_GRPC_ENDPOINT = os .environ .get ("OTLP_GRPC_ENDPOINT" , "otel-collector:4317" )
48- OTLP_HTTP_ENDPOINT = os .environ .get (
49- "OTLP_HTTP_ENDPOINT" , "http://otel-collector:4318/v1/traces"
49+ # Configuration options
50+ OTLP_MODE = os .environ .get ("OTLP_MODE" , "otlp-grpc" ) # Default to otlp-grpc
51+ OTLP_COLLECTOR_HOST = os .environ .get ("OTLP_COLLECTOR_HOST" , "otel-collector" )
52+ OTLP_COLLECTOR_PORT = os .environ .get ("OTLP_COLLECTOR_PORT" , "4317" )
53+ JAEGER_AGENT_HOST = os .environ .get (
54+ "JAEGER_AGENT_HOST" , "localhost" if IN_TEST else "jaeger"
5055)
56+ JAEGER_AGENT_PORT = int (os .environ .get ("JAEGER_AGENT_PORT" , "6831" ))
57+ SERVICE_NAME = os .environ .get ("OTEL_SERVICE_NAME" , "babeltron" )
5158
5259
5360def setup_jaeger (app : FastAPI , log_correlation : bool = True ) -> None :
5461 if IN_TEST :
55- logging .info ("Skipping OpenTelemetry setup in test environment" )
62+ logging .info ("Using minimal OpenTelemetry setup for test environment" )
63+ # For tests, we'll set up a minimal tracer that doesn't try to connect to external services
64+ resource = Resource .create (
65+ {ResourceAttributes .SERVICE_NAME : f"{ SERVICE_NAME } -test" }
66+ )
67+ tracer = TracerProvider (resource = resource )
68+ trace .set_tracer_provider (tracer )
69+
70+ # In tests, we'll use the console exporter which doesn't require external connections
71+ # This is optional and can be disabled if you don't want any tracing in tests
72+ if os .environ .get ("OTEL_TEST_EXPORT" , "false" ).lower () == "true" :
73+ tracer .add_span_processor (BatchSpanProcessor (ConsoleSpanExporter ()))
74+ logging .info ("Console span exporter enabled for tests" )
75+
76+ # Instrument the app for tests if needed
77+ FastAPIInstrumentor .instrument_app (
78+ app ,
79+ tracer_provider = tracer ,
80+ excluded_urls = "/metrics,/healthz,/readyz,/docs,/redoc,/openapi.json" ,
81+ )
5682 return
5783
58- if OTLP_GRPC_ENDPOINT .lower () == "disabled" :
84+ if OTLP_MODE .lower () == "disabled" :
5985 logging .info ("OpenTelemetry tracing is disabled" )
6086 return
6187
62- tracer = TracerProvider ()
88+ # Create a resource with service name
89+ resource = Resource .create ({ResourceAttributes .SERVICE_NAME : SERVICE_NAME })
90+
91+ # Create tracer provider with resource
92+ tracer = TracerProvider (resource = resource )
6393 trace .set_tracer_provider (tracer )
6494
6595 if OTLP_MODE == "otlp-grpc" :
66- if not IN_TEST :
67- tracer . add_span_processor (
68- BatchSpanProcessor (
69- OTLPSpanExporterGRPC (endpoint = OTLP_GRPC_ENDPOINT , insecure = True )
70- )
71- )
96+ # Use OTLP gRPC exporter to send to the OpenTelemetry Collector
97+ endpoint = f" { OTLP_COLLECTOR_HOST } : { OTLP_COLLECTOR_PORT } "
98+ tracer . add_span_processor (
99+ BatchSpanProcessor ( OTLPSpanExporterGRPC (endpoint = endpoint , insecure = True ) )
100+ )
101+ logging . info ( f"OTLP gRPC exporter enabled with endpoint: { endpoint } " )
72102 elif OTLP_MODE == "otlp-http" :
73- if not IN_TEST :
74- tracer .add_span_processor (
75- BatchSpanProcessor (OTLPSpanExporterHTTP (endpoint = OTLP_HTTP_ENDPOINT ))
76- )
103+ # Use OTLP HTTP exporter to send to the OpenTelemetry Collector
104+ endpoint = f"{ OTLP_COLLECTOR_HOST } :{ OTLP_COLLECTOR_PORT } "
105+ tracer .add_span_processor (
106+ BatchSpanProcessor (OTLPSpanExporterHTTP (endpoint = endpoint ))
107+ )
108+ logging .info (f"OTLP HTTP exporter enabled with endpoint: { endpoint } " )
77109 else :
78- if not IN_TEST :
79- tracer .add_span_processor (
80- BatchSpanProcessor (
81- OTLPSpanExporterGRPC (endpoint = OTLP_GRPC_ENDPOINT , insecure = True )
82- )
83- )
84-
85- if log_correlation and not IN_TEST :
86- LoggingInstrumentor ().instrument (set_logging_format = True )
87-
88- if not IN_TEST :
89- FastAPIInstrumentor .instrument_app (
90- app ,
91- tracer_provider = tracer ,
92- excluded_urls = "/metrics,/healthz,/readyz,/docs,/redoc,/openapi.json" ,
110+ # Use Jaeger Thrift exporter (deprecated but still functional)
111+ jaeger_exporter = JaegerExporter (
112+ agent_host_name = JAEGER_AGENT_HOST ,
113+ agent_port = JAEGER_AGENT_PORT ,
114+ )
115+ tracer .add_span_processor (BatchSpanProcessor (jaeger_exporter ))
116+ logging .info (
117+ f"Jaeger Thrift exporter enabled with agent: { JAEGER_AGENT_HOST } :{ JAEGER_AGENT_PORT } "
118+ )
119+ logging .warning (
120+ "Note: The Jaeger Thrift exporter is deprecated. Consider migrating to OTLP."
93121 )
94122
95- logging .info (f"OpenTelemetry tracing enabled with endpoint: { OTLP_GRPC_ENDPOINT } " )
123+ if log_correlation :
124+ LoggingInstrumentor ().instrument (set_logging_format = True )
125+
126+ FastAPIInstrumentor .instrument_app (
127+ app ,
128+ tracer_provider = tracer ,
129+ excluded_urls = "/metrics,/healthz,/readyz,/docs,/redoc,/openapi.json" ,
130+ )
0 commit comments