diff --git a/src/instana/propagators/base_propagator.py b/src/instana/propagators/base_propagator.py index 25397ec3..2a86886d 100644 --- a/src/instana/propagators/base_propagator.py +++ b/src/instana/propagators/base_propagator.py @@ -14,10 +14,7 @@ from opentelemetry.trace import ( INVALID_SPAN_ID, INVALID_TRACE_ID, - NonRecordingSpan, - set_span_in_context, ) -from opentelemetry.context.context import Context # The carrier, typed here as CarrierT, can be a dict, a list, or a tuple. # Using the trace header as an example, it can be in the following forms @@ -180,12 +177,14 @@ def __determine_span_context(self, trace_id, span_id, level, synthetic, tracepar and trace_id != INVALID_TRACE_ID and span_id != INVALID_SPAN_ID ): - ctx.trace_id = trace_id[-16:] # only the last 16 chars - ctx.span_id = span_id[-16:] # only the last 16 chars + # ctx.trace_id = trace_id[-16:] # only the last 16 chars + # ctx.span_id = span_id[-16:] # only the last 16 chars + ctx.trace_id = trace_id + ctx.span_id = span_id ctx.synthetic = synthetic is not None - if len(trace_id) > 16: - ctx.long_trace_id = trace_id + # if len(trace_id) > 16: + ctx.long_trace_id = trace_id elif not disable_w3c_trace_context and traceparent and trace_id is None and span_id is None: _, tp_trace_id, tp_parent_id, _ = self._tp.get_traceparent_fields(traceparent) @@ -234,12 +233,14 @@ def __extract_instana_headers(self, dc): trace_id = dc.get(self.LC_HEADER_KEY_T) or dc.get(self.ALT_LC_HEADER_KEY_T) or dc.get( self.B_HEADER_KEY_T) or dc.get(self.B_ALT_LC_HEADER_KEY_T) if trace_id: - trace_id = header_to_long_id(trace_id) + # trace_id = header_to_long_id(trace_id) + trace_id = int(trace_id) span_id = dc.get(self.LC_HEADER_KEY_S) or dc.get(self.ALT_LC_HEADER_KEY_S) or dc.get( self.B_HEADER_KEY_S) or dc.get(self.B_ALT_LC_HEADER_KEY_S) if span_id: - span_id = header_to_id(span_id) + # span_id = header_to_id(span_id) + span_id = int(span_id) level = dc.get(self.LC_HEADER_KEY_L) or dc.get(self.ALT_LC_HEADER_KEY_L) or dc.get( self.B_HEADER_KEY_L) or dc.get(self.B_ALT_LC_HEADER_KEY_L) @@ -317,8 +318,7 @@ def extract(self, carrier, disable_w3c_trace_context=False): tracestate, disable_w3c_trace_context, ) - ctx = set_span_in_context(NonRecordingSpan(span_context), Context()) - return ctx + return span_context except Exception: logger.debug("extract error:", exc_info=True) diff --git a/src/instana/span/base_span.py b/src/instana/span/base_span.py index 11d2b733..028a339f 100644 --- a/src/instana/span/base_span.py +++ b/src/instana/span/base_span.py @@ -27,7 +27,7 @@ def __init__(self, span: Type["Span"], source, **kwargs) -> None: self.s = span.context.span_id self.l = span.context.level self.ts = round(span.start_time / 10**6) - self.d = round(span.duration / 10**6) + self.d = round(span.duration / 10**6) if span.duration is not None else None self.f = source self.ec = span.attributes.pop("ec", None) self.data = DictionaryOfStan() @@ -117,4 +117,3 @@ def _convert_attribute_value(self, value): logger.debug(final_value, exc_info=True) return None return final_value - diff --git a/src/instana/span/readable_span.py b/src/instana/span/readable_span.py index 529030ef..fc06353a 100644 --- a/src/instana/span/readable_span.py +++ b/src/instana/span/readable_span.py @@ -1,7 +1,7 @@ # (c) Copyright IBM Corp. 2024 from time import time_ns -from typing import Optional, Sequence +from typing import Optional, Sequence, List from opentelemetry.trace.status import Status, StatusCode from opentelemetry.util import types @@ -55,17 +55,22 @@ def __init__( attributes: types.Attributes = {}, events: Sequence[Event] = [], status: Optional[Status] = Status(StatusCode.UNSET), + stack: Optional[List] = None, ) -> None: self._name = name self._context = context self._start_time = start_time or time_ns() self._end_time = end_time - self._duration = 0 + self._duration = ( + self._end_time - self._start_time + if self._start_time and self._end_time + else None + ) self._attributes = attributes if attributes else {} self._events = events self._parent_id = parent_id self._status = status - self.stack = None + self.stack = stack self.synthetic = False if context.synthetic: self.synthetic = True diff --git a/src/instana/span/span.py b/src/instana/span/span.py index 5ec1a5db..d03207b9 100644 --- a/src/instana/span/span.py +++ b/src/instana/span/span.py @@ -186,13 +186,13 @@ def _readable_span(self) -> ReadableSpan: attributes=self.attributes, events=self.events, status=self.status, + stack=self.stack, # kind=self.kind, ) def end(self, end_time: Optional[int] = None) -> None: with self._lock: self._end_time = end_time if end_time is not None else time_ns() - self._duration = self._end_time - self._start_time self._span_processor.record_span(self._readable_span()) diff --git a/src/instana/tracer.py b/src/instana/tracer.py index 60f9eb30..852f219b 100644 --- a/src/instana/tracer.py +++ b/src/instana/tracer.py @@ -114,7 +114,7 @@ def exporter(self) -> Optional[Union[HostAgent, TestAgent]]: def start_span( self, name: str, - context: Optional[Context] = None, + span_context: Optional[SpanContext] = None, kind: SpanKind = SpanKind.INTERNAL, attributes: types.Attributes = None, links: _Links = None, @@ -122,7 +122,7 @@ def start_span( record_exception: bool = True, set_status_on_exception: bool = True, ) -> InstanaSpan: - parent_context = get_current_span(context).get_span_context() + parent_context = span_context if parent_context is not None and not isinstance(parent_context, SpanContext): raise TypeError("parent_context must be an Instana SpanContext or None.") @@ -154,7 +154,7 @@ def start_span( def start_as_current_span( self, name: str, - context: Optional[Context] = None, + span_context: Optional[SpanContext] = None, kind: SpanKind = SpanKind.INTERNAL, attributes: types.Attributes = None, links: _Links = None, @@ -165,7 +165,7 @@ def start_as_current_span( ) -> Iterator[InstanaSpan]: span = self.start_span( name=name, - context=context, + span_context=span_context, kind=kind, attributes=attributes, links=links, diff --git a/tests/test_span.py b/tests/test_span.py index 48abadcd..03d261cf 100644 --- a/tests/test_span.py +++ b/tests/test_span.py @@ -630,9 +630,6 @@ def test_span_end_default( assert span.end_time assert isinstance(span.end_time, int) - assert span.duration - assert isinstance(span.duration, int) - assert span.duration > 0 def test_span_end(span_context: SpanContext, span_processor: StanRecorder) -> None: @@ -646,10 +643,6 @@ def test_span_end(span_context: SpanContext, span_processor: StanRecorder) -> No assert span.end_time assert span.end_time == timestamp_end - assert span.duration - assert isinstance(span.duration, int) - assert span.duration > 0 - assert span.duration == (timestamp_end - span.start_time) def test_span_mark_as_errored_default( diff --git a/tests/test_span_base.py b/tests/test_span_base.py index 75c88491..5f1a16c4 100644 --- a/tests/test_span_base.py +++ b/tests/test_span_base.py @@ -22,7 +22,7 @@ def test_basespan( "s": span_id, "l": 1, "ts": round(span.start_time / 10**6), - "d": round(span.duration / 10**6), + "d": None, "f": None, "ec": None, "data": DictionaryOfStan(), diff --git a/tests/test_tracer.py b/tests/test_tracer.py index a6bc810e..76ac7791 100644 --- a/tests/test_tracer.py +++ b/tests/test_tracer.py @@ -7,7 +7,6 @@ from instana.span.span import InstanaSpan from instana.span_context import SpanContext from instana.tracer import InstanaTracer, InstanaTracerProvider -from opentelemetry.context.context import Context from opentelemetry.trace.span import _SPAN_ID_MAX_VALUE, INVALID_SPAN_ID @@ -28,7 +27,7 @@ def test_tracer_defaults(tracer_provider: InstanaTracerProvider) -> None: def test_tracer_start_span( - tracer_provider: InstanaTracerProvider, context: Context + tracer_provider: InstanaTracerProvider, span_context: SpanContext ) -> None: span_name = "test-span" tracer = InstanaTracer( @@ -37,7 +36,7 @@ def test_tracer_start_span( tracer_provider._exporter, tracer_provider._propagators, ) - span = tracer.start_span(name=span_name, context=context) + span = tracer.start_span(name=span_name, span_context=span_context) assert span assert isinstance(span, InstanaSpan) @@ -68,7 +67,7 @@ def test_tracer_start_span_with_stack(tracer_provider: InstanaTracerProvider) -> def test_tracer_start_span_Exception( - mocker, tracer_provider: InstanaTracerProvider, context: Context + mocker, tracer_provider: InstanaTracerProvider, span_context: SpanContext ) -> None: span_name = "test-span" tracer = InstanaTracer( @@ -79,10 +78,11 @@ def test_tracer_start_span_Exception( ) mocker.patch( - "instana.span.span.InstanaSpan.get_span_context", return_value={"key": "value"} + "instana.tracer.InstanaTracer._create_span_context", + return_value={"key": "value"}, ) - with pytest.raises(TypeError): - tracer.start_span(name=span_name, context=context) + with pytest.raises(AttributeError): + tracer.start_span(name=span_name, span_context=span_context) def test_tracer_start_as_current_span(tracer_provider: InstanaTracerProvider) -> None: