-
Notifications
You must be signed in to change notification settings - Fork 644
feat(streaming): Support ai_track decorator #6895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| from sentry_sdk.consts import SPANDATA | ||
| from sentry_sdk.traces import StreamedSpan | ||
| from sentry_sdk.tracing import Span | ||
| from sentry_sdk.tracing_utils import has_span_streaming_enabled | ||
| from sentry_sdk.utils import ContextVar, capture_internal_exceptions, reraise | ||
|
|
||
| if TYPE_CHECKING: | ||
|
|
@@ -30,64 +31,141 @@ def get_ai_pipeline_name() -> "Optional[str]": | |
| def ai_track(description: str, **span_kwargs: "Any") -> "Callable[[F], F]": | ||
| def decorator(f: "F") -> "F": | ||
| def sync_wrapped(*args: "Any", **kwargs: "Any") -> "Any": | ||
| client = sentry_sdk.get_client() | ||
|
|
||
| curr_pipeline = _ai_pipeline_name.get() | ||
| op = span_kwargs.pop("op", "ai.run" if curr_pipeline else "ai.pipeline") | ||
|
|
||
| with start_span(name=description, op=op, **span_kwargs) as span: | ||
| for k, v in kwargs.pop("sentry_tags", {}).items(): | ||
| span.set_tag(k, v) | ||
| for k, v in kwargs.pop("sentry_data", {}).items(): | ||
| span.set_data(k, v) | ||
| if curr_pipeline: | ||
| span.set_data(SPANDATA.GEN_AI_PIPELINE_NAME, curr_pipeline) | ||
| return f(*args, **kwargs) | ||
| else: | ||
| _ai_pipeline_name.set(description) | ||
| try: | ||
| res = f(*args, **kwargs) | ||
| except Exception as e: | ||
| exc_info = sys.exc_info() | ||
| with capture_internal_exceptions(): | ||
| event, hint = sentry_sdk.utils.event_from_exception( | ||
| e, | ||
| client_options=sentry_sdk.get_client().options, | ||
| mechanism={"type": "ai_monitoring", "handled": False}, | ||
| ) | ||
| sentry_sdk.capture_event(event, hint=hint) | ||
| reraise(*exc_info) | ||
| finally: | ||
| _ai_pipeline_name.set(None) | ||
| return res | ||
| if has_span_streaming_enabled(client.options): | ||
| with sentry_sdk.traces.start_span( | ||
| name=description, attributes={"sentry.op": op} | ||
| ) as span: | ||
| for k, v in kwargs.pop("sentry_tags", {}).items(): | ||
| span.set_attribute(k, v) | ||
| for k, v in kwargs.pop("sentry_data", {}).items(): | ||
| span.set_attribute(k, v) | ||
|
|
||
| if curr_pipeline: | ||
|
Comment on lines
+45
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: In streaming mode, extra keyword arguments passed to the Suggested FixCreate a copy of the Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct, intentional |
||
| span.set_attribute(SPANDATA.GEN_AI_PIPELINE_NAME, curr_pipeline) | ||
| return f(*args, **kwargs) | ||
| else: | ||
| _ai_pipeline_name.set(description) | ||
| try: | ||
| res = f(*args, **kwargs) | ||
| except Exception as e: | ||
| exc_info = sys.exc_info() | ||
| with capture_internal_exceptions(): | ||
| event, hint = sentry_sdk.utils.event_from_exception( | ||
| e, | ||
| client_options=sentry_sdk.get_client().options, | ||
| mechanism={ | ||
| "type": "ai_monitoring", | ||
| "handled": False, | ||
| }, | ||
| ) | ||
| sentry_sdk.capture_event(event, hint=hint) | ||
| reraise(*exc_info) | ||
| finally: | ||
| _ai_pipeline_name.set(None) | ||
| return res | ||
|
|
||
| else: | ||
| with start_span(name=description, op=op, **span_kwargs) as span: | ||
| for k, v in kwargs.pop("sentry_tags", {}).items(): | ||
| span.set_tag(k, v) | ||
| for k, v in kwargs.pop("sentry_data", {}).items(): | ||
| span.set_data(k, v) | ||
| if curr_pipeline: | ||
| span.set_data(SPANDATA.GEN_AI_PIPELINE_NAME, curr_pipeline) | ||
| return f(*args, **kwargs) | ||
| else: | ||
| _ai_pipeline_name.set(description) | ||
| try: | ||
| res = f(*args, **kwargs) | ||
| except Exception as e: | ||
| exc_info = sys.exc_info() | ||
| with capture_internal_exceptions(): | ||
| event, hint = sentry_sdk.utils.event_from_exception( | ||
| e, | ||
| client_options=sentry_sdk.get_client().options, | ||
| mechanism={ | ||
| "type": "ai_monitoring", | ||
| "handled": False, | ||
| }, | ||
| ) | ||
| sentry_sdk.capture_event(event, hint=hint) | ||
| reraise(*exc_info) | ||
| finally: | ||
| _ai_pipeline_name.set(None) | ||
| return res | ||
|
|
||
| async def async_wrapped(*args: "Any", **kwargs: "Any") -> "Any": | ||
| client = sentry_sdk.get_client() | ||
|
|
||
| curr_pipeline = _ai_pipeline_name.get() | ||
| op = span_kwargs.pop("op", "ai.run" if curr_pipeline else "ai.pipeline") | ||
|
|
||
| with start_span(name=description, op=op, **span_kwargs) as span: | ||
| for k, v in kwargs.pop("sentry_tags", {}).items(): | ||
| span.set_tag(k, v) | ||
| for k, v in kwargs.pop("sentry_data", {}).items(): | ||
| span.set_data(k, v) | ||
| if curr_pipeline: | ||
| span.set_data(SPANDATA.GEN_AI_PIPELINE_NAME, curr_pipeline) | ||
| return await f(*args, **kwargs) | ||
| else: | ||
| _ai_pipeline_name.set(description) | ||
| try: | ||
| res = await f(*args, **kwargs) | ||
| except Exception as e: | ||
| exc_info = sys.exc_info() | ||
| with capture_internal_exceptions(): | ||
| event, hint = sentry_sdk.utils.event_from_exception( | ||
| e, | ||
| client_options=sentry_sdk.get_client().options, | ||
| mechanism={"type": "ai_monitoring", "handled": False}, | ||
| ) | ||
| sentry_sdk.capture_event(event, hint=hint) | ||
| reraise(*exc_info) | ||
| finally: | ||
| _ai_pipeline_name.set(None) | ||
| return res | ||
| if has_span_streaming_enabled(client.options): | ||
| with sentry_sdk.traces.start_span( | ||
| name=description, attributes={"sentry.op": op} | ||
| ) as span: | ||
| for k, v in kwargs.pop("sentry_tags", {}).items(): | ||
| span.set_attribute(k, v) | ||
| for k, v in kwargs.pop("sentry_data", {}).items(): | ||
| span.set_attribute(k, v) | ||
|
|
||
| if curr_pipeline: | ||
| span.set_attribute(SPANDATA.GEN_AI_PIPELINE_NAME, curr_pipeline) | ||
| return await f(*args, **kwargs) | ||
| else: | ||
| _ai_pipeline_name.set(description) | ||
| try: | ||
| res = await f(*args, **kwargs) | ||
| except Exception as e: | ||
| exc_info = sys.exc_info() | ||
| with capture_internal_exceptions(): | ||
| event, hint = sentry_sdk.utils.event_from_exception( | ||
| e, | ||
| client_options=sentry_sdk.get_client().options, | ||
| mechanism={ | ||
| "type": "ai_monitoring", | ||
| "handled": False, | ||
| }, | ||
| ) | ||
| sentry_sdk.capture_event(event, hint=hint) | ||
| reraise(*exc_info) | ||
| finally: | ||
| _ai_pipeline_name.set(None) | ||
| return res | ||
| else: | ||
| with start_span(name=description, op=op, **span_kwargs) as span: | ||
| for k, v in kwargs.pop("sentry_tags", {}).items(): | ||
| span.set_tag(k, v) | ||
| for k, v in kwargs.pop("sentry_data", {}).items(): | ||
| span.set_data(k, v) | ||
| if curr_pipeline: | ||
| span.set_data(SPANDATA.GEN_AI_PIPELINE_NAME, curr_pipeline) | ||
| return await f(*args, **kwargs) | ||
| else: | ||
| _ai_pipeline_name.set(description) | ||
| try: | ||
| res = await f(*args, **kwargs) | ||
| except Exception as e: | ||
| exc_info = sys.exc_info() | ||
| with capture_internal_exceptions(): | ||
| event, hint = sentry_sdk.utils.event_from_exception( | ||
| e, | ||
| client_options=sentry_sdk.get_client().options, | ||
| mechanism={ | ||
| "type": "ai_monitoring", | ||
| "handled": False, | ||
| }, | ||
| ) | ||
| sentry_sdk.capture_event(event, hint=hint) | ||
| reraise(*exc_info) | ||
| finally: | ||
| _ai_pipeline_name.set(None) | ||
| return res | ||
|
|
||
| if inspect.iscoroutinefunction(f): | ||
| return wraps(f)(async_wrapped) # type: ignore | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: Using
span_kwargs.pop("op")mutates the sharedspan_kwargsdictionary, causing subsequent calls to the decorated function to lose the customopand use a default value.Severity: MEDIUM
Suggested Fix
Instead of mutating
span_kwargswith.pop(), use.get()to retrieve theopvalue. To prevent passingopalong with other keyword arguments, create a copy of the dictionary for thestart_spancall, excluding theopkey. For example:op = span_kwargs.get("op", ...)andother_kwargs = {k: v for k, v in span_kwargs.items() if k != 'op'}.Prompt for AI Agent
Did we get this right? 👍 / 👎 to inform future reviews.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pre-existing issue