-
Notifications
You must be signed in to change notification settings - Fork 568
fix(integrations): ensure that GEN_AI_AGENT_NAME is properly set for GEN_AI spans under an invoke_agent span #5030
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
Open
constantinius
wants to merge
2
commits into
master
Choose a base branch
from
constantinius/fix/integrations/ensure-genai-agent-name
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -276,6 +276,14 @@ def on_chat_model_start(self, serialized, messages, *, run_id, **kwargs): | |
| elif "openai" in ai_type: | ||
| span.set_data(SPANDATA.GEN_AI_SYSTEM, "openai") | ||
|
|
||
| agent_name = ( | ||
| sentry_sdk.get_current_scope() | ||
| ._contexts.get("langchain_agent", {}) | ||
| .get("agent_name") | ||
| ) | ||
| if agent_name: | ||
| span.set_data(SPANDATA.GEN_AI_AGENT_NAME, agent_name) | ||
|
|
||
| for key, attribute in DATA_FIELDS.items(): | ||
| if key in all_params and all_params[key] is not None: | ||
| set_data_normalized(span, attribute, all_params[key], unpack=False) | ||
|
|
@@ -428,6 +436,14 @@ def on_tool_start(self, serialized, input_str, *, run_id, **kwargs): | |
| if tool_description is not None: | ||
| span.set_data(SPANDATA.GEN_AI_TOOL_DESCRIPTION, tool_description) | ||
|
|
||
| agent_name = ( | ||
| sentry_sdk.get_current_scope() | ||
| ._contexts.get("langchain_agent", {}) | ||
| .get("agent_name") | ||
| ) | ||
| if agent_name: | ||
| span.set_data(SPANDATA.GEN_AI_AGENT_NAME, agent_name) | ||
|
|
||
| if should_send_default_pii() and self.include_prompts: | ||
| set_data_normalized( | ||
| span, | ||
|
|
@@ -756,6 +772,9 @@ def new_invoke(self, *args, **kwargs): | |
| name=f"invoke_agent {agent_name}" if agent_name else "invoke_agent", | ||
| origin=LangchainIntegration.origin, | ||
| ) as span: | ||
| sentry_sdk.get_current_scope().set_context( | ||
| "langchain_agent", {"agent_name": agent_name, "tools": tools} | ||
| ) | ||
| if agent_name: | ||
| span.set_data(SPANDATA.GEN_AI_AGENT_NAME, agent_name) | ||
|
|
||
|
|
@@ -794,6 +813,8 @@ def new_invoke(self, *args, **kwargs): | |
| ): | ||
| set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, output) | ||
|
|
||
| sentry_sdk.get_current_scope().remove_context("langchain_agent") | ||
|
|
||
| return result | ||
|
|
||
| return new_invoke | ||
|
|
@@ -814,11 +835,15 @@ def new_stream(self, *args, **kwargs): | |
|
|
||
| span = start_span_function( | ||
| op=OP.GEN_AI_INVOKE_AGENT, | ||
| name=f"invoke_agent {agent_name}".strip(), | ||
| name=f"invoke_agent {agent_name}" if agent_name else "invoke_agent", | ||
| origin=LangchainIntegration.origin, | ||
| ) | ||
| span.__enter__() | ||
|
|
||
| sentry_sdk.get_current_scope().set_context( | ||
| "langchain_agent", {"agent_name": agent_name, "tools": tools} | ||
| ) | ||
|
|
||
| if agent_name: | ||
| span.set_data(SPANDATA.GEN_AI_AGENT_NAME, agent_name) | ||
|
|
||
|
|
@@ -868,6 +893,8 @@ def new_iterator(): | |
| ): | ||
| set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, output) | ||
|
|
||
| sentry_sdk.get_current_scope().remove_context("langchain_agent") | ||
|
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: Context Leak in Exception HandlingContext leak in _wrap_agent_executor_stream: if f(self, *args, **kwargs) throws an exception after the context is set (line 843-845), the iterators containing the remove_context calls (lines 896, 917) are never created, causing the "langchain_agent" context to leak permanently. |
||
|
|
||
| span.__exit__(None, None, None) | ||
|
|
||
| async def new_iterator_async(): | ||
|
|
@@ -887,6 +914,8 @@ async def new_iterator_async(): | |
| ): | ||
| set_data_normalized(span, SPANDATA.GEN_AI_RESPONSE_TEXT, output) | ||
|
|
||
| sentry_sdk.get_current_scope().remove_context("langchain_agent") | ||
|
|
||
| span.__exit__(None, None, None) | ||
|
|
||
| if str(type(result)) == "<class 'async_generator'>": | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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: Context Leak in Exception Handling
Context leak in _wrap_agent_executor_invoke: if f(self, *args, **kwargs) throws an exception after the context is set (line 775-777), the remove_context call at line 816 will never execute, causing the "langchain_agent" context to persist beyond its intended scope.