Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/instana/propagators/base_propagator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
3 changes: 1 addition & 2 deletions src/instana/span/base_span.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -117,4 +117,3 @@ def _convert_attribute_value(self, value):
logger.debug(final_value, exc_info=True)
return None
return final_value

11 changes: 8 additions & 3 deletions src/instana/span/readable_span.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/instana/span/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down
8 changes: 4 additions & 4 deletions src/instana/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ 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,
start_time: Optional[int] = None,
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.")
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
7 changes: 0 additions & 7 deletions tests/test_span.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_span_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
14 changes: 7 additions & 7 deletions tests/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand All @@ -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(
Expand All @@ -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)
Expand Down Expand Up @@ -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(
Expand All @@ -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:
Expand Down