55from typing import Final
66
77from aiohttp import web
8- from opentelemetry import trace
98from opentelemetry .exporter .otlp .proto .http .trace_exporter import (
109 OTLPSpanExporter as OTLPSpanExporterHTTP ,
1110)
1514from opentelemetry .instrumentation .aiohttp_server import (
1615 middleware as aiohttp_server_opentelemetry_middleware , # pylint:disable=no-name-in-module
1716)
18- from opentelemetry .sdk .resources import Resource
19- from opentelemetry .sdk .trace import SpanProcessor , TracerProvider
17+ from opentelemetry .sdk .trace import SpanProcessor
2018from opentelemetry .sdk .trace .export import BatchSpanProcessor
21- from opentelemetry .sdk .trace .sampling import ParentBased , TraceIdRatioBased
2219from settings_library .tracing import TracingSettings
2320from yarl import URL
2421
2522from ..logging_utils import log_context
26- from ..tracing import get_trace_id_header
23+ from ..tracing import TracingData , get_trace_id_header
2724
2825_logger = logging .getLogger (__name__ )
2926try :
@@ -77,7 +74,7 @@ def _startup(
7774 * ,
7875 app : web .Application ,
7976 tracing_settings : TracingSettings ,
80- service_name : str ,
77+ tracing_data : TracingData ,
8178 add_response_trace_id_header : bool = False ,
8279) -> None :
8380 """
@@ -100,26 +97,19 @@ def _startup(
10097 "unset. Provide both or remove both."
10198 )
10299 raise RuntimeError (msg )
103- resource = Resource (attributes = {"service.name" : service_name })
104- sampler = ParentBased (
105- root = TraceIdRatioBased (tracing_settings .TRACING_SAMPLING_PROBABILITY )
106- )
107- tracer_provider = TracerProvider (resource = resource , sampler = sampler )
108- trace .set_tracer_provider (tracer_provider = tracer_provider )
109- tracer_provider : trace .TracerProvider = trace .get_tracer_provider ()
110100
111101 tracing_destination : str = (
112102 f"{ URL (opentelemetry_collector_endpoint ).with_port (opentelemetry_collector_port ).with_path ('/v1/traces' )} "
113103 )
114104
115105 _logger .info (
116106 "Trying to connect service %s to tracing collector at %s." ,
117- service_name ,
107+ tracing_data . service_name ,
118108 tracing_destination ,
119109 )
120110
121111 # Add the span processor to the tracer provider
122- tracer_provider .add_span_processor (_create_span_processor (tracing_destination )) # type: ignore[attr-defined] # https://github.com/open-telemetry/opentelemetry-python/issues/3713
112+ tracing_data . tracer_provider .add_span_processor (_create_span_processor (tracing_destination )) # type: ignore[attr-defined] # https://github.com/open-telemetry/opentelemetry-python/issues/3713
123113 # Instrument aiohttp server
124114 # Explanation for custom middleware call DK 10/2024:
125115 # OpenTelemetry Aiohttp autoinstrumentation is meant to be used by only calling `AioHttpServerInstrumentor().instrument()`
@@ -140,43 +130,51 @@ def _startup(
140130 # - opentelemetry-instrumentation==0.48b0
141131
142132 # Instrument aiohttp client
143- AioHttpClientInstrumentor ().instrument ()
133+ AioHttpClientInstrumentor ().instrument (tracer_provier = tracing_data . tracer_provider )
144134 if HAS_AIOPG :
145135 with log_context (
146136 _logger ,
147137 logging .INFO ,
148138 msg = "Attempting to add aio-pg opentelemetry autoinstrumentation..." ,
149139 ):
150- AiopgInstrumentor ().instrument ()
140+ AiopgInstrumentor ().instrument (tracer_provider = tracing_data . tracer_provider )
151141 if HAS_ASYNCPG :
152142 with log_context (
153143 _logger ,
154144 logging .INFO ,
155145 msg = "Attempting to add asyncpg opentelemetry autoinstrumentation..." ,
156146 ):
157- AsyncPGInstrumentor ().instrument ()
147+ AsyncPGInstrumentor ().instrument (
148+ tracer_provider = tracing_data .tracer_provider
149+ )
158150 if HAS_BOTOCORE :
159151 with log_context (
160152 _logger ,
161153 logging .INFO ,
162154 msg = "Attempting to add botocore opentelemetry autoinstrumentation..." ,
163155 ):
164- BotocoreInstrumentor ().instrument ()
156+ BotocoreInstrumentor ().instrument (
157+ tracer_provider = tracing_data .tracer_provider
158+ )
165159 if HAS_REQUESTS :
166160 with log_context (
167161 _logger ,
168162 logging .INFO ,
169163 msg = "Attempting to add requests opentelemetry autoinstrumentation..." ,
170164 ):
171- RequestsInstrumentor ().instrument ()
165+ RequestsInstrumentor ().instrument (
166+ tracer_provider = tracing_data .tracer_provider
167+ )
172168
173169 if HAS_AIO_PIKA :
174170 with log_context (
175171 _logger ,
176172 logging .INFO ,
177173 msg = "Attempting to add aio_pika opentelemetry autoinstrumentation..." ,
178174 ):
179- AioPikaInstrumentor ().instrument ()
175+ AioPikaInstrumentor ().instrument (
176+ tracer_provider = tracing_data .tracer_provider
177+ )
180178
181179
182180@web .middleware
@@ -194,8 +192,9 @@ async def response_trace_id_header_middleware(request: web.Request, handler):
194192 return response
195193
196194
197- def _shutdown () -> None :
195+ def _shutdown (tracing_data : TracingData ) -> None :
198196 """Uninstruments all opentelemetry instrumentors that were instrumented."""
197+ assert tracing_data # nosec
199198 try :
200199 AioHttpClientInstrumentor ().uninstrument ()
201200 except Exception : # pylint:disable=broad-exception-caught
@@ -231,19 +230,19 @@ def get_tracing_lifespan(
231230 * ,
232231 app : web .Application ,
233232 tracing_settings : TracingSettings ,
234- service_name : str ,
233+ tracing_data : TracingData ,
235234 add_response_trace_id_header : bool = False ,
236235) -> Callable [[web .Application ], AsyncIterator ]:
237236 _startup (
238237 app = app ,
239238 tracing_settings = tracing_settings ,
240- service_name = service_name ,
239+ tracing_data = tracing_data ,
241240 add_response_trace_id_header = add_response_trace_id_header ,
242241 )
243242
244243 async def tracing_lifespan (app : web .Application ):
245244 assert app # nosec
246245 yield
247- _shutdown ()
246+ _shutdown (tracing_data = tracing_data )
248247
249248 return tracing_lifespan
0 commit comments