mseep-agentops 0.4.18__py3-none-any.whl → 0.4.22__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- agentops/__init__.py +0 -0
- agentops/client/api/base.py +28 -30
- agentops/client/api/versions/v3.py +29 -25
- agentops/client/api/versions/v4.py +87 -46
- agentops/client/client.py +98 -29
- agentops/client/http/README.md +87 -0
- agentops/client/http/http_client.py +126 -172
- agentops/config.py +8 -2
- agentops/instrumentation/OpenTelemetry.md +133 -0
- agentops/instrumentation/README.md +167 -0
- agentops/instrumentation/__init__.py +13 -1
- agentops/instrumentation/agentic/ag2/__init__.py +18 -0
- agentops/instrumentation/agentic/ag2/instrumentor.py +922 -0
- agentops/instrumentation/agentic/agno/__init__.py +19 -0
- agentops/instrumentation/agentic/agno/attributes/__init__.py +20 -0
- agentops/instrumentation/agentic/agno/attributes/agent.py +250 -0
- agentops/instrumentation/agentic/agno/attributes/metrics.py +214 -0
- agentops/instrumentation/agentic/agno/attributes/storage.py +158 -0
- agentops/instrumentation/agentic/agno/attributes/team.py +195 -0
- agentops/instrumentation/agentic/agno/attributes/tool.py +210 -0
- agentops/instrumentation/agentic/agno/attributes/workflow.py +254 -0
- agentops/instrumentation/agentic/agno/instrumentor.py +1313 -0
- agentops/instrumentation/agentic/crewai/LICENSE +201 -0
- agentops/instrumentation/agentic/crewai/NOTICE.md +10 -0
- agentops/instrumentation/agentic/crewai/__init__.py +6 -0
- agentops/instrumentation/agentic/crewai/crewai_span_attributes.py +335 -0
- agentops/instrumentation/agentic/crewai/instrumentation.py +535 -0
- agentops/instrumentation/agentic/crewai/version.py +1 -0
- agentops/instrumentation/agentic/google_adk/__init__.py +19 -0
- agentops/instrumentation/agentic/google_adk/instrumentor.py +68 -0
- agentops/instrumentation/agentic/google_adk/patch.py +767 -0
- agentops/instrumentation/agentic/haystack/__init__.py +1 -0
- agentops/instrumentation/agentic/haystack/instrumentor.py +186 -0
- agentops/instrumentation/agentic/langgraph/__init__.py +3 -0
- agentops/instrumentation/agentic/langgraph/attributes.py +54 -0
- agentops/instrumentation/agentic/langgraph/instrumentation.py +598 -0
- agentops/instrumentation/agentic/langgraph/version.py +1 -0
- agentops/instrumentation/agentic/openai_agents/README.md +156 -0
- agentops/instrumentation/agentic/openai_agents/SPANS.md +145 -0
- agentops/instrumentation/agentic/openai_agents/TRACING_API.md +144 -0
- agentops/instrumentation/agentic/openai_agents/__init__.py +30 -0
- agentops/instrumentation/agentic/openai_agents/attributes/common.py +549 -0
- agentops/instrumentation/agentic/openai_agents/attributes/completion.py +172 -0
- agentops/instrumentation/agentic/openai_agents/attributes/model.py +58 -0
- agentops/instrumentation/agentic/openai_agents/attributes/tokens.py +275 -0
- agentops/instrumentation/agentic/openai_agents/exporter.py +469 -0
- agentops/instrumentation/agentic/openai_agents/instrumentor.py +107 -0
- agentops/instrumentation/agentic/openai_agents/processor.py +58 -0
- agentops/instrumentation/agentic/smolagents/README.md +88 -0
- agentops/instrumentation/agentic/smolagents/__init__.py +12 -0
- agentops/instrumentation/agentic/smolagents/attributes/agent.py +354 -0
- agentops/instrumentation/agentic/smolagents/attributes/model.py +205 -0
- agentops/instrumentation/agentic/smolagents/instrumentor.py +286 -0
- agentops/instrumentation/agentic/smolagents/stream_wrapper.py +258 -0
- agentops/instrumentation/agentic/xpander/__init__.py +15 -0
- agentops/instrumentation/agentic/xpander/context.py +112 -0
- agentops/instrumentation/agentic/xpander/instrumentor.py +877 -0
- agentops/instrumentation/agentic/xpander/trace_probe.py +86 -0
- agentops/instrumentation/agentic/xpander/version.py +3 -0
- agentops/instrumentation/common/README.md +65 -0
- agentops/instrumentation/common/attributes.py +1 -2
- agentops/instrumentation/providers/anthropic/__init__.py +24 -0
- agentops/instrumentation/providers/anthropic/attributes/__init__.py +23 -0
- agentops/instrumentation/providers/anthropic/attributes/common.py +64 -0
- agentops/instrumentation/providers/anthropic/attributes/message.py +541 -0
- agentops/instrumentation/providers/anthropic/attributes/tools.py +231 -0
- agentops/instrumentation/providers/anthropic/event_handler_wrapper.py +90 -0
- agentops/instrumentation/providers/anthropic/instrumentor.py +146 -0
- agentops/instrumentation/providers/anthropic/stream_wrapper.py +436 -0
- agentops/instrumentation/providers/google_genai/README.md +33 -0
- agentops/instrumentation/providers/google_genai/__init__.py +24 -0
- agentops/instrumentation/providers/google_genai/attributes/__init__.py +25 -0
- agentops/instrumentation/providers/google_genai/attributes/chat.py +125 -0
- agentops/instrumentation/providers/google_genai/attributes/common.py +88 -0
- agentops/instrumentation/providers/google_genai/attributes/model.py +284 -0
- agentops/instrumentation/providers/google_genai/instrumentor.py +170 -0
- agentops/instrumentation/providers/google_genai/stream_wrapper.py +238 -0
- agentops/instrumentation/providers/ibm_watsonx_ai/__init__.py +28 -0
- agentops/instrumentation/providers/ibm_watsonx_ai/attributes/__init__.py +27 -0
- agentops/instrumentation/providers/ibm_watsonx_ai/attributes/attributes.py +277 -0
- agentops/instrumentation/providers/ibm_watsonx_ai/attributes/common.py +104 -0
- agentops/instrumentation/providers/ibm_watsonx_ai/instrumentor.py +162 -0
- agentops/instrumentation/providers/ibm_watsonx_ai/stream_wrapper.py +302 -0
- agentops/instrumentation/providers/mem0/__init__.py +45 -0
- agentops/instrumentation/providers/mem0/common.py +377 -0
- agentops/instrumentation/providers/mem0/instrumentor.py +270 -0
- agentops/instrumentation/providers/mem0/memory.py +430 -0
- agentops/instrumentation/providers/openai/__init__.py +21 -0
- agentops/instrumentation/providers/openai/attributes/__init__.py +7 -0
- agentops/instrumentation/providers/openai/attributes/common.py +55 -0
- agentops/instrumentation/providers/openai/attributes/response.py +607 -0
- agentops/instrumentation/providers/openai/config.py +36 -0
- agentops/instrumentation/providers/openai/instrumentor.py +312 -0
- agentops/instrumentation/providers/openai/stream_wrapper.py +941 -0
- agentops/instrumentation/providers/openai/utils.py +44 -0
- agentops/instrumentation/providers/openai/v0.py +176 -0
- agentops/instrumentation/providers/openai/v0_wrappers.py +483 -0
- agentops/instrumentation/providers/openai/wrappers/__init__.py +30 -0
- agentops/instrumentation/providers/openai/wrappers/assistant.py +277 -0
- agentops/instrumentation/providers/openai/wrappers/chat.py +259 -0
- agentops/instrumentation/providers/openai/wrappers/completion.py +109 -0
- agentops/instrumentation/providers/openai/wrappers/embeddings.py +94 -0
- agentops/instrumentation/providers/openai/wrappers/image_gen.py +75 -0
- agentops/instrumentation/providers/openai/wrappers/responses.py +191 -0
- agentops/instrumentation/providers/openai/wrappers/shared.py +81 -0
- agentops/instrumentation/utilities/concurrent_futures/__init__.py +10 -0
- agentops/instrumentation/utilities/concurrent_futures/instrumentation.py +206 -0
- agentops/integration/callbacks/dspy/__init__.py +11 -0
- agentops/integration/callbacks/dspy/callback.py +471 -0
- agentops/integration/callbacks/langchain/README.md +59 -0
- agentops/integration/callbacks/langchain/__init__.py +15 -0
- agentops/integration/callbacks/langchain/callback.py +791 -0
- agentops/integration/callbacks/langchain/utils.py +54 -0
- agentops/legacy/crewai.md +121 -0
- agentops/logging/instrument_logging.py +4 -0
- agentops/sdk/README.md +220 -0
- agentops/sdk/core.py +75 -32
- agentops/sdk/descriptors/classproperty.py +28 -0
- agentops/sdk/exporters.py +152 -33
- agentops/semconv/README.md +125 -0
- agentops/semconv/span_kinds.py +0 -2
- agentops/validation.py +102 -63
- {mseep_agentops-0.4.18.dist-info → mseep_agentops-0.4.22.dist-info}/METADATA +30 -40
- mseep_agentops-0.4.22.dist-info/RECORD +178 -0
- {mseep_agentops-0.4.18.dist-info → mseep_agentops-0.4.22.dist-info}/WHEEL +1 -2
- mseep_agentops-0.4.18.dist-info/RECORD +0 -94
- mseep_agentops-0.4.18.dist-info/top_level.txt +0 -2
- tests/conftest.py +0 -10
- tests/unit/client/__init__.py +0 -1
- tests/unit/client/test_http_adapter.py +0 -221
- tests/unit/client/test_http_client.py +0 -206
- tests/unit/conftest.py +0 -54
- tests/unit/sdk/__init__.py +0 -1
- tests/unit/sdk/instrumentation_tester.py +0 -207
- tests/unit/sdk/test_attributes.py +0 -392
- tests/unit/sdk/test_concurrent_instrumentation.py +0 -468
- tests/unit/sdk/test_decorators.py +0 -763
- tests/unit/sdk/test_exporters.py +0 -241
- tests/unit/sdk/test_factory.py +0 -1188
- tests/unit/sdk/test_internal_span_processor.py +0 -397
- tests/unit/sdk/test_resource_attributes.py +0 -35
- tests/unit/test_config.py +0 -82
- tests/unit/test_context_manager.py +0 -777
- tests/unit/test_events.py +0 -27
- tests/unit/test_host_env.py +0 -54
- tests/unit/test_init_py.py +0 -501
- tests/unit/test_serialization.py +0 -433
- tests/unit/test_session.py +0 -676
- tests/unit/test_user_agent.py +0 -34
- tests/unit/test_validation.py +0 -405
- {tests → agentops/instrumentation/agentic/openai_agents/attributes}/__init__.py +0 -0
- /tests/unit/__init__.py → /agentops/instrumentation/providers/openai/attributes/tools.py +0 -0
- {mseep_agentops-0.4.18.dist-info → mseep_agentops-0.4.22.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,156 @@
|
|
1
|
+
# OpenAI Agents SDK Instrumentation
|
2
|
+
|
3
|
+
This module provides automatic instrumentation for the OpenAI Agents SDK, adding telemetry that follows OpenTelemetry semantic conventions for Generative AI systems.
|
4
|
+
|
5
|
+
## Architecture Overview
|
6
|
+
|
7
|
+
The OpenAI Agents SDK instrumentor works by:
|
8
|
+
|
9
|
+
1. Intercepting the Agents SDK's trace processor interface to capture Agent, Function, Generation, and other span types
|
10
|
+
2. Monkey-patching the Agents SDK `Runner` class to capture the full execution lifecycle, including streaming operations
|
11
|
+
3. Converting all captured data to OpenTelemetry spans and metrics following semantic conventions
|
12
|
+
|
13
|
+
The instrumentation is organized into several key components:
|
14
|
+
|
15
|
+
1. **Instrumentor (`instrumentor.py`)**: The entry point that patches the Agents SDK and configures trace capture
|
16
|
+
2. **Processor (`processor.py`)**: Receives events from the SDK and prepares them for export
|
17
|
+
3. **Exporter (`exporter.py`)**: Converts SDK spans to OpenTelemetry spans and exports them
|
18
|
+
4. **Attributes Module (`attributes/`)**: Specialized modules for extracting and formatting span attributes
|
19
|
+
|
20
|
+
## Attribute Processing Modules
|
21
|
+
|
22
|
+
The attribute modules extract and format OpenTelemetry-compatible attributes from span data:
|
23
|
+
|
24
|
+
- **Common (`attributes/common.py`)**: Core attribute extraction functions for all span types and utility functions
|
25
|
+
- **Completion (`attributes/completion.py`)**: Handles different completion content formats (Chat Completions API, Response API, Agents SDK)
|
26
|
+
- **Model (`attributes/model.py`)**: Extracts model information and parameters
|
27
|
+
- **Tokens (`attributes/tokens.py`)**: Processes token usage data and metrics
|
28
|
+
- **Response (`attributes/response.py`)**: Handles interpretation of Response API objects
|
29
|
+
|
30
|
+
Each getter function in these modules is focused on a single responsibility and does not modify global state. Functions are designed to be composable, allowing different attribute types to be combined as needed in the exporter.
|
31
|
+
|
32
|
+
## Span Types
|
33
|
+
|
34
|
+
The instrumentor captures the following span types:
|
35
|
+
|
36
|
+
- **Trace**: The root span representing an entire agent workflow execution
|
37
|
+
- Created using `get_base_trace_attributes()` to initialize with standard fields
|
38
|
+
- Captures workflow name, trace ID, and workflow-level metadata
|
39
|
+
|
40
|
+
- **Agent**: Represents an agent's execution lifecycle
|
41
|
+
- Processed using `get_agent_span_attributes()` with `AGENT_SPAN_ATTRIBUTES` mapping
|
42
|
+
- Uses `SpanKind.CONSUMER` to indicate an agent receiving a request
|
43
|
+
- Captures agent name, input, output, tools, and other metadata
|
44
|
+
|
45
|
+
- **Function**: Represents a tool/function call
|
46
|
+
- Processed using `get_function_span_attributes()` with `FUNCTION_SPAN_ATTRIBUTES` mapping
|
47
|
+
- Uses `SpanKind.CLIENT` to indicate an outbound call to a function
|
48
|
+
- Captures function name, input arguments, output results, and from_agent information
|
49
|
+
|
50
|
+
- **Generation**: Captures details of model generation
|
51
|
+
- Processed using `get_generation_span_attributes()` with `GENERATION_SPAN_ATTRIBUTES` mapping
|
52
|
+
- Uses `SpanKind.CLIENT` to indicate an outbound call to an LLM
|
53
|
+
- Captures model name, configuration, usage statistics, and response content
|
54
|
+
|
55
|
+
- **Response**: Lightweight span for tracking model response data
|
56
|
+
- Processed using `get_response_span_attributes()` with `RESPONSE_SPAN_ATTRIBUTES` mapping
|
57
|
+
- Extracts response content and metadata from different API formats
|
58
|
+
|
59
|
+
- **Handoff**: Represents control transfer between agents
|
60
|
+
- Processed using `get_handoff_span_attributes()` with `HANDOFF_SPAN_ATTRIBUTES` mapping
|
61
|
+
- Tracks from_agent and to_agent information
|
62
|
+
|
63
|
+
## Span Lifecycle Management
|
64
|
+
|
65
|
+
The exporter (`exporter.py`) handles the full span lifecycle:
|
66
|
+
|
67
|
+
1. **Start Events**:
|
68
|
+
- Create spans but DO NOT END them
|
69
|
+
- Store span references in tracking dictionaries
|
70
|
+
- Use OpenTelemetry's start_span to control when spans end
|
71
|
+
- Leave status as UNSET to indicate in-progress
|
72
|
+
|
73
|
+
2. **End Events**:
|
74
|
+
- Look up existing span by ID in tracking dictionaries
|
75
|
+
- If found and not ended:
|
76
|
+
- Update span with all final attributes
|
77
|
+
- Set status to OK or ERROR based on task outcome
|
78
|
+
- End the span manually
|
79
|
+
- If not found or already ended:
|
80
|
+
- Create a new complete span with all data
|
81
|
+
- End it immediately
|
82
|
+
|
83
|
+
3. **Error Handling**:
|
84
|
+
- Check if spans are already ended before attempting updates
|
85
|
+
- Provide informative log messages about span lifecycle
|
86
|
+
- Properly clean up tracking resources
|
87
|
+
|
88
|
+
This approach is essential because:
|
89
|
+
- Agents SDK sends separate start and end events for each task
|
90
|
+
- We need to maintain a single span for the entire task lifecycle to get accurate timing
|
91
|
+
- Final data (outputs, token usage, etc.) is only available at the end event
|
92
|
+
- We want to avoid creating duplicate spans for the same task
|
93
|
+
- Spans must be properly created and ended to avoid leaks
|
94
|
+
|
95
|
+
The span lifecycle management ensures spans have:
|
96
|
+
- Accurate start and end times (preserving the actual task duration)
|
97
|
+
- Complete attribute data from both start and end events
|
98
|
+
- Proper status reflecting task completion
|
99
|
+
- All final outputs, errors, and metrics
|
100
|
+
- Clean resource management with no memory leaks
|
101
|
+
|
102
|
+
## Key Design Patterns
|
103
|
+
|
104
|
+
### Semantic Conventions
|
105
|
+
|
106
|
+
All attribute names follow the OpenTelemetry semantic conventions defined in `agentops.semconv`:
|
107
|
+
|
108
|
+
```python
|
109
|
+
# Using constants from semconv module
|
110
|
+
attributes[CoreAttributes.TRACE_ID] = trace_id
|
111
|
+
attributes[WorkflowAttributes.WORKFLOW_NAME] = trace.name
|
112
|
+
attributes[SpanAttributes.LLM_SYSTEM] = "openai"
|
113
|
+
attributes[MessageAttributes.COMPLETION_CONTENT.format(i=0)] = content
|
114
|
+
```
|
115
|
+
|
116
|
+
### Target → Source Attribute Mapping
|
117
|
+
|
118
|
+
We use a consistent pattern for attribute extraction with typed mapping dictionaries:
|
119
|
+
|
120
|
+
```python
|
121
|
+
# Attribute mapping example
|
122
|
+
AGENT_SPAN_ATTRIBUTES: AttributeMap = {
|
123
|
+
# target_attribute: source_attribute
|
124
|
+
AgentAttributes.AGENT_NAME: "name",
|
125
|
+
WorkflowAttributes.WORKFLOW_INPUT: "input",
|
126
|
+
WorkflowAttributes.FINAL_OUTPUT: "output",
|
127
|
+
# ...
|
128
|
+
}
|
129
|
+
```
|
130
|
+
|
131
|
+
### Structured Attribute Handling
|
132
|
+
|
133
|
+
- Always use MessageAttributes semantic conventions for content and tool calls
|
134
|
+
- For chat completions, use MessageAttributes.COMPLETION_CONTENT.format(i=0)
|
135
|
+
- For tool calls, use MessageAttributes.COMPLETION_TOOL_CALL_NAME.format(i=0, j=0), etc.
|
136
|
+
- Never try to combine or aggregate contents into a single attribute
|
137
|
+
- Each message component should have its own properly formatted attribute
|
138
|
+
- This ensures proper display in OpenTelemetry backends and dashboards
|
139
|
+
|
140
|
+
### Serialization Rules
|
141
|
+
|
142
|
+
1. We do not serialize data structures arbitrarily; everything has a semantic convention
|
143
|
+
2. Span attributes should use semantic conventions and avoid complex serialized structures
|
144
|
+
3. Keep all string data in its original form - do not parse JSON within strings
|
145
|
+
4. If a function has JSON attributes for its arguments, do not parse that JSON - keep as string
|
146
|
+
5. If a completion or response body text/content contains JSON, keep it as a string
|
147
|
+
7. Function arguments and tool call arguments should remain in their raw string form
|
148
|
+
|
149
|
+
### Critical Notes for Attribute Handling
|
150
|
+
|
151
|
+
- NEVER manually set the root completion attributes (`SpanAttributes.LLM_COMPLETIONS` or "gen_ai.completion")
|
152
|
+
- Let OpenTelemetry backend derive these values from the detailed attributes
|
153
|
+
- Setting root completion attributes creates duplication and inconsistency
|
154
|
+
- Tests should verify attribute existence using MessageAttributes constants
|
155
|
+
- Do not check for the presence of SpanAttributes.LLM_COMPLETIONS
|
156
|
+
- Verify individual content/tool attributes instead of root attributes
|
@@ -0,0 +1,145 @@
|
|
1
|
+
# OpenAI Agents Spans and Traces
|
2
|
+
|
3
|
+
This document describes the span types, naming conventions, and attribute patterns used by the AgentOps instrumentation for the OpenAI Agents SDK.
|
4
|
+
|
5
|
+
## Span Types and Classes
|
6
|
+
|
7
|
+
The instrumentation works with these specific span data classes:
|
8
|
+
|
9
|
+
1. **AgentSpanData**: Represents a single agent's operation
|
10
|
+
- Has attributes for name, input, output, tools, and handoffs
|
11
|
+
- Processed by `get_agent_span_attributes()` using `AGENT_SPAN_ATTRIBUTES` mapping
|
12
|
+
|
13
|
+
2. **FunctionSpanData**: Represents tool or function calls
|
14
|
+
- Has attributes for name, input, output, and from_agent
|
15
|
+
- Processed by `get_function_span_attributes()` using `FUNCTION_SPAN_ATTRIBUTES` mapping
|
16
|
+
|
17
|
+
3. **GenerationSpanData**: Represents LLM model invocations
|
18
|
+
- Has attributes for model, input, output, tools, and from_agent
|
19
|
+
- Processed by `get_generation_span_attributes()` using `GENERATION_SPAN_ATTRIBUTES` mapping
|
20
|
+
|
21
|
+
4. **HandoffSpanData**: Represents agent-to-agent handoffs
|
22
|
+
- Has attributes for from_agent and to_agent
|
23
|
+
- Processed by `get_handoff_span_attributes()` using `HANDOFF_SPAN_ATTRIBUTES` mapping
|
24
|
+
|
25
|
+
5. **ResponseSpanData**: Represents model response data
|
26
|
+
- Has attributes for input and response
|
27
|
+
- Processed by `get_response_span_attributes()` using `RESPONSE_SPAN_ATTRIBUTES` mapping
|
28
|
+
|
29
|
+
## Span Naming Conventions
|
30
|
+
|
31
|
+
Spans are named according to these conventions:
|
32
|
+
|
33
|
+
1. **Trace Spans**: `agents.trace.{workflow_name}`
|
34
|
+
- Represents the entire agent workflow
|
35
|
+
- Named after the workflow or trace name
|
36
|
+
|
37
|
+
2. **Agent Spans**: `agents.agent`
|
38
|
+
- Represents a single agent's operation
|
39
|
+
- Uses `SpanKind.CONSUMER`
|
40
|
+
|
41
|
+
3. **Function Spans**: `agents.function`
|
42
|
+
- Represents tool or function calls
|
43
|
+
- Uses `SpanKind.CLIENT`
|
44
|
+
|
45
|
+
4. **Generation Spans**: `agents.generation`
|
46
|
+
- Represents LLM model invocations
|
47
|
+
- Uses `SpanKind.CLIENT`
|
48
|
+
|
49
|
+
5. **Handoff Spans**: `agents.handoff`
|
50
|
+
- Represents agent-to-agent handoffs
|
51
|
+
- Uses `SpanKind.INTERNAL`
|
52
|
+
|
53
|
+
6. **Response Spans**: `agents.response`
|
54
|
+
- Represents model response data
|
55
|
+
- Uses `SpanKind.CLIENT`
|
56
|
+
|
57
|
+
## Span Hierarchy
|
58
|
+
|
59
|
+
The spans follow a parent-child relationship that reflects the execution flow:
|
60
|
+
|
61
|
+
```
|
62
|
+
agents.trace.{workflow_name}
|
63
|
+
└── agents.agent
|
64
|
+
├── agents.generation
|
65
|
+
├── agents.function
|
66
|
+
├── agents.response
|
67
|
+
└── agents.handoff
|
68
|
+
```
|
69
|
+
|
70
|
+
## Semantic Conventions and Attributes
|
71
|
+
|
72
|
+
Each span type has attributes following OpenTelemetry semantic conventions:
|
73
|
+
|
74
|
+
### Common Attributes (All Spans)
|
75
|
+
|
76
|
+
- `trace.id`: OpenTelemetry trace ID
|
77
|
+
- `span.id`: OpenTelemetry span ID
|
78
|
+
- `parent.id`: Parent span ID (if applicable)
|
79
|
+
- `instrumentation.name`: "agentops"
|
80
|
+
- `instrumentation.version`: AgentOps library version
|
81
|
+
- `instrumentation.library.name`: "openai_agents"
|
82
|
+
- `instrumentation.library.version`: Library version
|
83
|
+
|
84
|
+
### Workflow and Trace Attributes
|
85
|
+
|
86
|
+
- `workflow.name`: Name of the workflow or trace
|
87
|
+
- `workflow.step_type`: "trace" for trace spans
|
88
|
+
- `workflow.input`: Input to the workflow
|
89
|
+
- `workflow.final_output`: Final output from the workflow
|
90
|
+
|
91
|
+
### Agent Attributes
|
92
|
+
|
93
|
+
- `agent.name`: The name of the agent
|
94
|
+
- `agent.tools`: Comma-separated list of available tools
|
95
|
+
- `agent.handoffs`: Comma-separated list of handoff targets
|
96
|
+
- `agent.from`: Source agent in handoffs (used in HandoffSpanData)
|
97
|
+
- `agent.to`: Destination agent in handoffs (used in HandoffSpanData)
|
98
|
+
|
99
|
+
### LLM Attributes
|
100
|
+
|
101
|
+
- `gen_ai.system`: "openai" for all OpenAI spans
|
102
|
+
- `gen_ai.request.model`: Model used for generation
|
103
|
+
- `gen_ai.response.model`: Model that provided the response
|
104
|
+
- `gen_ai.prompt`: Input prompt or message
|
105
|
+
- `gen_ai.completion.0.role`: Role of the completion message (usually "assistant")
|
106
|
+
- `gen_ai.completion.0.content`: Content of the completion message
|
107
|
+
- `gen_ai.tool_call.0.0.name`: Name of the tool called (if applicable)
|
108
|
+
- `gen_ai.tool_call.0.0.arguments`: Arguments for the tool call (if applicable)
|
109
|
+
|
110
|
+
### Token Usage Attributes
|
111
|
+
|
112
|
+
- `gen_ai.usage.prompt_tokens`: Number of input tokens
|
113
|
+
- `gen_ai.usage.completion_tokens`: Number of output tokens
|
114
|
+
- `gen_ai.usage.total_tokens`: Total number of tokens
|
115
|
+
- `gen_ai.usage.reasoning_tokens`: Tokens used for reasoning (Response API)
|
116
|
+
- `gen_ai.usage.cache_read.input_tokens`: Cached input tokens (Response API)
|
117
|
+
|
118
|
+
## Span Lifecycle Management
|
119
|
+
|
120
|
+
The exporter handles span lifecycle with these stages:
|
121
|
+
|
122
|
+
1. **Start Events**:
|
123
|
+
- Create spans with `start_span()` (not using context manager)
|
124
|
+
- Store span references in tracking dictionaries
|
125
|
+
- Leave status as UNSET to indicate in-progress
|
126
|
+
|
127
|
+
2. **End Events**:
|
128
|
+
- Look up existing span by ID
|
129
|
+
- Update with final attributes
|
130
|
+
- Set appropriate status and end the span manually
|
131
|
+
|
132
|
+
3. **Error Handling**:
|
133
|
+
- Set status to ERROR for spans with errors
|
134
|
+
- Add error type and message as attributes
|
135
|
+
- Record exceptions with `record_exception()`
|
136
|
+
|
137
|
+
## OpenTelemetry Span Kinds
|
138
|
+
|
139
|
+
Span kinds map to OpenTelemetry concepts:
|
140
|
+
|
141
|
+
- `AgentSpanData` → `SpanKind.CONSUMER`
|
142
|
+
- `FunctionSpanData` → `SpanKind.CLIENT`
|
143
|
+
- `GenerationSpanData` → `SpanKind.CLIENT`
|
144
|
+
- `ResponseSpanData` → `SpanKind.CLIENT`
|
145
|
+
- `HandoffSpanData` → `SpanKind.INTERNAL`
|
@@ -0,0 +1,144 @@
|
|
1
|
+
# OpenAI Agents Tracing API Integration
|
2
|
+
|
3
|
+
This document provides an overview of how AgentOps integrates with the OpenAI Agents SDK tracing system.
|
4
|
+
|
5
|
+
## OpenAI Agents Tracing API Overview
|
6
|
+
|
7
|
+
The OpenAI Agents SDK provides a comprehensive tracing system that allows you to monitor and instrument agent activities. AgentOps integrates with this system to capture and forward trace data to its backend.
|
8
|
+
|
9
|
+
## Core Integration Methods
|
10
|
+
|
11
|
+
### 1. `add_trace_processor(processor)`
|
12
|
+
|
13
|
+
The main integration point that allows external systems like AgentOps to receive trace events:
|
14
|
+
|
15
|
+
```python
|
16
|
+
from agents import add_trace_processor
|
17
|
+
from agentops.instrumentation.openai_agents.processor import OpenAIAgentsProcessor
|
18
|
+
|
19
|
+
processor = OpenAIAgentsProcessor()
|
20
|
+
add_trace_processor(processor)
|
21
|
+
```
|
22
|
+
|
23
|
+
### 2. `set_trace_processors(processors)`
|
24
|
+
|
25
|
+
Replaces all current processors with a new list:
|
26
|
+
|
27
|
+
```python
|
28
|
+
from agents import set_trace_processors
|
29
|
+
set_trace_processors([my_processor1, my_processor2])
|
30
|
+
```
|
31
|
+
|
32
|
+
### 3. `set_tracing_disabled(disabled)`
|
33
|
+
|
34
|
+
Globally enables/disables tracing:
|
35
|
+
|
36
|
+
```python
|
37
|
+
from agents import set_tracing_disabled
|
38
|
+
set_tracing_disabled(True) # Disable tracing
|
39
|
+
```
|
40
|
+
|
41
|
+
### 4. `set_tracing_export_api_key(api_key)`
|
42
|
+
|
43
|
+
Sets the API key for the backend exporter:
|
44
|
+
|
45
|
+
```python
|
46
|
+
from agents import set_tracing_export_api_key
|
47
|
+
set_tracing_export_api_key("your-api-key")
|
48
|
+
```
|
49
|
+
|
50
|
+
## Span Creation Methods
|
51
|
+
|
52
|
+
The SDK provides specialized methods for creating different types of spans:
|
53
|
+
|
54
|
+
1. **`agent_span(name, handoffs, tools, output_type, ...)`**
|
55
|
+
- Creates spans for agent operations
|
56
|
+
- Tracks agent name, available tools, potential handoffs
|
57
|
+
|
58
|
+
2. **`function_span(name, input, output, ...)`**
|
59
|
+
- Creates spans for function/tool calls
|
60
|
+
- Records function name, input arguments, and results
|
61
|
+
|
62
|
+
3. **`generation_span(input, output, model, model_config, usage, ...)`**
|
63
|
+
- Creates spans for LLM generations
|
64
|
+
- Records prompts, completions, model details, and token usage
|
65
|
+
|
66
|
+
4. **`response_span(response, ...)`**
|
67
|
+
- Lightweight span for capturing OpenAI API response metadata
|
68
|
+
|
69
|
+
5. **`handoff_span(from_agent, to_agent, ...)`**
|
70
|
+
- Tracks agent-to-agent handoffs
|
71
|
+
|
72
|
+
6. **`guardrail_span(name, triggered, ...)`**
|
73
|
+
- Records guardrail evaluations
|
74
|
+
|
75
|
+
7. **`custom_span(name, data, ...)`**
|
76
|
+
- Creates user-defined spans with arbitrary data
|
77
|
+
|
78
|
+
## Trace and Context Management
|
79
|
+
|
80
|
+
1. **`trace(workflow_name, trace_id, group_id, metadata, ...)`**
|
81
|
+
- Creates and manages a trace context
|
82
|
+
- Groups related spans into a logical trace/session
|
83
|
+
|
84
|
+
2. **`get_current_span()`**
|
85
|
+
- Returns the current active span
|
86
|
+
|
87
|
+
3. **`get_current_trace()`**
|
88
|
+
- Returns the current active trace
|
89
|
+
|
90
|
+
## How AgentOps Implements Integration
|
91
|
+
|
92
|
+
AgentOps integrates with this API through:
|
93
|
+
|
94
|
+
1. The `OpenAIAgentsProcessor` class that implements the `TracingProcessor` interface
|
95
|
+
2. The `create_span` context manager that ensures proper parent-child relationships between spans
|
96
|
+
3. The `AgentsInstrumentor` which registers the processor and adds additional instrumentation
|
97
|
+
|
98
|
+
This integration allows AgentOps to capture detailed information about agent execution, including:
|
99
|
+
- Agent operations and tool usage
|
100
|
+
- LLM requests and responses
|
101
|
+
- Token usage metrics
|
102
|
+
- Error information
|
103
|
+
- Agent-to-agent handoffs
|
104
|
+
|
105
|
+
### Trace Context Propagation
|
106
|
+
|
107
|
+
Our implementation ensures proper parent-child relationships between spans through:
|
108
|
+
|
109
|
+
1. **Context Manager Pattern**: Using `start_as_current_span()` to maintain the OpenTelemetry span context
|
110
|
+
2. **Parent Reference Tracking**: Storing parent span relationships and using them to create proper span hierarchies
|
111
|
+
3. **Trace Correlation Attributes**: Adding consistent attributes to help with querying:
|
112
|
+
- `agentops.original_trace_id`: Original trace ID from the Agents SDK
|
113
|
+
- `agentops.original_span_id`: Original span ID from the Agents SDK
|
114
|
+
- `agentops.parent_span_id`: Parent span ID for child spans
|
115
|
+
- `agentops.trace_hash`: Consistent hash based on the original trace ID
|
116
|
+
- `agentops.is_root_span`: "true" for spans without a parent
|
117
|
+
|
118
|
+
When querying spans for analysis:
|
119
|
+
1. Group spans by `agentops.original_trace_id` to find all spans in the same trace
|
120
|
+
2. Use `agentops.parent_span_id` to reconstruct the parent-child hierarchy
|
121
|
+
|
122
|
+
## Span Data Types
|
123
|
+
|
124
|
+
Several specialized span data types exist in the OpenAI Agents SDK to capture different operations:
|
125
|
+
|
126
|
+
- **AgentSpanData**: Captures agent execution data
|
127
|
+
- **FunctionSpanData**: Records tool/function calls
|
128
|
+
- **GenerationSpanData**: Records LLM generation details
|
129
|
+
- **ResponseSpanData**: Captures model response information
|
130
|
+
- **HandoffSpanData**: Tracks agent-to-agent handoffs
|
131
|
+
- **GuardrailSpanData**: Records guardrail evaluations
|
132
|
+
- **CustomSpanData**: For user-defined spans
|
133
|
+
|
134
|
+
## Processor Interface
|
135
|
+
|
136
|
+
The `TracingProcessor` interface defines methods processors must implement:
|
137
|
+
- `on_trace_start`: Called when a trace begins
|
138
|
+
- `on_trace_end`: Called when a trace ends
|
139
|
+
- `on_span_start`: Called when a span begins
|
140
|
+
- `on_span_end`: Called when a span completes
|
141
|
+
- `shutdown`: Called during application shutdown
|
142
|
+
- `force_flush`: Forces immediate processing of pending spans
|
143
|
+
|
144
|
+
The processor receives events from OpenAI Agents SDK's tracing system through these callback methods, translates them to OpenTelemetry spans, and sends them to the AgentOps backend for analysis and visualization.
|
@@ -0,0 +1,30 @@
|
|
1
|
+
"""
|
2
|
+
AgentOps Instrumentor for OpenAI Agents SDK
|
3
|
+
|
4
|
+
This module provides automatic instrumentation for the OpenAI Agents SDK when AgentOps is imported.
|
5
|
+
It implements a clean, maintainable implementation that follows semantic conventions.
|
6
|
+
|
7
|
+
IMPORTANT DISTINCTION BETWEEN OPENAI API FORMATS:
|
8
|
+
1. OpenAI Completions API - The traditional API format using prompt_tokens/completion_tokens
|
9
|
+
2. OpenAI Response API - The newer format used by the Agents SDK using input_tokens/output_tokens
|
10
|
+
3. Agents SDK - The framework that uses Response API format
|
11
|
+
|
12
|
+
The Agents SDK uses the Response API format, which we handle using shared utilities from
|
13
|
+
agentops.instrumentation.openai.
|
14
|
+
"""
|
15
|
+
|
16
|
+
from agentops.instrumentation.common import LibraryInfo
|
17
|
+
|
18
|
+
# Library information
|
19
|
+
_library_info = LibraryInfo(name="openai-agents")
|
20
|
+
LIBRARY_NAME = _library_info.name
|
21
|
+
LIBRARY_VERSION = _library_info.version
|
22
|
+
|
23
|
+
# Import after defining constants to avoid circular imports
|
24
|
+
from agentops.instrumentation.agentic.openai_agents.instrumentor import OpenAIAgentsInstrumentor # noqa: E402
|
25
|
+
|
26
|
+
__all__ = [
|
27
|
+
"LIBRARY_NAME",
|
28
|
+
"LIBRARY_VERSION",
|
29
|
+
"OpenAIAgentsInstrumentor",
|
30
|
+
]
|