88 from sentry_sdk .tracing import Span
99
1010import sentry_sdk
11- from sentry_sdk .hub import Hub , _should_send_default_pii
11+ from sentry_sdk .scope import should_send_default_pii
1212from 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+ )
1419
1520try :
1621 from openai .resources .chat .completions import Completions
@@ -62,16 +67,14 @@ def setup_once():
6267 Embeddings .create = _wrap_embeddings_create (Embeddings .create )
6368
6469
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 )
7578
7679
7780def _normalize_data (data ):
@@ -145,16 +148,9 @@ def _calculate_chat_completion_usage(
145148def _wrap_chat_completion_create (f ):
146149 # type: (Callable[..., Any]) -> Callable[..., Any]
147150 @wraps (f )
151+ @ensure_integration_enabled (OpenAIIntegration , f )
148152 def new_chat_completion (* args , ** kwargs ):
149153 # 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-
158154 if "messages" not in kwargs :
159155 # invalid call (in all versions of openai), let it return error
160156 return f (* args , ** kwargs )
@@ -177,19 +173,21 @@ def new_chat_completion(*args, **kwargs):
177173 try :
178174 res = f (* args , ** kwargs )
179175 except Exception as e :
180- _capture_exception (Hub . current , e )
176+ _capture_exception (e )
181177 span .__exit__ (None , None , None )
182178 raise e from None
183179
180+ integration = sentry_sdk .get_client ().get_integration (OpenAIIntegration )
181+
184182 with capture_internal_exceptions ():
185- if _should_send_default_pii () and integration .include_prompts :
183+ if should_send_default_pii () and integration .include_prompts :
186184 set_data_normalized (span , "ai.input_messages" , messages )
187185
188186 set_data_normalized (span , "ai.model_id" , model )
189187 set_data_normalized (span , "ai.streaming" , streaming )
190188
191189 if hasattr (res , "choices" ):
192- if _should_send_default_pii () and integration .include_prompts :
190+ if should_send_default_pii () and integration .include_prompts :
193191 set_data_normalized (
194192 span ,
195193 "ai.responses" ,
@@ -223,7 +221,7 @@ def new_iterator():
223221 map (lambda chunk : "" .join (chunk ), data_buf )
224222 )
225223 if (
226- _should_send_default_pii ()
224+ should_send_default_pii ()
227225 and integration .include_prompts
228226 ):
229227 set_data_normalized (span , "ai.responses" , all_responses )
@@ -245,23 +243,16 @@ def _wrap_embeddings_create(f):
245243 # type: (Callable[..., Any]) -> Callable[..., Any]
246244
247245 @wraps (f )
246+ @ensure_integration_enabled (OpenAIIntegration , f )
248247 def new_embeddings_create (* args , ** kwargs ):
249248 # 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-
259249 with sentry_sdk .start_span (
260250 op = consts .OP .OPENAI_EMBEDDINGS_CREATE ,
261251 description = "OpenAI Embedding Creation" ,
262252 ) as span :
253+ integration = sentry_sdk .get_client ().get_integration (OpenAIIntegration )
263254 if "input" in kwargs and (
264- _should_send_default_pii () and integration .include_prompts
255+ should_send_default_pii () and integration .include_prompts
265256 ):
266257 if isinstance (kwargs ["input" ], str ):
267258 set_data_normalized (span , "ai.input_messages" , [kwargs ["input" ]])
@@ -276,7 +267,7 @@ def new_embeddings_create(*args, **kwargs):
276267 try :
277268 response = f (* args , ** kwargs )
278269 except Exception as e :
279- _capture_exception (Hub . current , e )
270+ _capture_exception (e )
280271 raise e from None
281272
282273 prompt_tokens = 0
0 commit comments