8000 Use root span transaction name in populated head DSC (#3677) · getsentry/sentry-python@e72e62f · GitHub
[go: up one dir, main page]

Skip to content

Commit e72e62f

Browse files
authored
Use root span transaction name in populated head DSC (#3677)
1 parent 8e339bd commit e72e62f

File tree

4 files changed

+38
-46
lines changed

4 files changed

+38
-46
lines changed

sentry_sdk/integrations/opentelemetry/integration.py

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
try:
1919
from opentelemetry import trace
2020
from opentelemetry.propagate import set_global_textmap
21-
from opentelemetry.trace import Span as AbstractSpan
2221
from opentelemetry.sdk.trace import TracerProvider, Span, ReadableSpan
2322
except ImportError:
2423
raise DidNotEnable("opentelemetry not installed")
@@ -28,11 +27,6 @@
2827
except ImportError:
2928
DjangoInstrumentor = None
3029

31-
from sentry_sdk._types import TYPE_CHECKING
32-
33-
if TYPE_CHECKING:
34-
from typing import Union, Any
35-
3630

3731
CONFIGURABLE_INSTRUMENTATIONS = {
3832
DjangoInstrumentor: {"is_sql_commentor_enabled": True},
@@ -63,23 +57,12 @@ def _patch_readable_span():
6357
We need to pass through sentry specific metadata/objects from Span to ReadableSpan
6458
to work with them consistently in the SpanProcessor.
6559
"""
66-
67-
@property
68-
def sentry_meta(self):
69-
# type: (Union[AbstractSpan, Span, ReadableSpan]) -> dict[str, Any]
70-
if not getattr(self, "_sentry_meta", None):
71-
self._sentry_meta = {}
72-
return self._sentry_meta
73-
74-
AbstractSpan.sentry_meta = sentry_meta
75-
ReadableSpan.sentry_meta = sentry_meta
76-
7760
old_readable_span = Span._readable_span
7861

7962
def sentry_patched_readable_span(self):
8063
# type: (Span) -> ReadableSpan
8164
readable_span = old_readable_span(self)
82-
readable_span._sentry_meta = self._sentry_meta
65+
readable_span._sentry_meta = getattr(self, "_sentry_meta", {})
8366
return readable_span
8467

8568
Span._readable_span = sentry_patched_readable_span

sentry_sdk/integrations/opentelemetry/potel_span_processor.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
extract_span_data,
2121
extract_transaction_name_source,
2222
get_trace_context,
23+
get_sentry_meta,
24+
set_sentry_meta,
2325
)
2426
from sentry_sdk.integrations.opentelemetry.consts import (
2527
OTEL_SENTRY_CONTEXT,
@@ -85,12 +87,11 @@ def _add_root_span(self, span, parent_span):
8587
"""
8688
if parent_span != INVALID_SPAN and not parent_span.get_span_context().is_remote:
8789
# child span points to parent's root or parent
88-
span.sentry_meta["root_span"] = parent_span.sentry_meta.get(
89-
"root_span", parent_span
90-
)
90+
parent_root_span = get_sentry_meta(parent_span, "root_span")
91+
set_sentry_meta(span, "root_span", parent_root_span or parent_span)
9192
else:
9293
# root span points to itself
93-
span.sentry_meta["root_span"] = span
94+
set_sentry_meta(span, "root_span", span)
9495

9596
def _flush_root_span(self, span):
9697
# type: (ReadableSpan) -> None

sentry_sdk/integrations/opentelemetry/utils.py

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from urllib3.util import parse_url as urlparse
66
from urllib.parse import quote
77
from opentelemetry.trace import (
8-
Span,
8+
Span as AbstractSpan,
99
SpanKind,
1010
StatusCode,
1111
format_trace_id,
@@ -365,7 +365,7 @@ def has_incoming_trace(trace_state):
365365

366366

367367
def get_trace_state(span):
368-
# type: (Union[Span, ReadableSpan]) -> TraceState
368+
# type: (Union[AbstractSpan, ReadableSpan]) -> TraceState
369369
"""
370370
Get the existing trace_state with sentry items
371371
or populate it if we are the head SDK.
@@ -404,27 +404,31 @@ def get_trace_state(span):
404404
Baggage.SENTRY_PREFIX + "public_key", Dsn(options["dsn"]).public_key
405405
)
406406

407-
# we cannot access the root span in most cases here, so we HAVE to rely on the
408-
# scopes to carry the correct transaction name/source.
409-
# IDEALLY we will always move to using the isolation scope here
410-
# but our integrations do all kinds of stuff with both isolation and current
411-
# so I am keeping both for now as a best attempt solution till we get to a better state.
412-
isolation_scope = sentry_sdk.get_isolation_scope()
413-
current_scope = sentry_sdk.get_current_scope()
414-
if (
415-
current_scope.transaction_name
416-
and current_scope.transaction_source not in LOW_QUALITY_TRANSACTION_SOURCES
417-
):
418-
trace_state = trace_state.update(
419-
Baggage.SENTRY_PREFIX + "transaction", current_scope.transaction_name
420-
)
421-
elif (
422-
isolation_scope.transaction_name
423-
and isolation_scope.transaction_source
424-
not in LOW_QUALITY_TRANSACTION_SOURCES
425-
):
426-
trace_state = trace_state.update(
427-
Baggage.SENTRY_PREFIX + "transaction", isolation_scope.transaction_name
407+
root_span = get_sentry_meta(span, "root_span")
408+
if root_span and isinstance(root_span, ReadableSpan):
409+
transaction_name, transaction_source = extract_transaction_name_source(
410+
root_span
428411
)
429412

413+
if (
414+
transaction_name
415+
and transaction_source not in LOW_QUALITY_TRANSACTION_SOURCES
416+
):
417+
trace_state = trace_state.update(
418+
Baggage.SENTRY_PREFIX + "transaction", transaction_name
419+
)
420+
430421
return trace_state
422+
423+
424+
def get_sentry_meta(span, key):
425+
# type: (Union[AbstractSpan, ReadableSpan], str) -> Any
426+
sentry_meta = getattr(span, "_sentry_meta", None)
427+
return sentry_meta.get(key) if sentry_meta else None
428+
429+
430+
def set_sentry_meta(span, key, value):
431+
# type: (Union[AbstractSpan, ReadableSpan], str, Any) -> None
432+
sentry_meta = getattr(span, "_sentry_meta", {})
433+
sentry_meta[key] = value
434+
span._sentry_meta = sentry_meta

sentry_sdk/tracing.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1326,8 +1326,12 @@ def containing_transaction(self):
13261326
@property
13271327
def root_span(self):
13281328
# type: () -> Optional[POTelSpan]
1329+
from sentry_sdk.integrations.opentelemetry.utils import (
1330+
get_sentry_meta,
1331+
)
1332+
13291333
root_otel_span = cast(
1330-
"Optional[OtelSpan]", self._otel_span.sentry_meta.get("root_span", None)
1334+
"Optional[OtelSpan]", get_sentry_meta(self._otel_span, "root_span")
13311335
)
13321336
return POTelSpan(otel_span=root_otel_span) if root_otel_span else None
13331337

0 commit comments

Comments
 (0)
0