Skip to content

Commit 0082dd9

Browse files
committed
Set the correct SpanContext in continue_trace
1 parent 3d8cc7c commit 0082dd9

File tree

4 files changed

+47
-11
lines changed

4 files changed

+47
-11
lines changed

sentry_sdk/api.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,6 @@ def flush(
237237

238238
def start_span(
239239
*,
240-
span=None,
241240
custom_sampling_context=None,
242241
**kwargs, # type: Any
243242
):
@@ -255,7 +254,7 @@ def start_span(
255254
method.
256255
"""
257256
# TODO: Consider adding type hints to the method signature.
258-
return get_current_scope().start_span(span, custom_sampling_context, **kwargs)
257+
return get_current_scope().start_span(custom_sampling_context, **kwargs)
259258

260259

261260
def start_transaction(

sentry_sdk/integrations/opentelemetry/potel_span_processor.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ def on_end(self, span):
5151
if is_sentry_span(span):
5252
return
5353

54-
# TODO-neel-potel-remote only take parent if not remote
55-
if span.parent:
54+
if span.parent and not span.parent.is_remote:
5655
self._children_spans[span.parent.span_id].append(span)
5756
else:
5857
# if have a root span ending, we build a transaction and send it

sentry_sdk/integrations/opentelemetry/scope.py

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
from contextlib import contextmanager
33

44
from opentelemetry.context import get_value, set_value, attach, detach, get_current
5+
from opentelemetry.trace import SpanContext, NonRecordingSpan, TraceFlags, use_span
56

67
from sentry_sdk.scope import Scope, ScopeType
8+
from sentry_sdk.tracing import POTelSpan
79
from sentry_sdk.integrations.opentelemetry.consts import (
810
SENTRY_SCOPES_KEY,
911
SENTRY_FORK_ISOLATION_SCOPE_KEY,
@@ -14,6 +16,8 @@
1416
if TYPE_CHECKING:
1517
from typing import Tuple, Optional, Generator, Dict, Any
1618

19+
from sentry_sdk._types import SamplingContext
20+
1721

1822
class PotelScope(Scope):
1923
@classmethod
@@ -61,10 +65,43 @@ def _get_isolation_scope(cls):
6165
@contextmanager
6266
def continue_trace(self, environ_or_headers):
6367
# type: (Dict[str, Any]) -> Generator[None, None, None]
64-
with new_scope() as scope:
65-
scope.generate_propagation_context(environ_or_headers)
66-
# TODO-neel-potel add remote span on context
68+
self.generate_propagation_context(environ_or_headers)
69+
70+
span_context = self._incoming_otel_span_context()
71+
if span_context is None:
6772
yield
73+
else:
74+
with use_span(NonRecordingSpan(span_context)):
75+
yield
76+
77+
def _incoming_otel_span_context(self):
78+
# type: () -> Optional[SpanContext]
79+
if self._propagation_context is None:
80+
return None
81+
# If sentry-trace extraction didn't have a parent_span_id, we don't have an upstream header
82+
if self._propagation_context.parent_span_id is None:
83+
return None
84+
85+
trace_flags = TraceFlags(
86+
TraceFlags.SAMPLED
87+
if self._propagation_context.parent_sampled
88+
else TraceFlags.DEFAULT
89+
)
90+
91+
# TODO-neel-potel tracestate
92+
span_context = SpanContext(
93+
trace_id=int(self._propagation_context.trace_id, 16), # type: ignore
94+
span_id=int(self._propagation_context.parent_span_id, 16), # type: ignore
95+
is_remote=True,
96+
trace_flags=trace_flags,
97+
)
98+
99+
return span_context
100+
101+
def start_span(self, custom_sampling_context=None, **kwargs):
102+
# type: (Optional[SamplingContext], Any) -> POTelSpan
103+
# TODO-neel-potel ideally want to remove the span argument, discuss with ivana
104+
return POTelSpan(**kwargs, scope=self)
68105

69106

70107
_INITIAL_CURRENT_SCOPE = PotelScope(ty=ScopeType.CURRENT)

sentry_sdk/tracing.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from datetime import datetime, timedelta, timezone
44

55
from opentelemetry import trace as otel_trace, context
6+
from opentelemetry.trace import format_trace_id, format_span_id
67
from opentelemetry.trace.status import StatusCode
78

89
import sentry_sdk
@@ -1346,13 +1347,13 @@ def parent_span_id(self):
13461347

13471348
@property
13481349
def trace_id(self):
1349-
# type: () -> Optional[str]
1350-
return self._otel_span.get_span_context().trace_id
1350+
# type: () -> str
1351+
return format_trace_id(self._otel_span.get_span_context().trace_id)
13511352

13521353
@property
13531354
def span_id(self):
1354-
# type: () -> Optional[str]
1355-
return self._otel_span.get_span_context().span_id
1355+
# type: () -> str
1356+
return format_span_id(self._otel_span.get_span_context().span_id)
13561357

13571358
@property
13581359
def sampled(self):

0 commit comments

Comments
 (0)