Skip to content

Commit 295b591

Browse files
committed
add test cases
1 parent bcfefd8 commit 295b591

File tree

33 files changed

+2733
-58
lines changed

33 files changed

+2733
-58
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
examples/.env
2+
examples/openai_agents_multi_agent_travel/.env
3+
examples/**/.env
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7+
8+
## Unreleased
9+
- Document official package metadata and README for the OpenAI Agents instrumentation.
10+
([#3859](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3859))
11+
12+
## Version 0.1.0 (2025-10-15)
13+
14+
- Initial barebones package skeleton: minimal instrumentor stub, version module,
15+
and packaging metadata/entry point.
16+
([#3805](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3805))
17+
- Implement OpenAI Agents span processing aligned with GenAI semantic conventions.
18+
([#3817](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3817))
19+
- Input and output according to GenAI spec.
20+
([#3824](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3824))
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
Apache License
2+
Version 2.0, January 2004
3+
http://www.apache.org/licenses/
4+
5+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6+
7+
1. Definitions.
8+
9+
"License" shall mean the terms and conditions for use, reproduction,
10+
and distribution as defined by Sections 1 through 9 of this document.
11+
12+
"Licensor" shall mean the copyright owner or entity granting the License.
13+
14+
"Legal Entity" shall mean the union of the acting entity and all
15+
other entities that control, are controlled by, or are under common
16+
control with that entity. For the purposes of this definition,
17+
"control" means (i) the power, direct or indirect, to cause the
18+
direction or management of such entity, whether by contract or
19+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
20+
outstanding shares, or (iii) beneficial ownership of such entity.
21+
22+
"You" (or "Your") shall mean an individual or Legal Entity
23+
exercising permissions granted by this License.
24+
25+
"Source" form shall mean the preferred form for making modifications,
26+
including but not limited to software source code, documentation
27+
source, and configuration files.
28+
29+
"Object" form shall mean any form resulting from mechanical
30+
transformation or translation of a Source form, including but
31+
not limited to compiled object code, generated documentation,
32+
and conversions to other media types.
33+
34+
"Work" shall mean the work of authorship, whether in Source or
35+
Object form, made available under the License, as indicated by a
36+
copyright notice that is included in or attached to the work
37+
(which shall not include communication that is clearly marked or
38+
otherwise designated in writing by the copyright owner as "Not a Work").
39+
40+
"Derivative Works" shall mean any work, whether in Source or Object
41+
form, that is based upon (or derived from) the Work and for which the
42+
editorial revisions, annotations, elaborations, or other modifications
43+
represent, as a whole, an original work of authorship. For the purposes
44+
of this License, Derivative Works shall not include works that remain
45+
separable from, or merely link (or bind by name) to the interfaces of,
46+
the Work and derivative works thereof.
47+
48+
"Contribution" shall mean any work of authorship, including
49+
the original version of the Work and any modifications or additions
50+
to that Work or Derivative Works thereof, that is intentionally
51+
submitted to Licensor for inclusion in the Work by the copyright owner
52+
or by an individual or Legal Entity authorized to submit on behalf of
53+
the copyright owner. For the purposes of this definition, "submitted"
54+
means any form of electronic, verbal, or written communication sent
55+
to the Licensor or its representatives, including but not limited to
56+
communication on electronic mailing lists, source code control
57+
systems, and issue tracking systems that are managed by, or on behalf
58+
of, the Licensor for the purpose of discussing and improving the Work,
59+
but excluding communication that is clearly marked or otherwise
60+
designated in writing by the copyright owner as "Not a Contribution."
61+
62+
"Contributor" shall mean Licensor and any individual or Legal Entity
63+
on behalf of whom a Contribution has been received by Licensor and
64+
subsequently incorporated within the Work.
65+
66+
2. Grant of Copyright License. Subject to the terms and conditions of
67+
this License, each Contributor hereby grants to You a perpetual,
68+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69+
copyright license to use, reproduce, modify, distribute, and prepare
70+
Derivative Works of, publicly display, publicly perform, sublicense,
71+
and distribute the Work and such Derivative Works in Source or Object
72+
form.
73+
74+
3. Grant of Patent License. Subject to the terms and conditions of
75+
this License, each Contributor hereby grants to You a perpetual,
76+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77+
(except as stated in this section) patent license to make, have made,
78+
use, offer to sell, sell, import, and otherwise transfer the Work,
79+
where such license applies only to those patent claims licensable
80+
by such Contributor that are necessarily infringed by their
81+
Contribution(s) alone or by combination of their Contribution(s)
82+
with the Work to which such Contribution(s) was submitted. If You
83+
institute patent litigation against any entity (including a
84+
cross-claim or counterclaim in a lawsuit) alleging that the Work
85+
or a Contribution incorporated within the Work constitutes direct
86+
or contributory patent infringement, then any patent licenses
87+
granted to You under this License for that Work shall terminate
88+
as of the date such litigation is filed.
89+
90+
4. Redistribution. You may reproduce and distribute copies of the
91+
Work or Derivative Works thereof in any medium, with or without
92+
modifications, and in Source or Object form, provided that You
93+
meet the following conditions:
94+
95+
(a) You must give any other recipients of the Work or
96+
Derivative Works a copy of this License; and
97+
98+
(b) You must cause any modified files to carry prominent notices
99+
stating that You changed the files; and
100+
101+
(c) You must retain, in the Source form of any Derivative Works
102+
that You distribute, all copyright, trademark, patent,
103+
attribution and other notices from the Source form of the Work,
104+
excluding those notices that do not pertain to any part of
105+
the Derivative Works; and
106+
107+
(d) If the Work includes a "NOTICE" text file as part of its
108+
distribution, then any Derivative Works that You distribute must
109+
include a readable copy of the attribution notices contained
110+
within such NOTICE file, excluding those notices that do not
111+
pertain to any part of the Derivative Works, in at least one
112+
of the following places: within a NOTICE text file distributed
113+
as part of the Derivative Works; within the Source form or
114+
documentation, if provided along with the Derivative Works; or,
115+
within a display generated by the Derivative Works, if and
116+
wherever such third-party notices normally appear. The contents
117+
of the NOTICE file are for informational purposes only and
118+
do not modify the License. You may add Your own attribution
119+
notices within Derivative Works that You distribute, alongside
120+
or as an addendum to the NOTICE text from the Work, provided
121+
that such additional attribution notices cannot be construed
122+
as modifying the License.
123+
124+
You may add Your own copyright notice to Your modifications and
125+
may provide additional or different license terms and conditions
126+
for use, reproduction, or distribution of Your modifications, or
127+
for any such Derivative Works as a whole, provided Your use,
128+
reproduction, and distribution of the Work otherwise complies with
129+
the conditions stated in this License.
130+
131+
5. Submission of Contributions. Unless You explicitly state otherwise,
132+
any Contribution intentionally submitted for inclusion in the Work
133+
by You to the Licensor shall be under the terms and conditions of
134+
this License, without any additional terms or conditions.
135+
Notwithstanding the above, nothing herein shall supersede or modify
136+
the terms of any separate license agreement you may have executed
137+
with Licensor regarding such Contributions.
138+
139+
6. Trademarks. This License does not grant permission to use the trade
140+
names, trademarks, service marks, or product names of the Licensor,
141+
except as required for reasonable and customary use in describing the
142+
origin of the Work and reproducing the content of the NOTICE file.
143+
144+
7. Disclaimer of Warranty. Unless required by applicable law or
145+
agreed to in writing, Licensor provides the Work (and each
146+
Contributor provides its Contributions) on an "AS IS" BASIS,
147+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148+
implied, including, without limitation, any warranties or conditions
149+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150+
PARTICULAR PURPOSE. You are solely responsible for determining the
151+
appropriateness of using or redistributing the Work and assume any
152+
risks associated with Your exercise of permissions under this License.
153+
154+
8. Limitation of Liability. In no event and under no legal theory,
155+
whether in tort (including negligence), contract, or otherwise,
156+
unless required by applicable law (such as deliberate and grossly
157+
negligent acts) or agreed to in writing, shall any Contributor be
158+
liable to You for damages, including any direct, indirect, special,
159+
incidental, or consequential damages of any character arising as a
160+
result of this License or out of the use or inability to use the
161+
Work (including but not limited to damages for loss of goodwill,
162+
work stoppage, computer failure or malfunction, or any and all
163+
other commercial damages or losses), even if such Contributor
164+
has been advised of the possibility of such damages.
165+
166+
9. Accepting Warranty or Support. When redistributing the Work or
167+
Derivative Works thereof, You may choose to offer, and charge a fee
168+
for, acceptance of support, warranty, indemnity, or other liability
169+
obligations and/or rights consistent with this License. However, in
170+
accepting such obligations, You may act only on Your own behalf and on
171+
Your sole responsibility, not on behalf of any other Contributor, and
172+
only if You agree to indemnify, defend, and hold each Contributor
173+
harmless for any liability incurred by, or claims asserted against,
174+
such Contributor by reason of your accepting any such warranty or support.
175+
176+
END OF TERMS AND CONDITIONS
177+
178+
APPENDIX: How to apply the Apache License to your work.
179+
180+
To apply the Apache License to your work, attach the following
181+
boilerplate notice, with the fields enclosed by brackets "[]"
182+
replaced with your own identifying information. (Don't include
183+
the brackets!) The text should be enclosed in the appropriate
184+
comment syntax for the file format. We also recommend that a
185+
file or class name and description of purpose be included on the
186+
same "printed page" as the copyright notice for easier
187+
identification within third-party archives.
188+
189+
Copyright [yyyy] [name of copyright owner]
190+
191+
Licensed under the Apache License, Version 2.0 (the "License");
192+
you may not use this file except in compliance with the License.
193+
You may obtain a copy of the License at
194+
195+
http://www.apache.org/licenses/LICENSE-2.0
196+
197+
Unless required by applicable law or agreed to in writing, software
198+
distributed under the License is distributed on an "AS IS" BASIS,
199+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200+
See the License for the specific language governing permissions and
201+
limitations under the License.
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
OpenTelemetry OpenAI Agents Instrumentation
2+
===========================================
3+
4+
|pypi|
5+
6+
.. |pypi| image:: https://badge.fury.io/py/opentelemetry-instrumentation-openai-agents-v2.svg
7+
:target: https://pypi.org/project/opentelemetry-instrumentation-openai-agents-v2/
8+
9+
This library provides the official OpenTelemetry instrumentation for the
10+
`openai-agents SDK <https://pypi.org/project/openai-agents/>`_. It converts
11+
the rich trace data emitted by the Agents runtime
12+
into the GenAI semantic conventions, enriches spans with request/response payload
13+
metadata, and records duration/token usage metrics.
14+
15+
Features
16+
--------
17+
18+
* Generates spans for agents, tools, generations, guardrails, and handoffs using
19+
the OpenTelemetry GenAI semantic conventions.
20+
* Captures prompts, responses, tool arguments, and system instructions when content
21+
capture is enabled.
22+
* Publishes duration and token metrics for every operation.
23+
* Supports environment overrides so you can configure agent metadata or disable
24+
telemetry without code changes.
25+
26+
Installation
27+
------------
28+
29+
If your application is already configured with OpenTelemetry, install the package
30+
and its optional instruments:
31+
32+
.. code-block:: console
33+
34+
pip install opentelemetry-instrumentation-openai-agents-v2
35+
pip install openai-agents
36+
37+
Usage
38+
-----
39+
40+
Instrumentation automatically wires the Agents tracing processor into the SDK.
41+
Configure OpenTelemetry as usual, then call :class:`OpenAIAgentsInstrumentor`.
42+
43+
.. code-block:: python
44+
45+
from agents import Agent, Runner, function_tool
46+
from opentelemetry import trace
47+
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
48+
from opentelemetry.instrumentation.openai_agents import OpenAIAgentsInstrumentor
49+
from opentelemetry.sdk.trace import TracerProvider
50+
from opentelemetry.sdk.trace.export import BatchSpanProcessor
51+
52+
53+
def configure_otel() -> None:
54+
provider = TracerProvider()
55+
provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
56+
trace.set_tracer_provider(provider)
57+
58+
OpenAIAgentsInstrumentor().instrument(tracer_provider=provider)
59+
60+
61+
@function_tool
62+
def get_weather(city: str) -> str:
63+
return f"The forecast for {city} is sunny with pleasant temperatures."
64+
65+
66+
assistant = Agent(
67+
name="Travel Concierge",
68+
instructions="You are a concise travel concierge.",
69+
tools=[get_weather],
70+
)
71+
72+
result = Runner.run_sync(assistant, "I'm visiting Barcelona this weekend. How should I pack?")
73+
print(result.final_output)
74+
75+
Configuration
76+
-------------
77+
78+
The instrumentor exposes runtime toggles through keyword arguments and environment
79+
variables:
80+
81+
* ``OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT`` or
82+
``OTEL_INSTRUMENTATION_OPENAI_AGENTS_CAPTURE_CONTENT`` – controls how much
83+
message content is captured. Valid values map to
84+
:class:`opentelemetry.instrumentation.openai_agents.ContentCaptureMode`
85+
(``span_only``, ``event_only``, ``span_and_event``, ``no_content``).
86+
* ``OTEL_INSTRUMENTATION_OPENAI_AGENTS_CAPTURE_METRICS`` – set to ``false`` to
87+
disable duration/token metrics.
88+
* ``OTEL_INSTRUMENTATION_OPENAI_AGENTS_SYSTEM`` – overrides the ``gen_ai.system``
89+
attribute when your deployment is not the default OpenAI platform.
90+
91+
You can also override agent metadata directly when calling
92+
``OpenAIAgentsInstrumentor().instrument(...)`` using ``agent_name``, ``agent_id``,
93+
``agent_description``, ``base_url``, ``server_address``, and ``server_port``.
94+
95+
Examples
96+
--------
97+
98+
The :mod:`examples` directory contains runnable scenarios, including:
99+
100+
* ``examples/manual`` – manual OpenTelemetry configuration for a single agent run.
101+
* ``examples/content-capture`` – demonstrates span and event content capture.
102+
* ``examples/zero-code`` – end-to-end setup using environment configuration only.
103+
104+
References
105+
----------
106+
107+
* `OpenTelemetry Python Contrib <https://github.com/open-telemetry/opentelemetry-python-contrib>`_
108+
* `OpenTelemetry GenAI semantic conventions <https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-spans/>`_
109+
* `OpenAI Agents SDK <https://github.com/openai/openai-agents-python>`_
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Copy to .env and add values before running the sample.
2+
# Required for OpenAI client (only used if you swap in a real OpenAI call)
3+
OPENAI_API_KEY=
4+
5+
# Optional overrides for span attributes / exporters
6+
OTEL_SERVICE_NAME=openai-agents-content-capture-demo
7+
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
8+
OTEL_EXPORTER_OTLP_PROTOCOL=grpc
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# OpenAI Agents Content Capture Demo
2+
3+
This example exercises the `OpenAIAgentsInstrumentor` with message content
4+
capture enabled, illustrating how prompts, responses, and tool payloads are
5+
recorded on spans and span events.
6+
7+
> The demo uses the local tracing utilities from the `openai-agents`
8+
> package—no outbound API calls are made.
9+
10+
## Prerequisites
11+
12+
1. Activate the repository virtual environment:
13+
14+
```bash
15+
source ../../.venv/bin/activate
16+
```
17+
18+
2. Copy `.env.example` to `.env` and provide any overrides you need (for example,
19+
setting `OTEL_EXPORTER_OTLP_ENDPOINT`).
20+
3. Ensure `openai-agents` is installed in the environment (it is included in
21+
the shared development venv for this repository).
22+
23+
## Run the demo
24+
25+
```bash
26+
python main.py
27+
```
28+
29+
The script will:
30+
31+
- Configure the OpenTelemetry SDK with an OTLP exporter so spans reach your collector.
32+
- Instrument the OpenAI Agents tracing hooks with content capture enabled.
33+
- Simulate an agent invocation that performs a generation and a tool call.
34+
- Print the resulting spans, attributes, and events (including JSON-encoded
35+
prompts and responses) to stdout.
36+
37+
## Customisation tips
38+
39+
- Set `OTEL_SERVICE_NAME` before running to override the default service name.
40+
- Adjust the OTLP exporter configuration (endpoint, protocol) through `.env`.
41+
- Modify the prompts, tool payloads, or add additional spans in `run_workflow`
42+
to explore different content capture scenarios.

0 commit comments

Comments
 (0)