55from typing import Any , Dict , Generator
66
77import pytest
8+ from opentelemetry .context .context import Context
89from opentelemetry .trace import (
910 INVALID_SPAN_ID ,
1011 INVALID_TRACE_ID ,
1314)
1415
1516from instana .propagators .http_propagator import HTTPPropagator
17+ from instana .span .span import get_current_span
1618from instana .span_context import SpanContext
1719from instana .util .ids import header_to_long_id , internal_id
1820
@@ -76,18 +78,22 @@ def test_extract_carrier_dict(
7678 }
7779
7880 ctx = self .hptc .extract (carrier )
79-
80- assert ctx .correlation_id == str (span_id )
81- assert ctx .correlation_type == "web"
82- assert not ctx .instana_ancestor
83- assert ctx .level == 1
84- assert ctx .long_trace_id == header_to_long_id (_instana_long_tracer_id )
85- assert ctx .span_id == _span_id
86- assert not ctx .synthetic
87- assert ctx .trace_id == _trace_id
88- assert ctx .trace_parent
89- assert ctx .traceparent == f"00-{ _instana_long_tracer_id } -{ _instana_span_id } -01"
90- assert ctx .tracestate == _tracestate
81+ span_ctx = get_current_span (ctx ).get_span_context ()
82+
83+ assert span_ctx .correlation_id == str (span_id )
84+ assert span_ctx .correlation_type == "web"
85+ assert span_ctx .level == 1
86+ assert span_ctx .long_trace_id == header_to_long_id (_instana_long_tracer_id )
87+ assert span_ctx .span_id == _span_id
88+ assert span_ctx .trace_id == _trace_id
89+ assert span_ctx .trace_parent
90+ assert (
91+ span_ctx .traceparent
92+ == f"00-{ _instana_long_tracer_id } -{ _instana_span_id } -01"
93+ )
94+ assert span_ctx .tracestate == _tracestate
95+ assert not span_ctx .synthetic
96+ assert not span_ctx .instana_ancestor
9197
9298 def test_extract_carrier_list (
9399 self ,
@@ -112,18 +118,22 @@ def test_extract_carrier_list(
112118 ]
113119
114120 ctx = self .hptc .extract (carrier )
115-
116- assert not ctx .correlation_id
117- assert not ctx .correlation_type
118- assert not ctx .instana_ancestor
119- assert ctx .level == 1
120- assert not ctx .long_trace_id
121- assert ctx .span_id == _span_id
122- assert not ctx .synthetic
123- assert ctx .trace_id == internal_id (_trace_id )
124- assert not ctx .trace_parent
125- assert ctx .traceparent == f"00-{ _instana_long_tracer_id } -{ _instana_span_id } -01"
126- assert ctx .tracestate == _tracestate
121+ span_ctx = get_current_span (ctx ).get_span_context ()
122+
123+ assert not span_ctx .correlation_id
124+ assert not span_ctx .correlation_type
125+ assert not span_ctx .instana_ancestor
126+ assert span_ctx .level == 1
127+ assert not span_ctx .long_trace_id
128+ assert span_ctx .span_id == _span_id
129+ assert not span_ctx .synthetic
130+ assert span_ctx .trace_id == internal_id (_trace_id )
131+ assert not span_ctx .trace_parent
132+ assert (
133+ span_ctx .traceparent
134+ == f"00-{ _instana_long_tracer_id } -{ _instana_span_id } -01"
135+ )
136+ assert span_ctx .tracestate == _tracestate
127137
128138 def test_extract_carrier_dict_validate_Exception_None_returned (
129139 self ,
@@ -147,13 +157,15 @@ def test_extract_carrier_dict_validate_Exception_None_returned(
147157 }
148158
149159 ctx = self .hptc .extract (carrier )
160+ span_ctx = get_current_span (ctx ).get_span_context ()
150161
151- assert isinstance (ctx , SpanContext )
152- assert ctx .trace_id == INVALID_TRACE_ID
153- assert ctx .span_id == INVALID_SPAN_ID
154- assert not ctx .synthetic
155- assert ctx .correlation_id == str (span_id )
156- assert ctx .correlation_type == "web"
162+ assert isinstance (ctx , Context )
163+ assert isinstance (span_ctx , SpanContext )
164+ assert span_ctx .trace_id == INVALID_TRACE_ID
165+ assert span_ctx .span_id == INVALID_SPAN_ID
166+ assert not span_ctx .synthetic
167+ assert span_ctx .correlation_id == str (span_id )
168+ assert span_ctx .correlation_type == "web"
157169
158170 def test_extract_fake_exception (
159171 self ,
@@ -194,18 +206,19 @@ def test_extract_carrier_dict_corrupted_level_header(
194206 }
195207
196208 ctx = self .hptc .extract (carrier )
197-
198- assert not ctx .correlation_id
199- assert ctx .correlation_type == "web"
200- assert not ctx .instana_ancestor
201- assert ctx .level == 1
202- assert ctx .long_trace_id == header_to_long_id (_instana_long_tracer_id )
203- assert ctx .span_id == _span_id
204- assert not ctx .synthetic
205- assert ctx .trace_id == _trace_id
206- assert ctx .trace_parent
207- assert ctx .traceparent == _traceparent
208- assert ctx .tracestate == _tracestate
209+ span_ctx = get_current_span (ctx ).get_span_context ()
210+
211+ assert not span_ctx .correlation_id
212+ assert span_ctx .correlation_type == "web"
213+ assert not span_ctx .instana_ancestor
214+ assert span_ctx .level == 1
215+ assert span_ctx .long_trace_id == header_to_long_id (_instana_long_tracer_id )
216+ assert span_ctx .span_id == _span_id
217+ assert not span_ctx .synthetic
218+ assert span_ctx .trace_id == _trace_id
219+ assert span_ctx .trace_parent
220+ assert span_ctx .traceparent == _traceparent
221+ assert span_ctx .tracestate == _tracestate
209222
210223 def test_extract_carrier_dict_level_header_not_splitable (
211224 self ,
@@ -224,18 +237,19 @@ def test_extract_carrier_dict_level_header_not_splitable(
224237 }
225238
226239 ctx = self .hptc .extract (carrier )
227-
228- assert not ctx .correlation_id
229- assert not ctx .correlation_type
230- assert not ctx .instana_ancestor
231- assert ctx .level == 1
232- assert not ctx .long_trace_id
233- assert ctx .span_id == _span_id
234- assert not ctx .synthetic
235- assert ctx .trace_id == internal_id (_trace_id )
236- assert not ctx .trace_parent
237- assert ctx .traceparent == _traceparent
238- assert ctx .tracestate == _tracestate
240+ span_ctx = get_current_span (ctx ).get_span_context ()
241+
242+ assert not span_ctx .correlation_id
243+ assert not span_ctx .correlation_type
244+ assert not span_ctx .instana_ancestor
245+ assert span_ctx .level == 1
246+ assert not span_ctx .long_trace_id
247+ assert span_ctx .span_id == _span_id
248+ assert not span_ctx .synthetic
249+ assert span_ctx .trace_id == internal_id (_trace_id )
250+ assert not span_ctx .trace_parent
251+ assert span_ctx .traceparent == _traceparent
252+ assert span_ctx .tracestate == _tracestate
239253
240254 # The following tests are based on the test cases defined in the
241255 # tracer_compliance_test_cases.json file.
@@ -283,48 +297,49 @@ def test_w3c_off_x_instana_l_0(
283297 os .environ ["INSTANA_DISABLE_W3C_TRACE_CORRELATION" ] = disable_w3c
284298
285299 ctx = self .hptc .extract (carrier_header )
300+ span_ctx = get_current_span (ctx ).get_span_context ()
286301
287302 # Assert the level is (zero) int, not str
288- assert isinstance (ctx .level , int )
289- assert ctx .level == 0
303+ assert isinstance (span_ctx .level , int )
304+ assert span_ctx .level == 0
290305
291306 # Assert the suppression is on
292- assert ctx .suppression
307+ assert span_ctx .suppression
293308
294309 # Assert the rest of the attributes are on their default value
295- assert ctx .trace_id == INVALID_TRACE_ID
296- assert ctx .span_id == INVALID_SPAN_ID
297- assert not ctx .synthetic
298- assert not ctx .correlation_id
299- assert not ctx .trace_parent
300- assert not ctx .instana_ancestor
301- assert not ctx .long_trace_id
302- assert not ctx .correlation_type
303- assert not ctx .correlation_id
310+ assert span_ctx .trace_id == INVALID_TRACE_ID
311+ assert span_ctx .span_id == INVALID_SPAN_ID
312+ assert not span_ctx .synthetic
313+ assert not span_ctx .correlation_id
314+ assert not span_ctx .trace_parent
315+ assert not span_ctx .instana_ancestor
316+ assert not span_ctx .long_trace_id
317+ assert not span_ctx .correlation_type
318+ assert not span_ctx .correlation_id
304319
305320 # Assert that the traceparent is propagated when it is enabled
306321 if "traceparent" in carrier_header .keys ():
307- assert ctx .traceparent
322+ assert span_ctx .traceparent
308323 tp_trace_id = header_to_long_id (carrier_header ["traceparent" ].split ("-" )[1 ])
309324 else :
310- assert not ctx .traceparent
311- tp_trace_id = ctx .trace_id
325+ assert not span_ctx .traceparent
326+ tp_trace_id = span_ctx .trace_id
312327
313328 # Assert that the tracestate is propagated when it is enabled
314329 if "tracestate" in carrier_header .keys ():
315- assert ctx .tracestate
330+ assert span_ctx .tracestate
316331 else :
317- assert not ctx .tracestate
332+ assert not span_ctx .tracestate
318333
319334 # Simulate the side-effect of starting a span, getting a trace_id and span_id.
320335 # Actually, with OTel API using a Tuple to store the SpanContext info,
321336 # this will not change the values.
322- ctx .trace_id = ctx .span_id = trace_id
337+ span_ctx .trace_id = span_ctx .span_id = trace_id
323338
324339 # Test propagation
325340 downstream_carrier = {}
326341
327- self .hptc .inject (ctx , downstream_carrier )
342+ self .hptc .inject (span_ctx , downstream_carrier )
328343
329344 # Assert the 'X-INSTANA-L' has been injected with the correct 0 value
330345 assert "X-INSTANA-L" in downstream_carrier
@@ -333,7 +348,7 @@ def test_w3c_off_x_instana_l_0(
333348 assert "traceparent" in downstream_carrier
334349 assert (
335350 downstream_carrier .get ("traceparent" )
336- == f"00-{ format_trace_id (tp_trace_id )} -{ format_span_id (ctx .span_id )} -00"
351+ == f"00-{ format_trace_id (tp_trace_id )} -{ format_span_id (span_ctx .span_id )} -00"
337352 )
338353
339354 # Assert that the tracestate is propagated when it is enabled
@@ -347,7 +362,8 @@ def test_suppression_when_child_level_is_lower(
347362 _span_id : int ,
348363 ) -> None :
349364 """
350- Test that span_context.level is updated when the child level (extracted from carrier) is lower than the current span_context.level.
365+ Test that span_context.level is updated when the child level (extracted from carrier) is lower than the
366+ current span_context.level.
351367 """
352368 # Create a span context with level=1
353369 original_span_context = SpanContext (
@@ -365,16 +381,17 @@ def test_suppression_when_child_level_is_lower(
365381
366382 # Extract the span context from the carrier to verify the level was updated
367383 extracted_context = self .hptc .extract (carrier_header )
384+ span_ctx = get_current_span (extracted_context ).get_span_context ()
368385
369386 # Verify that the level is 0 (suppressed)
370- assert extracted_context .level == 0
371- assert extracted_context .suppression
387+ assert span_ctx .level == 0
388+ assert span_ctx .suppression
372389
373390 # Create a new carrier to test the propagation
374391 downstream_carrier = {}
375392
376393 # Inject the extracted context into the downstream carrier
377- self .hptc .inject (extracted_context , downstream_carrier )
394+ self .hptc .inject (span_ctx , downstream_carrier )
378395
379396 # Verify that the downstream carrier has the correct level
380397 assert downstream_carrier .get ("X-INSTANA-L" ) == "0"
0 commit comments