getsentry / getsentry/sentry-python

OpenAI Agents integration breaks parent-child span relationships due to isolation_scope usage

Đang mở
#4,786 6 bình luận 0 reaction 1 người được giao Được @alexander-alderman-webb nhận Xem trên GitHub
Improvement Integration: OpenAI Agents Python Spans
Ngôn ngữ chính
Python
Star
2.2k
Fork
669
Merge trung bình
1 ngày 1 giờ
Pull request đã merge (30 ngày)
213

Mô tả

_(Full disclosure, the issue text & root cause analysis were done by Claude. The problem is very much a problem we're running into atm tho)_

## Problem

The OpenAI Agents integration in Sentry SDK creates separate traces instead of proper parent-child span relationships when agents are called from within existing trace contexts. This results in:

1. **Trace fragmentation**: Long "holes" appear in traces when agents are executing
2. **Lost context**: Agent spans don't appear as children of the calling span
3. **Poor observability**: Agent-to-agent (through `agent.as_tool()` create completely separate traces instead of nested spans

## Root Cause

The issue is in `sentry_sdk/integrations/openai_agents/patches/runner.py:28` where `sentry_sdk.isolation_scope()` is used:

```python
# Isolate each workflow so that when agents are run in asyncio tasks they
# don't touch each other's scopes
with sentry_sdk.isolation_scope():
agent = args[0]
with agent_workflow_span(agent):
# ... agent execution
```

The `isolation_scope()` creates a completely separate tracing context that breaks the connection to any existing parent span, even when that parent-child relationship is desired.

## Proposed Solution

Replace `sentry_sdk.isolation_scope()` with `sentry_sdk.new_scope()` on line 28:

```python
# Create new scope for local span management while preserving parent context
with sentry_sdk.new_scope():
agent = args[0]
with agent_workflow_span(agent):
# ... agent execution
```

This change would:

✅ **Preserve parent-child relationships**: Agent spans would properly nest under calling spans
✅ **Maintain concurrent isolation**: Different concurrent agent executions would still be isolated from each other
✅ **Keep existing functionality**: All current tests should continue to pass
✅ **Improve observability**: Traces would show the complete flow including agent work

## Impact

- **Current behavior**: Agent execution creates separate traces
- **Expected behavior**: Agent execution creates child spans within the existing trace
- **Backward compatibility**: This change should not break existing functionality

The concurrent agent execution test (line 673 in tests) should still pass because `new_scope()` provides sufficient isolation between parallel executions while allowing proper parent-child relationships.

## Files Affected

- `sentry_sdk/integrations/openai_agents/patches/runner.py` (line 28)

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Đánh giá

Issue này chưa được đánh giá.

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.