Skip to content

Commit c4ab8cc

Browse files
wip: Adapt AI Agent Monitoring for potel
1 parent f0ad7ac commit c4ab8cc

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

sentry_sdk/integrations/openai_agents/spans/invoke_agent.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,14 @@ def invoke_agent_span(context, agent):
2424

2525
_set_agent_data(span, agent)
2626

27+
context._sentry_invoke_agent_span = span
28+
2729
return span
2830

2931

3032
def update_invoke_agent_span(context, agent, output):
3133
# type: (agents.RunContextWrapper, agents.Agent, Any) -> None
32-
current_span = sentry_sdk.get_current_span()
33-
if current_span:
34-
current_span.__exit__(None, None, None)
34+
span = getattr(context, "_sentry_invoke_agent_span", None)
35+
if span is not None:
36+
span.__exit__(None, None, None)
37+
del context._sentry_invoke_agent_span

tests/integrations/openai_agents/test_openai_agents.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,10 @@ async def test_handoff_span(sentry_init, capture_events, mock_usage):
263263

264264
(transaction,) = events
265265
spans = transaction["spans"]
266-
handoff_span = spans[2]
267266

268-
# Verify handoff span was created
269-
assert handoff_span is not None
267+
# There should be exactly one handoff span
268+
(handoff_span,) = [span for span in spans if span["op"] == "gen_ai.handoff"]
269+
270270
assert (
271271
handoff_span["description"] == "handoff from primary_agent to secondary_agent"
272272
)
@@ -354,12 +354,25 @@ def simple_test_tool(message: str) -> str:
354354

355355
(transaction,) = events
356356
spans = transaction["spans"]
357-
(
358-
agent_span,
359-
ai_client_span1,
360-
tool_span,
361-
ai_client_span2,
362-
) = spans
357+
358+
assert len(spans) == 4
359+
360+
# Find each span by its characteristics
361+
agent_span = next(s for s in spans if s["description"] == "invoke_agent test_agent")
362+
tool_span = next(
363+
s for s in spans if s["description"] == "execute_tool simple_test_tool"
364+
)
365+
ai_client_span1 = next(
366+
s
367+
for s in spans
368+
if s["description"] == "chat gpt-4"
369+
and "gen_ai.response.tool_calls" in s["data"]
370+
)
371+
ai_client_span2 = next(
372+
s
373+
for s in spans
374+
if s["description"] == "chat gpt-4" and "gen_ai.response.text" in s["data"]
375+
)
363376

364377
available_tools = safe_serialize(
365378
[
@@ -383,7 +396,6 @@ def simple_test_tool(message: str) -> str:
383396
assert transaction["transaction"] == "test_agent workflow"
384397
assert transaction["contexts"]["trace"]["origin"] == "auto.ai.openai_agents"
385398

386-
assert agent_span["description"] == "invoke_agent test_agent"
387399
assert agent_span["origin"] == "auto.ai.openai_agents"
388400
assert agent_span["data"]["gen_ai.agent.name"] == "test_agent"
389401
assert agent_span["data"]["gen_ai.operation.name"] == "invoke_agent"
@@ -394,7 +406,6 @@ def simple_test_tool(message: str) -> str:
394406
assert agent_span["data"]["gen_ai.request.top_p"] == 1.0
395407
assert agent_span["data"]["gen_ai.system"] == "openai"
396408

397-
assert ai_client_span1["description"] == "chat gpt-4"
398409
assert ai_client_span1["data"]["gen_ai.operation.name"] == "chat"
399410
assert ai_client_span1["data"]["gen_ai.system"] == "openai"
400411
assert ai_client_span1["data"]["gen_ai.agent.name"] == "test_agent"
@@ -442,7 +453,6 @@ def simple_test_tool(message: str) -> str:
442453
]
443454
)
444455

445-
assert tool_span["description"] == "execute_tool simple_test_tool"
446456
assert tool_span["data"]["gen_ai.agent.name"] == "test_agent"
447457
assert tool_span["data"]["gen_ai.operation.name"] == "execute_tool"
448458
assert (
@@ -464,7 +474,6 @@ def simple_test_tool(message: str) -> str:
464474
assert tool_span["data"]["gen_ai.tool.output"] == "Tool executed with: hello"
465475
assert tool_span["data"]["gen_ai.tool.type"] == "function"
466476

467-
assert ai_client_span2["description"] == "chat gpt-4"
468477
assert ai_client_span2["data"]["gen_ai.agent.name"] == "test_agent"
469478
assert ai_client_span2["data"]["gen_ai.operation.name"] == "chat"
470479
assert (

0 commit comments

Comments
 (0)