forked from openai/openai-agents-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprometheus_endpoint.py
More file actions
194 lines (153 loc) · 5.7 KB
/
prometheus_endpoint.py
File metadata and controls
194 lines (153 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
"""Example: Prometheus metrics endpoint for agent monitoring.
This example shows how to set up a FastAPI server with a /metrics endpoint
that exposes Prometheus metrics for your agents.
To run:
pip install 'openai-agents[prometheus]' fastapi uvicorn
uv run python examples/metrics/prometheus_endpoint.py
Then open http://localhost:8000/metrics in your browser or configure
Prometheus to scrape http://localhost:8000/metrics
"""
from __future__ import annotations
import asyncio
import time
import random
from contextlib import asynccontextmanager
from fastapi import FastAPI
from prometheus_client import make_asgi_app
from agents import Agent, Runner
from agents.metrics import PrometheusMetrics, MetricsHooks, enable_metrics
metrics = PrometheusMetrics()
enable_metrics(metrics)
metrics_app = make_asgi_app()
agent = Agent(
name="math_assistant",
instructions="You are a helpful math assistant. Solve simple math problems.",
)
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Lifespan context manager for startup/shutdown."""
print("Starting server with metrics enabled...")
print("Visit http://localhost:8000/metrics for Prometheus metrics")
yield
print("Shutting down...")
app = FastAPI(title="Agent Metrics Example", lifespan=lifespan)
app.mount("/metrics", metrics_app)
@app.get("/")
async def root():
"""Root endpoint with instructions."""
return {
"message": "Agent Metrics Example",
"endpoints": {
"/": "This help message",
"/metrics": "Prometheus metrics endpoint",
"/solve/{problem}": "Solve a math problem (generates metrics)",
"/chat/{message}": "Chat with the agent (generates metrics)",
},
}
@app.get("/solve/{problem}")
async def solve(problem: str):
"""Solve a math problem and record metrics."""
hooks = MetricsHooks()
start_time = time.monotonic()
try:
result = await Runner.run(
agent,
f"Solve this math problem: {problem}",
hooks=[hooks],
)
duration = time.monotonic() - start_time
return {
"problem": problem,
"solution": result.final_output,
"duration_seconds": round(duration, 3),
}
except Exception as e:
duration = time.monotonic() - start_time
return {
"problem": problem,
"error": str(e),
"duration_seconds": round(duration, 3),
}
@app.get("/chat/{message}")
async def chat(message: str):
"""Chat with the agent and record metrics."""
hooks = MetricsHooks()
try:
result = await Runner.run(
agent,
message,
hooks=[hooks],
)
return {
"message": message,
"response": result.final_output,
"usage": {
"input_tokens": result.usage.input_tokens if result.usage else 0,
"output_tokens": result.usage.output_tokens if result.usage else 0,
"total_tokens": result.usage.total_tokens if result.usage else 0,
},
}
except Exception as e:
return {
"message": message,
"error": str(e),
}
@app.post("/generate-load")
async def generate_load(count: int = 10):
"""Generate load for testing metrics (simulated)."""
results = []
for i in range(count):
operation = random.choice(["add", "multiply", "divide", "subtract"])
a, b = random.randint(1, 100), random.randint(1, 100)
latency = random.uniform(0.1, 2.0)
tokens_in = random.randint(50, 500)
tokens_out = random.randint(20, 200)
metrics.record_llm_call(
latency=latency,
tokens_in=tokens_in,
tokens_out=tokens_out,
model="gpt-4",
)
if random.random() < 0.1:
error_type = random.choice(["RateLimitError", "TimeoutError", "APIError"])
metrics.record_error(error_type, agent.name or "unknown")
results.append(
{
"operation": operation,
"error": error_type,
}
)
else:
results.append(
{
"operation": operation,
"a": a,
"b": b,
"latency": round(latency, 3),
}
)
await asyncio.sleep(0.01)
return {
"generated": count,
"results": results,
}
if __name__ == "__main__":
import uvicorn
print("""
Endpoints:
• http://localhost:8000/ - API documentation
• http://localhost:8000/metrics - Prometheus metrics
• http://localhost:8000/solve/{x} - Solve math problem
• http://localhost:8000/chat/{msg} - Chat with agent
• POST /generate-load?count=10 - Generate test load
Metrics available:
• agents_llm_latency_seconds - LLM call latency
• agents_tokens_total - Token usage
• agents_errors_total - Error counts
• agents_runs_total - Run counts
• agents_run_duration_seconds - Run duration
• agents_turns_total - LLM turns
• agents_tool_executions_total - Tool executions
• agents_tool_latency_seconds - Tool latency
""")
uvicorn.run(app, host="0.0.0.0", port=8000)