Skip to content

Commit 3ef5df4

Browse files
pvitalGSVarsha
andcommitted
refactor(tests): Use context parameter instead of span_context
This commit updates all tests to use the `context` parameter instead of the deprecated `span_context` parameter when calling `start_span()` and `start_as_current_span()` methods. This commit fixes #847. Signed-off-by: Paulo Vital <paulo.vital@ibm.com> Co-authored-by: Varsha GS <varsha.gs@ibm.com>
1 parent a2a68c8 commit 3ef5df4

4 files changed

Lines changed: 154 additions & 99 deletions

File tree

tests/collector/test_utils.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,30 @@
11
# (c) Copyright IBM Corp. 2025
22

33

4-
import pytest
54
from typing import Generator
5+
6+
import pytest
7+
from opentelemetry.context.context import Context
8+
from opentelemetry.trace.span import format_span_id
9+
610
from instana.collector.utils import format_span
711
from instana.singletons import get_tracer
812
from instana.span.registered_span import RegisteredSpan
913
from instana.span.span import get_current_span
10-
from opentelemetry.trace.span import format_span_id
11-
12-
from instana.span_context import SpanContext
1314

1415

1516
class TestUtils:
1617
@pytest.fixture(autouse=True)
1718
def _resource(self) -> Generator[None, None, None]:
1819
self.tracer = get_tracer()
1920
self.recorder = self.tracer.span_processor
20-
self.span_context = None
21+
self.context = None
2122
yield
2223

23-
def test_format_span(self, span_context: SpanContext) -> None:
24-
self.span_context = span_context
24+
def test_format_span(self, context: Context) -> None:
25+
self.context = context
2526
with self.tracer.start_as_current_span(
26-
name="span1", span_context=self.span_context
27+
name="span1", context=self.context
2728
) as pspan:
2829
expected_trace_id = format_span_id(pspan.context.trace_id)
2930
expected_span_id = format_span_id(pspan.context.span_id)
@@ -47,3 +48,4 @@ def test_format_span(self, span_context: SpanContext) -> None:
4748
assert formatted_spans[1].k == 1
4849
assert formatted_spans[1].s != formatted_spans[0].s
4950
assert formatted_spans[1].n == "span2"
51+
assert formatted_spans[1].n == "span2"

tests/propagators/test_http_propagator.py

Lines changed: 95 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from typing import Any, Dict, Generator
66

77
import pytest
8+
from opentelemetry.context.context import Context
89
from opentelemetry.trace import (
910
INVALID_SPAN_ID,
1011
INVALID_TRACE_ID,
@@ -13,6 +14,7 @@
1314
)
1415

1516
from instana.propagators.http_propagator import HTTPPropagator
17+
from instana.span.span import get_current_span
1618
from instana.span_context import SpanContext
1719
from 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"

tests/span/test_span.py

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,14 @@
66
from unittest.mock import patch
77

88
import pytest
9+
from opentelemetry.context.context import Context
10+
from opentelemetry.trace.span import NonRecordingSpan, Span
911
from opentelemetry.trace.status import Status, StatusCode
1012

1113
from instana.recorder import StanRecorder
1214
from instana.span.span import INVALID_SPAN, Event, InstanaSpan, get_current_span
1315
from instana.span_context import SpanContext
16+
from instana.tracer import InstanaTracerProvider
1417

1518

1619
class TestSpan:
@@ -837,7 +840,7 @@ def test_span_assure_errored_exception(
837840
self.span.assure_errored()
838841
assert not self.span.attributes
839842

840-
def test_get_current_span(self, context: SpanContext) -> None:
843+
def test_get_current_span(self, context: Context) -> None:
841844
self.span = get_current_span(context)
842845
assert isinstance(self.span, InstanaSpan)
843846

@@ -847,6 +850,44 @@ def test_get_current_span_INVALID_SPAN(self) -> None:
847850
assert self.span
848851
assert self.span == INVALID_SPAN
849852

853+
def test_get_current_span_OtelSpan(
854+
self,
855+
span_context: SpanContext,
856+
) -> None:
857+
"""Test get_current_span when get_value returns an OpenTelemetry Span object.
858+
859+
This test verifies that get_current_span() properly handles when get_value()
860+
returns a generic OpenTelemetry Span (NonRecordingSpan) that is not an InstanaSpan.
861+
"""
862+
# Create a mock OpenTelemetry Span (NonRecordingSpan)
863+
mock_otel_span = NonRecordingSpan(span_context)
864+
865+
# Mock get_value to return the OpenTelemetry Span
866+
with patch("instana.span.span.get_value", return_value=mock_otel_span):
867+
self.span = get_current_span()
868+
869+
assert self.span
870+
assert self.span == mock_otel_span
871+
assert isinstance(self.span, NonRecordingSpan)
872+
assert isinstance(self.span, Span)
873+
assert not isinstance(self.span, InstanaSpan)
874+
875+
def test_get_current_span_NoSpan(
876+
self,
877+
tracer_provider: InstanaTracerProvider,
878+
) -> None:
879+
"""Test get_current_span when get_value returns an different object.
880+
881+
This test verifies that get_current_span() properly handles when get_value()
882+
returns a generic object that is not an OpenTelemetry Span nor an InstanaSpan.
883+
"""
884+
# Mock get_value to return something that is not an OpenTelemetry Span nor an InstanaSpan.
885+
with patch("instana.span.span.get_value", return_value=tracer_provider):
886+
self.span = get_current_span()
887+
888+
assert self.span
889+
assert self.span == INVALID_SPAN
890+
850891
def test_span_duration_default(
851892
self,
852893
span_context: SpanContext,

0 commit comments

Comments
 (0)