|
| 1 | +from functools import wraps |
| 2 | + |
| 3 | +from sentry_sdk.integrations import DidNotEnable |
| 4 | + |
| 5 | +from ..spans import invoke_agent_span, update_invoke_agent_span |
| 6 | + |
| 7 | + |
| 8 | +try: |
| 9 | + import agents |
| 10 | +except ImportError: |
| 11 | + raise DidNotEnable("OpenAI Agents not installed") |
| 12 | + |
| 13 | + |
| 14 | +def _patch_agent_run(): |
| 15 | + """ |
| 16 | + Patches AgentRunner methods to create agent invocation spans without using RunHooks. |
| 17 | + This directly patches the execution flow to track when agents start and stop. |
| 18 | + """ |
| 19 | + |
| 20 | + # Store original methods |
| 21 | + original_run_single_turn = agents.run.AgentRunner._run_single_turn |
| 22 | + original_execute_handoffs = agents._run_impl.RunImpl.execute_handoffs |
| 23 | + original_execute_final_output = agents._run_impl.RunImpl.execute_final_output |
| 24 | + |
| 25 | + def _start_invoke_agent_span(context_wrapper, agent): |
| 26 | + """Start an agent invocation span""" |
| 27 | + # Store the agent on the context wrapper so we can access it later |
| 28 | + context_wrapper._sentry_current_agent = agent |
| 29 | + invoke_agent_span(context_wrapper, agent) |
| 30 | + |
| 31 | + def _end_invoke_agent_span(context_wrapper, agent, output): |
| 32 | + """End the agent invocation span""" |
| 33 | + # Clear the stored agent |
| 34 | + if hasattr(context_wrapper, "_sentry_current_agent"): |
| 35 | + delattr(context_wrapper, "_sentry_current_agent") |
| 36 | + |
| 37 | + update_invoke_agent_span(context_wrapper, agent, output) |
| 38 | + |
| 39 | + def _has_active_agent_span(context_wrapper): |
| 40 | + """Check if there's an active agent span for this context""" |
| 41 | + return hasattr(context_wrapper, "_sentry_current_agent") |
| 42 | + |
| 43 | + def _get_current_agent(context_wrapper): |
| 44 | + """Get the current agent from context wrapper""" |
| 45 | + return getattr(context_wrapper, "_sentry_current_agent", None) |
| 46 | + |
| 47 | + @wraps(original_run_single_turn) |
| 48 | + async def patched_run_single_turn( |
| 49 | + cls, |
| 50 | + *, |
| 51 | + agent, |
| 52 | + all_tools, |
| 53 | + original_input, |
| 54 | + generated_items, |
| 55 | + hooks, |
| 56 | + context_wrapper, |
| 57 | + run_config, |
| 58 | + should_run_agent_start_hooks, |
| 59 | + tool_use_tracker, |
| 60 | + previous_response_id, |
| 61 | + ): |
| 62 | + """Patched _run_single_turn that creates agent invocation spans""" |
| 63 | + |
| 64 | + # Start agent span when agent starts (but only once per agent) |
| 65 | + if should_run_agent_start_hooks and agent and context_wrapper: |
| 66 | + # End any existing span for a different agent |
| 67 | + if _has_active_agent_span(context_wrapper): |
| 68 | + current_agent = _get_current_agent(context_wrapper) |
| 69 | + if current_agent and current_agent != agent: |
| 70 | + _end_invoke_agent_span(context_wrapper, current_agent, None) |
| 71 | + |
| 72 | + _start_invoke_agent_span(context_wrapper, agent) |
| 73 | + |
| 74 | + # Call original method with all the correct parameters |
| 75 | + result = await original_run_single_turn( |
| 76 | + agent=agent, |
| 77 | + all_tools=all_tools, |
| 78 | + original_input=original_input, |
| 79 | + generated_items=generated_items, |
| 80 | + hooks=hooks, |
| 81 | + context_wrapper=context_wrapper, |
| 82 | + run_config=run_config, |
| 83 | + should_run_agent_start_hooks=should_run_agent_start_hooks, |
| 84 | + tool_use_tracker=tool_use_tracker, |
| 85 | + previous_response_id=previous_response_id, |
| 86 | + ) |
| 87 | + |
| 88 | + return result |
| 89 | + |
| 90 | + @wraps(original_execute_handoffs) |
| 91 | + async def patched_execute_handoffs( |
| 92 | + cls, |
| 93 | + *, |
| 94 | + agent, |
| 95 | + original_input, |
| 96 | + pre_step_items, |
| 97 | + new_step_items, |
| 98 | + new_response, |
| 99 | + run_handoffs, |
| 100 | + hooks, |
| 101 | + context_wrapper, |
| 102 | + run_config, |
| 103 | + ): |
| 104 | + """Patched execute_handoffs that ends agent span for handoffs""" |
| 105 | + |
| 106 | + # Call original method with all parameters |
| 107 | + result = await original_execute_handoffs( |
| 108 | + agent=agent, |
| 109 | + original_input=original_input, |
| 110 | + pre_step_items=pre_step_items, |
| 111 | + new_step_items=new_step_items, |
| 112 | + new_response=new_response, |
| 113 | + run_handoffs=run_handoffs, |
| 114 | + hooks=hooks, |
| 115 | + context_wrapper=context_wrapper, |
| 116 | + run_config=run_config, |
| 117 | + ) |
| 118 | + |
| 119 | + # End span for current agent after handoff processing is complete |
| 120 | + if agent and context_wrapper and _has_active_agent_span(context_wrapper): |
| 121 | + _end_invoke_agent_span(context_wrapper, agent, None) |
| 122 | + |
| 123 | + return result |
| 124 | + |
| 125 | + @wraps(original_execute_final_output) |
| 126 | + async def patched_execute_final_output( |
| 127 | + cls, |
| 128 | + *, |
| 129 | + agent, |
| 130 | + original_input, |
| 131 | + new_response, |
| 132 | + pre_step_items, |
| 133 | + new_step_items, |
| 134 | + final_output, |
| 135 | + hooks, |
| 136 | + context_wrapper, |
| 137 | + ): |
| 138 | + """Patched execute_final_output that ends agent span for final outputs""" |
| 139 | + |
| 140 | + # Call original method with all parameters |
| 141 | + result = await original_execute_final_output( |
| 142 | + agent=agent, |
| 143 | + original_input=original_input, |
| 144 | + new_response=new_response, |
| 145 | + pre_step_items=pre_step_items, |
| 146 | + new_step_items=new_step_items, |
| 147 | + final_output=final_output, |
| 148 | + hooks=hooks, |
| 149 | + context_wrapper=context_wrapper, |
| 150 | + ) |
| 151 | + |
| 152 | + # End span for current agent after final output processing is complete |
| 153 | + if agent and context_wrapper and _has_active_agent_span(context_wrapper): |
| 154 | + _end_invoke_agent_span(context_wrapper, agent, final_output) |
| 155 | + |
| 156 | + return result |
| 157 | + |
| 158 | + # Apply patches |
| 159 | + agents.run.AgentRunner._run_single_turn = classmethod(patched_run_single_turn) |
| 160 | + agents._run_impl.RunImpl.execute_handoffs = classmethod(patched_execute_handoffs) |
| 161 | + agents._run_impl.RunImpl.execute_final_output = classmethod( |
| 162 | + patched_execute_final_output |
| 163 | + ) |
0 commit comments