-
Notifications
You must be signed in to change notification settings - Fork 580
feat(integration): add gen_ai.conversation.id if available
#5307
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
base: master
Are you sure you want to change the base?
Changes from all commits
e5ad308
8884375
c446c35
ca2b9ba
e5d0ffa
5e0d558
5f73e4f
b09e718
635606f
fcd77a7
c67deeb
621766c
852402e
0f8c7ec
bc43364
2359f29
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 |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
| from functools import wraps | ||
|
|
||
| import sentry_sdk | ||
| from sentry_sdk.consts import SPANDATA | ||
| from sentry_sdk.integrations import DidNotEnable | ||
| from sentry_sdk.utils import capture_internal_exceptions, reraise | ||
|
|
||
|
|
@@ -34,7 +35,13 @@ async def wrapper(*args: "Any", **kwargs: "Any") -> "Any": | |
| with sentry_sdk.isolation_scope(): | ||
| # Clone agent because agent invocation spans are attached per run. | ||
| agent = args[0].clone() | ||
|
|
||
| with agent_workflow_span(agent): | ||
| # Set conversation ID on workflow span early so it's captured even on errors | ||
| conversation_id = kwargs.get("conversation_id") | ||
| if conversation_id: | ||
| agent._sentry_conversation_id = conversation_id | ||
|
Comment on lines
37
to
+43
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: The async wrapper Suggested FixModify the Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
|
|
||
|
Comment on lines
37
to
+44
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: The async wrapper for Suggested FixIn the async wrapper within Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
| args = (agent, *args[1:]) | ||
| try: | ||
| run_result = await original_func(*args, **kwargs) | ||
|
|
@@ -91,10 +98,19 @@ def wrapper(*args: "Any", **kwargs: "Any") -> "Any": | |
| # Clone agent because agent invocation spans are attached per run. | ||
| agent = args[0].clone() | ||
|
|
||
| # Capture conversation_id from kwargs if provided | ||
| conversation_id = kwargs.get("conversation_id") | ||
| if conversation_id: | ||
| agent._sentry_conversation_id = conversation_id | ||
|
|
||
| # Start workflow span immediately (before run_streamed returns) | ||
| workflow_span = agent_workflow_span(agent) | ||
| workflow_span.__enter__() | ||
|
|
||
| # Set conversation ID on workflow span early so it's captured even on errors | ||
| if conversation_id: | ||
| workflow_span.set_data(SPANDATA.GEN_AI_CONVERSATION_ID, conversation_id) | ||
|
|
||
| # Store span on agent for cleanup | ||
| agent._sentry_workflow_span = workflow_span | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,7 @@ def update_ai_client_span( | |
| span: "sentry_sdk.tracing.Span", | ||
| response: "Any", | ||
| response_model: "Optional[str]" = None, | ||
| agent: "Optional[Agent]" = None, | ||
| ) -> None: | ||
| """Update AI client span with response data (works for streaming and non-streaming).""" | ||
| if hasattr(response, "usage") and response.usage: | ||
|
|
@@ -59,3 +60,9 @@ def update_ai_client_span( | |
| span.set_data(SPANDATA.GEN_AI_RESPONSE_MODEL, response_model) | ||
| elif hasattr(response, "model") and response.model: | ||
| span.set_data(SPANDATA.GEN_AI_RESPONSE_MODEL, str(response.model)) | ||
|
|
||
| # Set conversation ID from agent if available | ||
| if agent: | ||
| conv_id = getattr(agent, "_sentry_conversation_id", None) | ||
| if conv_id: | ||
| span.set_data(SPANDATA.GEN_AI_CONVERSATION_ID, conv_id) | ||
|
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. Callers don't pass agent to update_ai_client_spanMedium Severity The |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,3 +51,8 @@ def update_execute_tool_span( | |
|
|
||
| if should_send_default_pii(): | ||
| span.set_data(SPANDATA.GEN_AI_TOOL_OUTPUT, result) | ||
|
|
||
| # Add conversation ID from agent | ||
| conv_id = getattr(agent, "_sentry_conversation_id", None) | ||
| if conv_id: | ||
| span.set_data(SPANDATA.GEN_AI_CONVERSATION_ID, conv_id) | ||
|
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. Duplicated pattern for setting conversation ID on spansLow Severity The same 3-line pattern for extracting and setting |
||


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.
Missing conversation_id on workflow span in async runner
Medium Severity
In
_create_run_wrapper, the comment says "Set conversation ID on workflow span early" but the code only storesconversation_idon the agent object (agent._sentry_conversation_id). Unlike the streaming version_create_run_streamed_wrapper, there's no call toset_data(SPANDATA.GEN_AI_CONVERSATION_ID, conversation_id)on the workflow span itself. The workflow span (transaction) will be missing thegen_ai.conversation.idfield when usingRunner.run().