Skip to content

Commit 2c283a6

Browse files
committed
Align OpenAI agents samples with OTLP exporter
1 parent c57d59b commit 2c283a6

File tree

12 files changed

+135
-61
lines changed

12 files changed

+135
-61
lines changed
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: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ recorded on spans and span events.
1212
1. Activate the repository virtual environment:
1313

1414
```bash
15-
source ../../../../.venv/bin/activate
15+
source ../../.venv/bin/activate
1616
```
1717

18-
2. Ensure `openai-agents` is installed in the environment (it is included in
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
1921
the shared development venv for this repository).
2022

2123
## Run the demo
@@ -26,7 +28,7 @@ python main.py
2628

2729
The script will:
2830

29-
- Configure the OpenTelemetry SDK with a console span exporter.
31+
- Configure the OpenTelemetry SDK with an OTLP exporter so spans reach your collector.
3032
- Instrument the OpenAI Agents tracing hooks with content capture enabled.
3133
- Simulate an agent invocation that performs a generation and a tool call.
3234
- Print the resulting spans, attributes, and events (including JSON-encoded
@@ -35,7 +37,6 @@ The script will:
3537
## Customisation tips
3638

3739
- Set `OTEL_SERVICE_NAME` before running to override the default service name.
38-
- Swap the `ConsoleSpanExporter` in `demo.py` for an OTLP exporter if you want
39-
to ship spans to a collector.
40+
- Adjust the OTLP exporter configuration (endpoint, protocol) through `.env`.
4041
- Modify the prompts, tool payloads, or add additional spans in `run_workflow`
4142
to explore different content capture scenarios.
Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,23 @@
1515
from typing import Any
1616

1717
from agents.tracing import agent_span, function_span, generation_span, trace
18+
from dotenv import load_dotenv
1819

20+
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import (
21+
OTLPSpanExporter,
22+
)
1923
from opentelemetry.instrumentation.openai_agents import (
2024
OpenAIAgentsInstrumentor,
2125
)
2226
from opentelemetry.sdk.resources import Resource
2327
from opentelemetry.sdk.trace import TracerProvider
24-
from opentelemetry.sdk.trace.export import (
25-
ConsoleSpanExporter,
26-
SimpleSpanProcessor,
27-
)
28+
from opentelemetry.sdk.trace.export import BatchSpanProcessor
29+
30+
load_dotenv() # take environment variables from .env.
2831

2932

3033
def configure_tracing() -> None:
31-
"""Configure a tracer provider that writes spans to stdout."""
34+
"""Configure a tracer provider that exports spans via OTLP."""
3235
resource = Resource.create(
3336
{
3437
"service.name": os.environ.get(
@@ -37,7 +40,7 @@ def configure_tracing() -> None:
3740
}
3841
)
3942
provider = TracerProvider(resource=resource)
40-
provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
43+
provider.add_span_processor(BatchSpanProcessor(OTLPSpanExporter()))
4144

4245
# Instrument with explicit content capture mode to ensure prompts/responses are recorded.
4346
OpenAIAgentsInstrumentor().instrument(
@@ -72,7 +75,7 @@ def run_workflow() -> None:
7275
}
7376

7477
with trace("travel-booking-workflow"):
75-
with agent_span(operation="invoke", name="travel_planner") as agent:
78+
with agent_span(name="travel_planner") as agent:
7679
dump(
7780
"Agent span started",
7881
{"span_id": agent.span_id, "trace_id": agent.trace_id},
@@ -105,7 +108,9 @@ def run_workflow() -> None:
105108
):
106109
pass
107110

108-
print("\nWorkflow complete – spans exported to console above.")
111+
print(
112+
"\nWorkflow complete – spans exported to the configured OTLP endpoint."
113+
)
109114

110115

111116
def main() -> None:
Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
# Update this with your real OpenAI API key
2-
OPENAI_API_KEY=sk-YOUR_API_KEY
3-
4-
# Uncomment and adjust if you use a non-default OTLP collector endpoint
5-
# OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
6-
# OTEL_EXPORTER_OTLP_PROTOCOL=grpc
7-
8-
OTEL_SERVICE_NAME=opentelemetry-python-openai-agents-manual
9-
10-
# Optionally override the agent name reported on spans
11-
# OTEL_GENAI_AGENT_NAME=Travel Concierge
1+
# Copy to .env and add real values before running main.py
2+
OPENAI_API_KEY=
3+
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
4+
OTEL_EXPORTER_OTLP_PROTOCOL=grpc
5+
OTEL_SERVICE_NAME=openai-agents-manual-demo

instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2/examples/manual/README.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Setup
2222
python3 -m venv .venv
2323
source .venv/bin/activate
2424
pip install "python-dotenv[cli]"
25-
pip install -r requirements.txt
25+
uv pip install -r requirements.txt --prerelease=allow
2626

2727
Run
2828
---
@@ -34,6 +34,8 @@ are applied:
3434

3535
dotenv run -- python main.py
3636

37+
Ensure ``OPENAI_API_KEY`` is present in your environment (or ``.env`` file); the OpenAI client raises ``OpenAIError`` if the key is missing.
38+
3739
The script automatically loads environment variables from ``.env`` so running
3840
``python main.py`` directly also works if the shell already has the required
3941
values exported.
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
openai-agents~=0.3.3
22
python-dotenv~=1.0
33

4-
opentelemetry-sdk~=1.36.0
5-
opentelemetry-exporter-otlp-proto-grpc~=1.36.0
6-
opentelemetry-instrumentation-openai-agents-v2~=0.1.0.dev
4+
opentelemetry-sdk @ git+https://github.com/open-telemetry/opentelemetry-python@main#subdirectory=opentelemetry-sdk
5+
opentelemetry-semantic-conventions @ git+https://github.com/open-telemetry/opentelemetry-python@main#subdirectory=opentelemetry-semantic-conventions
6+
opentelemetry-exporter-otlp-proto-grpc @ git+https://github.com/open-telemetry/opentelemetry-python@main#subdirectory=exporter/opentelemetry-exporter-otlp-proto-grpc
7+
-e ../..
Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,7 @@
1-
# Update this with your real OpenAI API key
2-
OPENAI_API_KEY=sk-YOUR_API_KEY
3-
4-
# Uncomment and adjust if you use a non-default OTLP collector endpoint
5-
# OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
6-
# OTEL_EXPORTER_OTLP_PROTOCOL=grpc
7-
1+
# Copy to .env and add real values before running main.py
2+
OPENAI_API_KEY=
3+
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4317
4+
OTEL_EXPORTER_OTLP_PROTOCOL=grpc
85
OTEL_SERVICE_NAME=opentelemetry-python-openai-agents-zero-code
9-
10-
# Enable auto-instrumentation for logs if desired
116
OTEL_PYTHON_LOGGING_AUTO_INSTRUMENTATION_ENABLED=true
12-
13-
# Optionally override the agent name reported on spans
147
# OTEL_GENAI_AGENT_NAME=Travel Concierge

instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2/examples/zero-code/README.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Setup
2222
python3 -m venv .venv
2323
source .venv/bin/activate
2424
pip install "python-dotenv[cli]"
25-
pip install -r requirements.txt
25+
uv pip install -r requirements.txt --prerelease=allow
2626

2727
Run
2828
---
@@ -34,6 +34,8 @@ instrumentation is activated automatically:
3434

3535
dotenv run -- opentelemetry-instrument python main.py
3636

37+
Ensure ``OPENAI_API_KEY`` is set in your shell or `.env`; the OpenAI client raises ``OpenAIError`` if the key is missing.
38+
3739
Because ``main.py`` invokes ``load_dotenv``, running ``python main.py`` directly
3840
also works when the required environment variables are already exported.
3941

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
openai-agents~=0.3.3
22
python-dotenv~=1.0
33

4-
opentelemetry-sdk~=1.36.0
5-
opentelemetry-exporter-otlp-proto-grpc~=1.36.0
6-
opentelemetry-distro~=0.57b0
7-
opentelemetry-instrumentation-openai-agents-v2~=0.1.0.dev
4+
opentelemetry-sdk @ git+https://github.com/open-telemetry/opentelemetry-python@main#subdirectory=opentelemetry-sdk
5+
opentelemetry-semantic-conventions @ git+https://github.com/open-telemetry/opentelemetry-python@main#subdirectory=opentelemetry-semantic-conventions
6+
opentelemetry-exporter-otlp-proto-grpc @ git+https://github.com/open-telemetry/opentelemetry-python@main#subdirectory=exporter/opentelemetry-exporter-otlp-proto-grpc
7+
-e ../..

instrumentation-genai/opentelemetry-instrumentation-openai-agents-v2/src/opentelemetry/instrumentation/openai_agents/constants.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ class GenAIEvaluationAttributes:
110110
GEN_AI_RESPONSE_FINISH_REASONS = "gen_ai.response.finish_reasons"
111111
GEN_AI_USAGE_INPUT_TOKENS = "gen_ai.usage.input_tokens"
112112
GEN_AI_USAGE_OUTPUT_TOKENS = "gen_ai.usage.output_tokens"
113-
GEN_AI_USAGE_TOTAL_TOKENS = "gen_ai.usage.total_tokens"
114113
GEN_AI_CONVERSATION_ID = "gen_ai.conversation.id"
115114
GEN_AI_AGENT_ID = "gen_ai.agent.id"
116115
GEN_AI_AGENT_NAME = "gen_ai.agent.name"

0 commit comments

Comments
 (0)