@@ -47,7 +47,9 @@ def inject_fixtures(self, caplog):
4747
4848 def setUp (self ):
4949 super ().setUp ()
50- LoggingInstrumentor ().instrument (tracer_provider = FakeTracerProvider ())
50+ LoggingInstrumentor ().instrument (
51+ tracer_provider = FakeTracerProvider (), set_logging_format = True
52+ )
5153
5254 def tearDown (self ):
5355 super ().tearDown ()
@@ -94,7 +96,21 @@ def assert_trace_context_injected(self, span_id, trace_id, trace_sampled):
9496 self .assertEqual (record .otelTraceSampled , trace_sampled )
9597 self .assertEqual (record .otelServiceName , "unknown_service" )
9698
97- def test_trace_context_injection (self ):
99+ @mock .patch .dict ("os.environ" , {"OTEL_PYTHON_LOG_CORRELATION" : "true" })
100+ def test_trace_context_injection_with_log_correlation_from_env_var (self ):
101+ LoggingInstrumentor ().uninstrument ()
102+ LoggingInstrumentor ().instrument ()
103+ with self .tracer .start_as_current_span ("s1" ) as span :
104+ span_id = format (span .get_span_context ().span_id , "016x" )
105+ trace_id = format (span .get_span_context ().trace_id , "032x" )
106+ trace_sampled = span .get_span_context ().trace_flags .sampled
107+ self .assert_trace_context_injected (
108+ span_id , trace_id , trace_sampled
109+ )
110+
111+ def test_trace_context_injection_with_log_correlation_instrument_arg (self ):
112+ LoggingInstrumentor ().uninstrument ()
113+ LoggingInstrumentor ().instrument (set_logging_format = True )
98114 with self .tracer .start_as_current_span ("s1" ) as span :
99115 span_id = format (span .get_span_context ().span_id , "016x" )
100116 trace_id = format (span .get_span_context ().trace_id , "032x" )
@@ -104,6 +120,8 @@ def test_trace_context_injection(self):
104120 )
105121
106122 def test_trace_context_injection_without_span (self ):
123+ LoggingInstrumentor ().uninstrument ()
124+ LoggingInstrumentor ().instrument (set_logging_format = True )
107125 self .assert_trace_context_injected ("0" , "0" , False )
108126
109127 @mock .patch ("logging.basicConfig" )
@@ -113,15 +131,13 @@ def test_basic_config_called(self, basic_config_mock):
113131 self .assertFalse (basic_config_mock .called )
114132 LoggingInstrumentor ().uninstrument ()
115133
116- env_patch = mock .patch .dict (
134+ with mock .patch .dict (
117135 "os.environ" , {"OTEL_PYTHON_LOG_CORRELATION" : "true" }
118- )
119- env_patch .start ()
120- LoggingInstrumentor ().instrument ()
121- basic_config_mock .assert_called_with (
122- format = DEFAULT_LOGGING_FORMAT , level = logging .INFO
123- )
124- env_patch .stop ()
136+ ):
137+ LoggingInstrumentor ().instrument ()
138+ basic_config_mock .assert_called_with (
139+ format = DEFAULT_LOGGING_FORMAT , level = logging .INFO
140+ )
125141
126142 @mock .patch ("logging.basicConfig" )
127143 def test_custom_format_and_level_env (self , basic_config_mock ):
@@ -130,20 +146,18 @@ def test_custom_format_and_level_env(self, basic_config_mock):
130146 self .assertFalse (basic_config_mock .called )
131147 LoggingInstrumentor ().uninstrument ()
132148
133- env_patch = mock .patch .dict (
149+ with mock .patch .dict (
134150 "os.environ" ,
135151 {
136152 "OTEL_PYTHON_LOG_CORRELATION" : "true" ,
137153 "OTEL_PYTHON_LOG_FORMAT" : "%(message)s %(otelSpanID)s" ,
138154 "OTEL_PYTHON_LOG_LEVEL" : "error" ,
139155 },
140- )
141- env_patch .start ()
142- LoggingInstrumentor ().instrument ()
143- basic_config_mock .assert_called_with (
144- format = "%(message)s %(otelSpanID)s" , level = logging .ERROR
145- )
146- env_patch .stop ()
156+ ):
157+ LoggingInstrumentor ().instrument ()
158+ basic_config_mock .assert_called_with (
159+ format = "%(message)s %(otelSpanID)s" , level = logging .ERROR
160+ )
147161
148162 @mock .patch ("logging.basicConfig" )
149163 def test_custom_format_and_level_api (self , basic_config_mock ): # pylint: disable=no-self-use
@@ -158,6 +172,25 @@ def test_custom_format_and_level_api(self, basic_config_mock): # pylint: disabl
158172 )
159173
160174 def test_log_hook (self ):
175+ LoggingInstrumentor ().uninstrument ()
176+ LoggingInstrumentor ().instrument (
177+ log_hook = log_hook ,
178+ )
179+ with self .tracer .start_as_current_span ("s1" ):
180+ with self .caplog .at_level (level = logging .INFO ):
181+ logger = logging .getLogger ("test logger" )
182+ logger .info ("hello" )
183+ self .assertEqual (len (self .caplog .records ), 1 )
184+ record = self .caplog .records [0 ]
185+ self .assertFalse (hasattr (record , "otelSpanID" ))
186+ self .assertFalse (hasattr (record , "otelTraceID" ))
187+ self .assertFalse (hasattr (record , "otelServiceName" ))
188+ self .assertFalse (hasattr (record , "otelTraceSampled" ))
189+ self .assertEqual (
190+ record .custom_user_attribute_from_log_hook , "some-value"
191+ )
192+
193+ def test_log_hook_with_set_logging_format (self ):
161194 LoggingInstrumentor ().uninstrument ()
162195 LoggingInstrumentor ().instrument (
163196 set_logging_format = True ,
@@ -181,21 +214,8 @@ def test_log_hook(self):
181214 )
182215
183216 def test_uninstrumented (self ):
184- with self .tracer .start_as_current_span ("s1" ) as span :
185- span_id = format (span .get_span_context ().span_id , "016x" )
186- trace_id = format (span .get_span_context ().trace_id , "032x" )
187- trace_sampled = span .get_span_context ().trace_flags .sampled
188- self .assert_trace_context_injected (
189- span_id , trace_id , trace_sampled
190- )
191-
192217 LoggingInstrumentor ().uninstrument ()
193-
194- self .caplog .clear ()
195- with self .tracer .start_as_current_span ("s1" ) as span :
196- span_id = format (span .get_span_context ().span_id , "016x" )
197- trace_id = format (span .get_span_context ().trace_id , "032x" )
198- trace_sampled = span .get_span_context ().trace_flags .sampled
218+ with self .tracer .start_as_current_span ("s1" ):
199219 with self .caplog .at_level (level = logging .INFO ):
200220 logger = logging .getLogger ("test logger" )
201221 logger .info ("hello" )
@@ -208,7 +228,9 @@ def test_uninstrumented(self):
208228
209229 def test_no_op_tracer_provider (self ):
210230 LoggingInstrumentor ().uninstrument ()
211- LoggingInstrumentor ().instrument (tracer_provider = NoOpTracerProvider ())
231+ LoggingInstrumentor ().instrument (
232+ tracer_provider = NoOpTracerProvider (), set_logging_format = True
233+ )
212234
213235 with self .caplog .at_level (level = logging .INFO ):
214236 logger = logging .getLogger ("test logger" )
0 commit comments