8000 Set the correct SpanContext in continue_trace · getsentry/sentry-python@d037a10 · GitHub
[go: up one dir, main page]

Skip to content

Commit d037a10

Browse files
committed
Set the correct SpanContext in continue_trace
1 parent 56e5312 commit d037a10

File tree

3 files changed

+46
-8
lines changed

3 files changed

+46
-8
lines changed

sentry_sdk/integrations/opentelemetry/potel_span_processor.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def on_end(self, span):
5252
return
5353

5454
# TODO-neel-potel-remote only take parent if not remote
55-
if span.parent:
55+
if span.parent and not span.parent.is_remote:
5656
self._children_spans[span.parent.span_id].append(span)
5757
else:
5858
# 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, span=None, custom_sampling_context=None, **kwargs):
102+
# type: (Optional[POTelSpan], 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)
0