1313from opentelemetry .sdk .resources import Resource
1414from opentelemetry .sdk .trace import TracerProvider
1515from opentelemetry .sdk .trace .export import BatchSpanProcessor
16+ from servicelib .logging_utils import log_context
1617from settings_library .tracing import TracingSettings
1718
18- log = logging .getLogger (__name__ )
19+ _logger = logging .getLogger (__name__ )
1920#########
2021try :
2122 from opentelemetry .instrumentation .asyncpg import AsyncPGInstrumentor
@@ -59,7 +60,7 @@ def setup_tracing(
5960 not tracing_settings .TRACING_OPENTELEMETRY_COLLECTOR_ENDPOINT
6061 and not tracing_settings .TRACING_OPENTELEMETRY_COLLECTOR_PORT
6162 ):
62- log .warning ("Skipping opentelemetry tracing setup" )
63+ _logger .warning ("Skipping opentelemetry tracing setup" )
6364 return
6465
6566 # Set up the tracer provider
@@ -68,7 +69,7 @@ def setup_tracing(
6869 global_tracer_provider = trace .get_tracer_provider ()
6970 assert isinstance (global_tracer_provider , TracerProvider ) # nosec
7071 tracing_destination : str = f"{ tracing_settings .TRACING_OPENTELEMETRY_COLLECTOR_ENDPOINT } :{ tracing_settings .TRACING_OPENTELEMETRY_COLLECTOR_PORT } /v1/traces"
71- log .info (
72+ _logger .info (
7273 "Trying to connect service %s to opentelemetry tracing collector at %s." ,
7374 service_name ,
7475 tracing_destination ,
@@ -81,17 +82,37 @@ def setup_tracing(
8182 FastAPIInstrumentor ().instrument_app (app )
8283
8384 if HAS_AIOPG :
84- log .info ("Attempting to add aiopg opentelemetry autoinstrumentation..." )
85- AiopgInstrumentor ().instrument ()
85+ with log_context (
86+ _logger ,
87+ logging .INFO ,
88+ msg = "Attempting to add asyncpg opentelemetry autoinstrumentation..." ,
89+ ):
90+ AiopgInstrumentor ().instrument ()
8691 if HAS_ASYNCPG :
87- log .info ("Attempting to add asyncpg opentelemetry autoinstrumentation..." )
88- AsyncPGInstrumentor ().instrument ()
92+ with log_context (
93+ _logger ,
94+ logging .INFO ,
95+ msg = "Attempting to add asyncpg opentelemetry autoinstrumentation..." ,
96+ ):
97+ AsyncPGInstrumentor ().instrument ()
8998 if HAS_AIOPIKA :
90- log .info ("Attempting to add aio-pika opentelemetry autoinstrumentation..." )
91- AioPikaInstrumentor ().instrument ()
99+ with log_context (
100+ _logger ,
101+ logging .INFO ,
102+ msg = "Attempting to add aio-pika opentelemetry autoinstrumentation..." ,
103+ ):
104+ AioPikaInstrumentor ().instrument ()
92105 if HAS_REDIS :
93- log .info ("Attempting to add redis opentelemetry autoinstrumentation..." )
94- RedisInstrumentor ().instrument ()
106+ with log_context (
107+ _logger ,
108+ logging .INFO ,
109+ msg = "Attempting to add redis opentelemetry autoinstrumentation..." ,
110+ ):
111+ RedisInstrumentor ().instrument ()
95112 if HAS_BOTOCORE :
96- log .info ("Attempting to add botocore opentelemetry autoinstrumentation..." )
97- BotocoreInstrumentor ().instrument ()
113+ with log_context (
114+ _logger ,
115+ logging .INFO ,
116+ msg = "Attempting to add botocore opentelemetry autoinstrumentation..." ,
117+ ):
118+ BotocoreInstrumentor ().instrument ()
0 commit comments