Skip to content

Commit da92544

Browse files
committed
Add exception handling to AI monitoring
1 parent 9645835 commit da92544

File tree

3 files changed

+15
-4
lines changed

3 files changed

+15
-4
lines changed

sentry_sdk/ai_analytics.py renamed to sentry_sdk/ai_monitoring.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from functools import wraps
22

3+
import sentry_sdk.utils
34
from sentry_sdk import start_span
45
from sentry_sdk.tracing import Span
56
from sentry_sdk.utils import ContextVar
@@ -36,8 +37,18 @@ def wrapped(*args, **kwargs):
3637
return f(*args, **kwargs)
3738
else:
3839
_ai_pipeline_name.set(description)
39-
res = f(*args, **kwargs)
40-
_ai_pipeline_name.set(None)
40+
try:
41+
res = f(*args, **kwargs)
42+
except Exception as e:
43+
event, hint = sentry_sdk.utils.event_from_exception(
44+
e,
45+
client_options=sentry_sdk.get_client().options,
46+
mechanism={"type": "ai_monitoring", "handled": False},
47+
)
48+
sentry_sdk.capture_event(event, hint=hint)
49+
raise e from None
50+
finally:
51+
_ai_pipeline_name.set(None)
4152
return res
4253

4354
return wrapped

sentry_sdk/integrations/langchain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import sentry_sdk
55
from sentry_sdk._types import TYPE_CHECKING
6-
from sentry_sdk.ai_analytics import set_ai_pipeline_name, record_token_usage
6+
from sentry_sdk.ai_monitoring import set_ai_pipeline_name, record_token_usage
77
from sentry_sdk.consts import OP, SPANDATA
88
from sentry_sdk.integrations._ai_common import set_data_normalized
99
from sentry_sdk.scope import should_send_default_pii

sentry_sdk/integrations/openai.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from sentry_sdk import consts
44
from sentry_sdk._types import TYPE_CHECKING
5-
from sentry_sdk.ai_analytics import record_token_usage
5+
from sentry_sdk.ai_monitoring import record_token_usage
66
from sentry_sdk.consts import SPANDATA
77
from sentry_sdk.integrations._ai_common import set_data_normalized
88

0 commit comments

Comments
 (0)