8
8
from sentry_sdk .tracing import Span
9
9
10
10
import sentry_sdk
11
- from sentry_sdk .hub import Hub , _should_send_default_pii
11
+ from sentry_sdk .scope import should_send_default_pii
12
12
from sentry_sdk .integrations import DidNotEnable , Integration
13
- from sentry_sdk .utils import logger , capture_internal_exceptions , event_from_exception
13
+ from sentry_sdk .utils import (
14
+ logger ,
15
+ capture_internal_exceptions ,
16
+ event_from_exception ,
17
+ ensure_integration_enabled ,
18
+ )
14
19
15
20
try :
16
21
from openai .resources .chat .completions import Completions
@@ -62,16 +67,14 @@ def setup_once():
62
67
Embeddings .create = _wrap_embeddings_create (Embeddings .create )
63
68
64
69
65
- def _capture_exception (hub , exc ):
66
- # type: (Hub, Any) -> None
67
-
68
- if hub .client is not None :
69
- event , hint = event_from_exception (
70
- exc ,
71
- client_options = hub .client .options ,
72
- mechanism = {"type" : "openai" , "handled" : False },
73
- )
74
- hub .capture_event (event , hint = hint )
70
+ def _capture_exception (exc ):
71
+ # type: (Any) -> None
72
+ event , hint = event_from_exception (
73
+ exc ,
74
+ client_options = sentry_sdk .get_client ().options ,
75
+ mechanism = {"type" : "openai" , "handled" : False },
76
+ )
77
+ sentry_sdk .capture_event (event , hint = hint )
75
78
76
79
77
80
def _normalize_data (data ):
@@ -145,16 +148,9 @@ def _calculate_chat_completion_usage(
145
148
def _wrap_chat_completion_create (f ):
146
149
# type: (Callable[..., Any]) -> Callable[..., Any]
147
150
@wraps (f )
151
+ @ensure_integration_enabled (OpenAIIntegration , f )
148
152
def new_chat_completion (* args , ** kwargs ):
149
153
# type: (*Any, **Any) -> Any
150
- hub = Hub .current
151
- if not hub :
152
- return f (* args , ** kwargs )
153
-
154
- integration = hub .get_integration (OpenAIIntegration ) # type: OpenAIIntegration
155
- if not integration :
156
- return f (* args , ** kwargs )
157
-
158
154
if "messages" not in kwargs :
159
155
# invalid call (in all versions of openai), let it return error
160
156
return f (* args , ** kwargs )
@@ -177,19 +173,21 @@ def new_chat_completion(*args, **kwargs):
177
173
try :
178
174
res = f (* args , ** kwargs )
179
175
except Exception as e :
180
- _capture_exception (Hub . current , e )
176
+ _capture_exception (e )
181
177
span .__exit__ (None , None , None )
182
178
raise e from None
183
179
180
+ integration = sentry_sdk .get_client ().get_integration (OpenAIIntegration )
181
+
184
182
with capture_internal_exceptions ():
185
- if _should_send_default_pii () and integration .include_prompts :
183
+ if should_send_default_pii () and integration .include_prompts :
186
184
set_data_normalized (span , "ai.input_messages" , messages )
187
185
188
186
set_data_normalized (span , "ai.model_id" , model )
189
187
set_data_normalized (span , "ai.streaming" , streaming )
190
188
191
189
if hasattr (res , "choices" ):
192
- if _should_send_default_pii () and integration .include_prompts :
190
+ if should_send_default_pii () and integration .include_prompts :
193
191
set_data_normalized (
194
192
span ,
195
193
"ai.responses" ,
@@ -223,7 +221,7 @@ def new_iterator():
223
221
map (lambda chunk : "" .join (chunk ), data_buf )
224
222
)
225
223
if (
226
- _should_send_default_pii ()
224
+ should_send_default_pii ()
227
225
and integration .include_prompts
228
226
):
229
227
set_data_normalized (span , "ai.responses" , all_responses )
@@ -245,23 +243,16 @@ def _wrap_embeddings_create(f):
245
243
# type: (Callable[..., Any]) -> Callable[..., Any]
246
244
247
245
@wraps (f )
246
+ @ensure_integration_enabled (OpenAIIntegration , f )
248
247
def new_embeddings_create (* args , ** kwargs ):
249
248
# type: (*Any, **Any) -> Any
250
-
251
- hub = Hub .current
252
- if not hub :
253
- return f (* args , ** kwargs )
254
-
255
- integration = hub .get_integration (OpenAIIntegration ) # type: OpenAIIntegration
256
- if not integration :
257
- return f (* args , ** kwargs )
258
-
259
249
with sentry_sdk .start_span (
260
250
op = consts .OP .OPENAI_EMBEDDINGS_CREATE ,
261
251
description = "OpenAI Embedding Creation" ,
262
252
) as span :
253
+ integration = sentry_sdk .get_client ().get_integration (OpenAIIntegration )
263
254
if "input" in kwargs and (
264
- _should_send_default_pii () and integration .include_prompts
255
+ should_send_default_pii () and integration .include_prompts
265
256
):
266
257
if isinstance (kwargs ["input" ], str ):
267
258
set_data_normalized (span , "ai.input_messages" , [kwargs ["input" ]])
@@ -276,7 +267,7 @@ def new_embeddings_create(*args, **kwargs):
276
267
try :
277
268
response = f (* args , ** kwargs )
278
269
except Exception as e :
279
- _capture_exception (Hub . current , e )
270
+ _capture_exception (e )
280
271
raise e from None
281
272
282
273
prompt_tokens = 0
0 commit comments