Skip to content

Commit 103091b

Browse files
pvitalGSVarsha
andcommitted
refactor(tracer) use Context instead of SpanContext
To maintain OpenTelemetry API compliance, this commit changes the InstanaTracer to use OTel's `Context` instead of the Instana's `SpanContext` as `context` parameter for `start_as_current_span()` and `start_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 2d3c4c8 commit 103091b

2 files changed

Lines changed: 14 additions & 12 deletions

File tree

src/instana/span/span.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ def assure_errored(self) -> None:
241241
INVALID_SPAN = NonRecordingSpan(INVALID_SPAN_CONTEXT)
242242

243243

244-
def get_current_span(context: Optional[Context] = None) -> InstanaSpan:
244+
def get_current_span(context: Optional[Context] = None) -> Union[InstanaSpan, Span]:
245245
"""Retrieve the current span.
246246
247247
Args:
@@ -252,6 +252,6 @@ def get_current_span(context: Optional[Context] = None) -> InstanaSpan:
252252
The Span set in the context if it exists. INVALID_SPAN otherwise.
253253
"""
254254
span = get_value(_SPAN_KEY, context=context)
255-
if span is None or not isinstance(span, InstanaSpan):
255+
if span is None or not isinstance(span, (InstanaSpan, Span)):
256256
return INVALID_SPAN
257257
return span

src/instana/tracer.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44

55
import time
6-
from contextlib import contextmanager
76
from typing import TYPE_CHECKING, Iterator, Mapping, Optional, Type, Union
87

98
from opentelemetry.context.context import Context
@@ -16,6 +15,7 @@
1615
use_span,
1716
)
1817
from opentelemetry.util import types
18+
from opentelemetry.util._decorator import _agnosticcontextmanager
1919

2020
from instana.agent.host import HostAgent
2121
from instana.log import logger
@@ -32,6 +32,8 @@
3232
from instana.util.ids import generate_id
3333

3434
if TYPE_CHECKING:
35+
from opentelemetry.trace import Span
36+
3537
from instana.agent.base import BaseAgent
3638
from instana.propagators.base_propagator import BasePropagator, CarrierT
3739

@@ -108,17 +110,15 @@ def exporter(self) -> Optional[Type["BaseAgent"]]:
108110
def start_span(
109111
self,
110112
name: str,
111-
span_context: Optional[SpanContext] = None,
113+
context: Optional[Context] = None,
112114
kind: SpanKind = SpanKind.INTERNAL,
113115
attributes: types.Attributes = None,
114116
links: _Links = None,
115117
start_time: Optional[int] = None,
116118
record_exception: bool = True,
117119
set_status_on_exception: bool = True,
118120
) -> InstanaSpan:
119-
parent_context = (
120-
span_context if span_context else get_current_span().get_span_context()
121-
)
121+
parent_context = get_current_span(context).get_span_context()
122122

123123
if parent_context and not isinstance(parent_context, SpanContext):
124124
raise TypeError("parent_context must be an Instana SpanContext or None.")
@@ -136,22 +136,22 @@ def start_span(
136136

137137
return span
138138

139-
@contextmanager
139+
@_agnosticcontextmanager
140140
def start_as_current_span(
141141
self,
142142
name: str,
143-
span_context: Optional[SpanContext] = None,
143+
context: Optional[Context] = None,
144144
kind: SpanKind = SpanKind.INTERNAL,
145145
attributes: types.Attributes = None,
146146
links: _Links = None,
147147
start_time: Optional[int] = None,
148148
record_exception: bool = True,
149149
set_status_on_exception: bool = True,
150150
end_on_exit: bool = True,
151-
) -> Iterator[InstanaSpan]:
151+
) -> Iterator["Span"]:
152152
span = self.start_span(
153153
name=name,
154-
span_context=span_context,
154+
context=context,
155155
kind=kind,
156156
attributes=attributes,
157157
links=links,
@@ -167,7 +167,9 @@ def start_as_current_span(
167167
) as span:
168168
yield span
169169

170-
def _create_span_context(self, parent_context: SpanContext) -> SpanContext:
170+
def _create_span_context(
171+
self, parent_context: Optional[SpanContext] = None
172+
) -> SpanContext:
171173
"""Creates a new SpanContext based on the given parent context."""
172174

173175
if parent_context and parent_context.is_valid:

0 commit comments

Comments
 (0)