Skip to content

Commit 1c4ced3

Browse files
committed
examples: reorganize samples and collectors; remove azure monitor from most; add enhanced_travel_planner and trace_collectors; update README
1 parent 303c1ff commit 1c4ced3

File tree

9 files changed

+121
-1193
lines changed

9 files changed

+121
-1193
lines changed

instrumentation-genai/opentelemetry-instrumentation-openai-agents/examples/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@ This directory contains examples of how to use the OpenAI Agents instrumentation
44

55
## Files
66

7-
- `quickstart.py` - Minimal OpenTelemetry setup and OpenAI chat sample using `OpenAIAgentsInstrumentor`
8-
- `basic_usage.py` - Basic example showing how to enable and use the instrumentation
9-
- `.env.example` - Example environment variables configuration
7+
- `quickstart.py` - Minimal OpenTelemetry setup and OpenAI chat sample.
8+
- `basic_usage.py` - Tool-calling airline demo (no Azure Monitor).
9+
- `mcp_hotel_capture_all.py` - MCP HTTP hotel finder sample (no Azure Monitor).
10+
- `enhanced_travel_planner.py` - Complex orchestration travel planner (console exporter).
11+
- `trace_collectors.py` - Showcase of Console, OTLP gRPC/HTTP, and Azure Monitor exporters.
12+
- `.env.example` - Example environment variables configuration.
1013

1114
## Basic Usage
1215

@@ -27,10 +30,7 @@ This directory contains examples of how to use the OpenAI Agents instrumentation
2730

2831
## Configuration
2932

30-
The instrumentation can be configured using environment variables:
31-
32-
- `OTEL_INSTRUMENTATION_OPENAI_AGENTS_CAPTURE_CONTENT` - Enable/disable content capture (default: false)
33-
- `OTEL_INSTRUMENTATION_OPENAI_AGENTS_CAPTURE_METRICS` - Enable/disable metrics collection (default: true)
33+
Content, metrics, and events are captured by default.
3434

3535
## Integration with Agent Frameworks
3636

instrumentation-genai/opentelemetry-instrumentation-openai-agents/examples/basic_usage.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
trace,
2424
)
2525
from agents.extensions.handoff_prompt import RECOMMENDED_PROMPT_PREFIX
26-
from azure.monitor.opentelemetry import configure_azure_monitor
2726
from dotenv import load_dotenv
2827
from openai import AsyncAzureOpenAI
2928
from pydantic import BaseModel
@@ -34,9 +33,6 @@
3433

3534
load_dotenv()
3635

37-
configure_azure_monitor(
38-
connection_string=os.environ["APPLICATION_INSIGHTS_CONNECTION_STRING"]
39-
)
4036

4137
OpenAIAgentsInstrumentor().instrument()
4238

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,19 @@
99
from typing import Any, Dict, List, Optional
1010
from uuid import uuid4
1111

12+
# Azure Monitor exporter removed for this sample
13+
from agents import (
14+
Agent,
15+
HandoffInputData,
16+
OpenAIChatCompletionsModel,
17+
Runner,
18+
function_tool,
19+
handoff,
20+
)
21+
from agents import (
22+
trace as agents_trace,
23+
)
24+
from agents.extensions import handoff_filters
1225
from dotenv import load_dotenv
1326
from openai import AsyncAzureOpenAI, AsyncOpenAI
1427
from pydantic import BaseModel, Field
@@ -27,24 +40,6 @@
2740
ConsoleSpanExporter,
2841
)
2942

30-
try:
31-
from azure.monitor.opentelemetry.exporter import AzureMonitorTraceExporter
32-
except ImportError:
33-
AzureMonitorTraceExporter = None
34-
35-
from agents import (
36-
Agent,
37-
HandoffInputData,
38-
OpenAIChatCompletionsModel,
39-
Runner,
40-
function_tool,
41-
handoff,
42-
)
43-
from agents import (
44-
trace as agents_trace,
45-
)
46-
from agents.extensions import handoff_filters
47-
4843
TRACER = trace.get_tracer(__name__)
4944

5045
# Agent metadata constants
@@ -112,10 +107,6 @@ class CompleteTravelPlan(BaseModel):
112107

113108
def _configure_otel(session_id: str) -> GenAISemanticProcessor:
114109
"""Configure OpenTelemetry with GenAI semantic processor."""
115-
conn = os.getenv("APPLICATIONINSIGHTS_CONNECTION_STRING") or os.getenv(
116-
"APPLICATION_INSIGHTS_CONNECTION_STRING"
117-
)
118-
119110
resource = Resource.create(
120111
{
121112
"service.name": "openai-agents-enhanced-travel-planner",
@@ -129,15 +120,7 @@ def _configure_otel(session_id: str) -> GenAISemanticProcessor:
129120
)
130121

131122
tp = TracerProvider(resource=resource)
132-
133-
if conn and AzureMonitorTraceExporter:
134-
tp.add_span_processor(
135-
BatchSpanProcessor(
136-
AzureMonitorTraceExporter.from_connection_string(conn)
137-
)
138-
)
139-
else:
140-
tp.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))
123+
tp.add_span_processor(BatchSpanProcessor(ConsoleSpanExporter()))
141124

142125
trace.set_tracer_provider(tp)
143126

@@ -166,31 +149,19 @@ def _configure_otel(session_id: str) -> GenAISemanticProcessor:
166149
tracer=tracer,
167150
event_logger=None,
168151
system_name=provider,
169-
include_sensitive_data=os.getenv(
170-
"OTEL_GENAI_INCLUDE_SENSITIVE", "false"
171-
).lower()
172-
== "true",
152+
include_sensitive_data=True,
173153
base_url=base_url,
174-
emit_legacy=os.getenv("OTEL_GENAI_EMIT_LEGACY", "false").lower()
175-
== "true",
176-
agent_name=os.getenv("OTEL_GENAI_AGENT_NAME", AGENT_SYSTEM_NAME),
177-
agent_id=os.getenv("OTEL_GENAI_AGENT_ID", agent_id),
178-
agent_description=os.getenv(
179-
"OTEL_GENAI_AGENT_DESCRIPTION",
180-
"Multi-agent travel planning system with 6 specialized agents: Travel Coordinator, Flight Specialist, Hotel Specialist, Activity Specialist, Budget Analyst, and Plan Synthesizer",
154+
emit_legacy=False,
155+
agent_name=AGENT_SYSTEM_NAME,
156+
agent_id=agent_id,
157+
agent_description=(
158+
"Multi-agent travel planning system with specialized agents: "
159+
"Coordinator, Flight Specialist, Hotel Specialist, Activity Specialist, Budget Analyst, and Plan Synthesizer"
181160
),
182161
server_address=None,
183162
server_port=None,
184163
)
185164

186-
if os.getenv("ENABLE_GENAI_CAPTURE_ALL", "false").lower() == "true":
187-
os.environ["OTEL_GENAI_CAPTURE_SYSTEM_INSTRUCTIONS"] = "true"
188-
os.environ["OTEL_GENAI_CAPTURE_MESSAGES"] = "true"
189-
os.environ["OTEL_GENAI_CAPTURE_TOOL_DEFINITIONS"] = "true"
190-
191-
if os.getenv("OTEL_GENAI_METRICS_ENABLED", "true").lower() == "true":
192-
os.environ["OTEL_GENAI_METRICS_ENABLED"] = "true"
193-
194165
return genai_processor
195166

196167

instrumentation-genai/opentelemetry-instrumentation-openai-agents/examples/mcp_hotel_capture_all.py

Lines changed: 7 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import os
88
from dataclasses import dataclass
99
from typing import Callable
10-
from urllib.parse import urlparse
1110

1211
import azure.identity
1312
import openai
@@ -48,34 +47,8 @@ class _ApiConfig:
4847

4948

5049
def _set_capture_env(provider: str, base_url: str) -> None:
51-
"""Enable all GenAI capture toggles before instrumentation hooks."""
52-
53-
capture_defaults = {
54-
"OTEL_INSTRUMENTATION_OPENAI_AGENTS_CAPTURE_CONTENT": "true",
55-
"OTEL_INSTRUMENTATION_OPENAI_AGENTS_CAPTURE_METRICS": "true",
56-
"OTEL_GENAI_CAPTURE_MESSAGES": "true",
57-
"OTEL_GENAI_CAPTURE_SYSTEM_INSTRUCTIONS": "true",
58-
"OTEL_GENAI_CAPTURE_TOOL_DEFINITIONS": "true",
59-
"OTEL_GENAI_EMIT_OPERATION_DETAILS": "true",
60-
"OTEL_GENAI_AGENT_NAME": os.getenv(
61-
"OTEL_GENAI_AGENT_NAME", "MCP Hotel Assistant"
62-
),
63-
"OTEL_GENAI_AGENT_DESCRIPTION": os.getenv(
64-
"OTEL_GENAI_AGENT_DESCRIPTION",
65-
"Agent orchestrator that uses MCP to find hotels matching user criteria",
66-
),
67-
"OTEL_GENAI_AGENT_ID": os.getenv(
68-
"OTEL_GENAI_AGENT_ID", "mcp-hotel-assistant"
69-
),
70-
}
71-
for env_key, value in capture_defaults.items():
72-
os.environ.setdefault(env_key, value)
73-
74-
parsed = urlparse(base_url)
75-
if parsed.hostname:
76-
os.environ.setdefault("OTEL_GENAI_SERVER_ADDRESS", parsed.hostname)
77-
if parsed.port:
78-
os.environ.setdefault("OTEL_GENAI_SERVER_PORT", str(parsed.port))
50+
"""Deprecated: capture is always on; keep for backward compatibility."""
51+
return
7952

8053

8154
def _resolve_api_config() -> _ApiConfig:
@@ -126,70 +99,24 @@ def _build_client() -> openai.AsyncAzureOpenAI:
12699
provider="azure.ai.openai",
127100
)
128101

129-
if host == "ollama":
130-
base_url = os.getenv(
131-
"OLLAMA_BASE_URL", "http://localhost:11434/v1"
132-
).rstrip("/")
133-
model_name = os.getenv("OLLAMA_MODEL", "llama3.1:latest")
134-
api_key = os.getenv("OLLAMA_API_KEY", "none")
135-
136-
def _build_client() -> openai.AsyncOpenAI:
137-
return openai.AsyncOpenAI(base_url=base_url, api_key=api_key)
138-
139-
return _ApiConfig(
140-
build_client=_build_client,
141-
model_name=model_name,
142-
base_url=base_url,
143-
provider="self.hosted",
144-
)
102+
# Removed ollama path to simplify sample
145103

146104
raise ValueError(f"Unsupported API_HOST '{host}'")
147105

148106

149107
def _configure_otel() -> None:
150-
"""Configure the tracer provider and exporters."""
151-
152-
conn = os.getenv("APPLICATION_INSIGHTS_CONNECTION_STRING")
108+
"""Configure tracer provider with console exporter (no Azure Monitor)."""
153109
resource = Resource.create(
154110
{
155111
"service.name": "mcp-hotel-finder-service",
156112
"service.namespace": "mcp-orchestration",
157113
"service.version": os.getenv("SERVICE_VERSION", "1.0.0"),
158114
}
159115
)
160-
161116
tracer_provider = TracerProvider(resource=resource)
162-
163-
if conn:
164-
try:
165-
from azure.monitor.opentelemetry.exporter import ( # type: ignore import-not-found
166-
AzureMonitorTraceExporter,
167-
)
168-
except ImportError: # pragma: no cover - optional dependency
169-
print(
170-
"Warning: Azure Monitor exporter not installed. "
171-
"Install with: pip install azure-monitor-opentelemetry-exporter",
172-
)
173-
tracer_provider.add_span_processor(
174-
BatchSpanProcessor(ConsoleSpanExporter())
175-
)
176-
else:
177-
tracer_provider.add_span_processor(
178-
BatchSpanProcessor(
179-
AzureMonitorTraceExporter.from_connection_string(conn)
180-
)
181-
)
182-
print("[otel] Azure Monitor trace exporter configured")
183-
else:
184-
tracer_provider.add_span_processor(
185-
BatchSpanProcessor(ConsoleSpanExporter())
186-
)
187-
print("[otel] Console span exporter configured")
188-
print(
189-
"[otel] Set APPLICATION_INSIGHTS_CONNECTION_STRING to export to "
190-
"Application Insights instead of the console",
191-
)
192-
117+
tracer_provider.add_span_processor(
118+
BatchSpanProcessor(ConsoleSpanExporter())
119+
)
193120
trace.set_tracer_provider(tracer_provider)
194121

195122

0 commit comments

Comments
 (0)