16
16
INVALID_SPAN_ID ,
17
17
INVALID_TRACE_ID ,
18
18
)
19
+ from sentry_sdk import get_client , start_transaction
19
20
from sentry_sdk .consts import INSTRUMENTER
20
- from sentry_sdk .hub import Hub
21
21
from sentry_sdk .integrations .opentelemetry .consts import (
22
22
SENTRY_BAGGAGE_KEY ,
23
23
SENTRY_TRACE_KEY ,
40
40
41
41
def link_trace_context_to_error_event (event , otel_span_map ):
42
42
# type: (Event, Dict[str, Union[Transaction, SentrySpan]]) -> Event
43
- hub = Hub .current
44
- if not hub :
45
- return event
43
+ client = get_client ()
46
44
47
- if hub . client and hub . client .options ["instrumenter" ] != INSTRUMENTER .OTEL :
45
+ if client .options ["instrumenter" ] != INSTRUMENTER .OTEL :
48
46
return event
49
47
50
48
if hasattr (event , "type" ) and event ["type" ] == "transaction" :
@@ -116,25 +114,23 @@ def _prune_old_spans(self):
116
114
117
115
def on_start (self , otel_span , parent_context = None ):
118
116
# type: (OTelSpan, Optional[SpanContext]) -> None
119
- hub = Hub .current
120
- if not hub :
121
- return
117
+ client = get_client ()
122
118
123
- if not hub . client or ( hub . client and not hub . client . dsn ) :
119
+ if not client . dsn :
124
120
return
125
121
126
122
try :
127
- _ = Dsn (hub . client .dsn or "" )
123
+ _ = Dsn (client .dsn )
128
124
except Exception :
129
125
return
130
126
131
- if hub . client and hub . client .options ["instrumenter" ] != INSTRUMENTER .OTEL :
127
+ if client .options ["instrumenter" ] != INSTRUMENTER .OTEL :
132
128
return
133
129
134
130
if not otel_span .get_span_context ().is_valid :
135
131
return
136
132
137
- if self ._is_sentry_span (hub , otel_span ):
133
+ if self ._is_sentry_span (otel_span ):
138
134
return
139
135
140
136
trace_data = self ._get_trace_data (otel_span , parent_context )
@@ -155,7 +151,7 @@ def on_start(self, otel_span, parent_context=None):
155
151
instrumenter = INSTRUMENTER .OTEL ,
156
152
)
157
153
else :
158
- sentry_span = hub . start_transaction (
154
+ sentry_span = start_transaction (
159
155
name = otel_span .name ,
160
156
span_id = trace_data ["span_id" ],
161
157
parent_span_id = parent_span_id ,
@@ -179,11 +175,9 @@ def on_start(self, otel_span, parent_context=None):
179
175
180
176
def on_end (self , otel_span ):
181
177
# type: (OTelSpan) -> None
182
- hub = Hub .current
183
- if not hub :
184
- return
178
+ client = get_client ()
185
179
186
- if hub . client and hub . client .options ["instrumenter" ] != INSTRUMENTER .OTEL :
180
+ if client .options ["instrumenter" ] != INSTRUMENTER .OTEL :
187
181
return
188
182
189
183
span_context = otel_span .get_span_context ()
@@ -219,14 +213,18 @@ def on_end(self, otel_span):
219
213
self .open_spans .setdefault (span_start_in_minutes , set ()).discard (span_id )
220
214
self ._prune_old_spans ()
221
215
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
224
218
"""
225
219
Break infinite loop:
226
220
HTTP requests to Sentry are caught by OTel and send again to Sentry.
227
221
"""
228
222
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
230
228
231
229
if otel_span_url and dsn_url in otel_span_url :
232
230
return True
0 commit comments