1- from functools import wraps
2-
31import sentry_sdk
42from sentry_sdk .consts import SPANDATA
53from sentry_sdk .integrations import DidNotEnable
1513
1614try :
1715 import agents
18- from agents import RunHooks
1916
2017except ImportError :
2118 raise DidNotEnable ("OpenAI Agents not installed" )
@@ -40,52 +37,6 @@ def _get_start_span_function():
4037 return sentry_sdk .start_span if is_transaction else sentry_sdk .start_transaction
4138
4239
43- def _create_hook_wrapper (original_hook , sentry_hook ):
44- # type: (Callable[..., Any], Callable[..., Any]) -> Callable[..., Any]
45- @wraps (original_hook )
46- async def async_wrapper (* args , ** kwargs ):
47- # type: (*Any, **Any) -> Any
48- await sentry_hook (* args , ** kwargs )
49- return await original_hook (* args , ** kwargs )
50-
51- return async_wrapper
52-
53-
54- def _wrap_hooks (hooks ):
55- # type: (RunHooks) -> RunHooks
56- """
57- Our integration uses RunHooks to create spans. This function will either
58- enable our SentryRunHooks or if the users has given custom RunHooks wrap
59- them so the Sentry hooks and the users hooks are both called
60- """
61- from .run_hooks import SentryRunHooks
62-
63- sentry_hooks = SentryRunHooks ()
64-
65- if hooks is None :
66- return sentry_hooks
67-
68- wrapped_hooks = type ("SentryWrappedHooks" , (hooks .__class__ ,), {})
69-
70- # Wrap all methods from RunHooks
71- for method_name in dir (RunHooks ):
72- if method_name .startswith ("on_" ):
73- original_method = getattr (hooks , method_name )
74- # Only wrap if the method exists in SentryRunHooks
75- try :
76- sentry_method = getattr (sentry_hooks , method_name )
77- setattr (
78- wrapped_hooks ,
79- method_name ,
80- _create_hook_wrapper (original_method , sentry_method ),
81- )
82- except AttributeError :
83- # If method doesn't exist in SentryRunHooks, just use the original method
84- setattr (wrapped_hooks , method_name , original_method )
85-
86- return wrapped_hooks ()
87-
88-
8940def _set_agent_data (span , agent ):
9041 # type: (sentry_sdk.tracing.Span, agents.Agent) -> None
9142 span .set_data (
0 commit comments