Skip to content

Commit 66f530a

Browse files
ref(otel): Use new scopes API (#2865)
--------- Co-authored-by: Daniel Szoke <[email protected]>
1 parent 301028b commit 66f530a

File tree

2 files changed

+83
-114
lines changed

2 files changed

+83
-114
lines changed

sentry_sdk/integrations/opentelemetry/span_processor.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
INVALID_SPAN_ID,
1717
INVALID_TRACE_ID,
1818
)
19+
from sentry_sdk import get_client, start_transaction
1920
from sentry_sdk.consts import INSTRUMENTER
20-
from sentry_sdk.hub import Hub
2121
from sentry_sdk.integrations.opentelemetry.consts import (
2222
SENTRY_BAGGAGE_KEY,
2323
SENTRY_TRACE_KEY,
@@ -40,11 +40,9 @@
4040

4141
def link_trace_context_to_error_event(event, otel_span_map):
4242
# type: (Event, Dict[str, Union[Transaction, SentrySpan]]) -> Event
43-
hub = Hub.current
44-
if not hub:
45-
return event
43+
client = get_client()
4644

47-
if hub.client and hub.client.options["instrumenter"] != INSTRUMENTER.OTEL:
45+
if client.options["instrumenter"] != INSTRUMENTER.OTEL:
4846
return event
4947

5048
if hasattr(event, "type") and event["type"] == "transaction":
@@ -116,25 +114,23 @@ def _prune_old_spans(self):
116114

117115
def on_start(self, otel_span, parent_context=None):
118116
# type: (OTelSpan, Optional[SpanContext]) -> None
119-
hub = Hub.current
120-
if not hub:
121-
return
117+
client = get_client()
122118

123-
if not hub.client or (hub.client and not hub.client.dsn):
119+
if not client.dsn:
124120
return
125121

126122
try:
127-
_ = Dsn(hub.client.dsn or "")
123+
_ = Dsn(client.dsn)
128124
except Exception:
129125
return
130126

131-
if hub.client and hub.client.options["instrumenter"] != INSTRUMENTER.OTEL:
127+
if client.options["instrumenter"] != INSTRUMENTER.OTEL:
132128
return
133129

134130
if not otel_span.get_span_context().is_valid:
135131
return
136132

137-
if self._is_sentry_span(hub, otel_span):
133+
if self._is_sentry_span(otel_span):
138134
return
139135

140136
trace_data = self._get_trace_data(otel_span, parent_context)
@@ -155,7 +151,7 @@ def on_start(self, otel_span, parent_context=None):
155151
instrumenter=INSTRUMENTER.OTEL,
156152
)
157153
else:
158-
sentry_span = hub.start_transaction(
154+
sentry_span = start_transaction(
159155
name=otel_span.name,
160156
span_id=trace_data["span_id"],
161157
parent_span_id=parent_span_id,
@@ -179,11 +175,9 @@ def on_start(self, otel_span, parent_context=None):
179175

180176
def on_end(self, otel_span):
181177
# type: (OTelSpan) -> None
182-
hub = Hub.current
183-
if not hub:
184-
return
178+
client = get_client()
185179

186-
if hub.client and hub.client.options["instrumenter"] != INSTRUMENTER.OTEL:
180+
if client.options["instrumenter"] != INSTRUMENTER.OTEL:
187181
return
188182

189183
span_context = otel_span.get_span_context()
@@ -219,14 +213,18 @@ def on_end(self, otel_span):
219213
self.open_spans.setdefault(span_start_in_minutes, set()).discard(span_id)
220214
self._prune_old_spans()
221215

222-
def _is_sentry_span(self, hub, otel_span):
223-
# type: (Hub, OTelSpan) -> bool
216+
def _is_sentry_span(self, otel_span):
217+
# type: (OTelSpan) -> bool
224218
"""
225219
Break infinite loop:
226220
HTTP requests to Sentry are caught by OTel and send again to Sentry.
227221
"""
228222
otel_span_url = otel_span.attributes.get(SpanAttributes.HTTP_URL, None)
229-
dsn_url = hub.client and Dsn(hub.client.dsn or "").netloc
223+
224+
dsn_url = None
225+
client = get_client()
226+
if client.dsn:
227+
dsn_url = Dsn(client.dsn).netloc
230228

231229
if otel_span_url and dsn_url in otel_span_url:
232230
return True

0 commit comments

Comments
 (0)