33
44
55import time
6- from contextlib import contextmanager
76from typing import TYPE_CHECKING , Iterator , Mapping , Optional , Type , Union
87
98from opentelemetry .context .context import Context
1615 use_span ,
1716)
1817from opentelemetry .util import types
18+ from opentelemetry .util ._decorator import _agnosticcontextmanager
1919
2020from instana .agent .host import HostAgent
2121from instana .log import logger
3232from instana .util .ids import generate_id
3333
3434if 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