From c8e9f1b03f50ec961161cb167ae3c39ef2dae8ce Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Mon, 14 Apr 2025 15:42:23 +0200 Subject: [PATCH 1/2] Wrap otel span if set on scope --- sentry_sdk/scope.py | 6 +++++- sentry_sdk/tracing.py | 6 +++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 1f3162605f..9677416e0d 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -8,6 +8,7 @@ from datetime import datetime, timezone from functools import wraps from itertools import chain +from opentelemetry.trace import Span as OTelSpan from sentry_sdk._types import AnnotatedValue from sentry_sdk.attachments import Attachment @@ -748,7 +749,10 @@ def span(self): def span(self, span): # type: (Optional[Span]) -> None """Set current tracing span.""" - self._span = span + if isinstance(span, OTelSpan): + self._span = Span(otel_span=span) + else: + self._span = span @property def profile(self): diff --git a/sentry_sdk/tracing.py b/sentry_sdk/tracing.py index c56a7e729c..a8ed04dfc4 100644 --- a/sentry_sdk/tracing.py +++ b/sentry_sdk/tracing.py @@ -6,7 +6,7 @@ from opentelemetry.trace import ( format_trace_id, format_span_id, - Span as OtelSpan, + Span as OTelSpan, TraceState, get_current_span, INVALID_SPAN, @@ -182,7 +182,7 @@ def __init__( attributes=None, # type: Optional[dict[str, Any]] only_if_parent=False, # type: bool parent_span=None, # type: Optional[Span] - otel_span=None, # type: Optional[OtelSpan] + otel_span=None, # type: Optional[OTelSpan] ): # type: (...) -> None """ @@ -319,7 +319,7 @@ def origin(self, value): def root_span(self): # type: () -> Optional[Span] root_otel_span = cast( - "Optional[OtelSpan]", get_sentry_meta(self._otel_span, "root_span") + "Optional[OTelSpan]", get_sentry_meta(self._otel_span, "root_span") ) return Span(otel_span=root_otel_span) if root_otel_span else None From a2f365568c2727a369bb29fa8362ec69abf5af63 Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Mon, 14 Apr 2025 15:43:21 +0200 Subject: [PATCH 2/2] newline --- sentry_sdk/scope.py | 1 + 1 file changed, 1 insertion(+) diff --git a/sentry_sdk/scope.py b/sentry_sdk/scope.py index 9677416e0d..dbc26e28e1 100644 --- a/sentry_sdk/scope.py +++ b/sentry_sdk/scope.py @@ -8,6 +8,7 @@ from datetime import datetime, timezone from functools import wraps from itertools import chain + from opentelemetry.trace import Span as OTelSpan from sentry_sdk._types import AnnotatedValue