Skip to content

Commit 4861510

Browse files
pvitalGSVarsha
andcommitted
refactor(propagators): Use context parameter instead of span_context
This commit updates base_propagator to use the `context` parameter instead of the deprecated `span_context` parameter when calling `start_span()` and `start_as_current_span()` methods. This change aligns with OpenTelemetry's API conventions and improves consistency across the codebase. This commit fixes #847. Signed-off-by: Paulo Vital <paulo.vital@ibm.com> Co-authored-by: Varsha GS <varsha.gs@ibm.com>
1 parent 103091b commit 4861510

2 files changed

Lines changed: 19 additions & 13 deletions

File tree

src/instana/propagators/base_propagator.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,29 @@
33

44

55
import os
6+
from typing import Any, Dict, List, Optional, Tuple, TypeVar
67

7-
from typing import Any, Optional, TypeVar, Dict, List, Tuple
8+
from opentelemetry.context.context import Context
9+
from opentelemetry.trace import (
10+
INVALID_SPAN_ID,
11+
INVALID_TRACE_ID,
12+
NonRecordingSpan,
13+
set_span_in_context,
14+
)
815

916
from instana.log import logger
1017
from instana.span_context import SpanContext
1118
from instana.util.ids import (
1219
header_to_id,
1320
header_to_long_id,
1421
hex_id,
22+
hex_id_limited,
1523
internal_id,
1624
internal_id_limited,
17-
hex_id_limited,
1825
)
1926
from instana.w3c_trace_context.traceparent import Traceparent
2027
from instana.w3c_trace_context.tracestate import Tracestate
2128

22-
from opentelemetry.trace import (
23-
INVALID_SPAN_ID,
24-
INVALID_TRACE_ID,
25-
)
26-
2729
# The carrier, typed here as CarrierT, can be a dict, a list, or a tuple.
2830
# Using the trace header as an example, it can be in the following forms
2931
# for extraction:
@@ -399,7 +401,7 @@ def __extract_w3c_trace_context_headers(self, dc):
399401

400402
def extract(
401403
self, carrier: CarrierT, disable_w3c_trace_context: bool = False
402-
) -> Optional[SpanContext]:
404+
) -> Optional[Context]:
403405
"""
404406
This method overrides one of the Base classes as with the introduction
405407
of W3C trace context for the HTTP requests more extracting steps and
@@ -441,7 +443,9 @@ def extract(
441443
tracestate,
442444
disable_w3c_trace_context,
443445
)
444-
return span_context
446+
447+
context = set_span_in_context(NonRecordingSpan(span_context), Context())
448+
return context
445449

446450
except Exception:
447451
logger.debug("base_propagator extract error:", exc_info=True)

src/instana/propagators/kafka_propagator.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
# (c) Copyright IBM Corp. 2025
22
from typing import Any, Dict, Optional
33

4+
from opentelemetry.context.context import Context
45
from opentelemetry.trace.span import format_span_id
56

67
from instana.log import logger
78
from instana.propagators.base_propagator import BasePropagator, CarrierT
8-
from instana.util.ids import hex_id_limited
99
from instana.span_context import SpanContext
10+
from instana.util.ids import hex_id_limited
11+
1012

1113
class KafkaPropagator(BasePropagator):
1214
"""
@@ -50,7 +52,7 @@ def extract_carrier_headers(self, carrier: CarrierT) -> Dict[str, Any]:
5052

5153
def extract(
5254
self, carrier: CarrierT, disable_w3c_trace_context: bool = False
53-
) -> Optional[SpanContext]:
55+
) -> Optional[Context]:
5456
"""
5557
This method overrides one of the Base classes as with the introduction
5658
of W3C trace context for the Kafka requests more extracting steps and
@@ -61,7 +63,7 @@ def extract(
6163
disable_w3c_trace_context (bool): A flag to disable the W3C trace context.
6264
6365
Returns:
64-
Optional[SpanContext]: The extracted span context or None.
66+
Optional[Context]: The extracted span context or None.
6567
"""
6668
try:
6769
headers = self.extract_carrier_headers(carrier=carrier)
@@ -118,7 +120,7 @@ def inject(
118120
correlation_type=span_context.correlation_type,
119121
correlation_id=span_context.correlation_id,
120122
traceparent=span_context.traceparent,
121-
tracestate=span_context.tracestate
123+
tracestate=span_context.tracestate,
122124
)
123125

124126
def inject_key_value(carrier, key, value):

0 commit comments

Comments
 (0)