-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_observability_demo.py
More file actions
753 lines (591 loc) Β· 25.4 KB
/
Copy pathagent_observability_demo.py
File metadata and controls
753 lines (591 loc) Β· 25.4 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
"""
agent_observability_demo.py
Demo: Agent Observability Framework - Monitoring, Tracing, and Metrics
This demo provides comprehensive observability features for AI agents including:
- Structured Logging: JSON-formatted logs with timestamps and levels
- Metrics Collection: Request latency, token usage, error rates
- Distributed Tracing: Track agent decisions and tool executions
- Health Monitoring: Agent health checks and status tracking
- Alerting: Configurable alerts for anomalies
Flow:
1. Initialize ObservabilityManager with logging, metrics, and tracing
2. Instrument the agent with decorators/wrappers
3. Execute agent tasks with full visibility
4. View collected metrics and traces
Usage:
- pip install requests
- Set OLLAMA_HOST, OLLAMA_MODEL (or OPENAI_API_KEY)
- python agent_observability_demo.py
"""
import os
import json
import time
import traceback
from typing import Optional, Any, Callable
from datetime import datetime
from dataclasses import dataclass, field
from enum import Enum
from collections import defaultdict
from contextlib import contextmanager
import threading
try:
import requests
OLLAMA_AVAILABLE = True
except ImportError:
OLLAMA_AVAILABLE = False
# ============================================================================
# Observability Enums and Data Classes
# ============================================================================
class LogLevel(Enum):
"""Log levels for observability."""
DEBUG = "DEBUG"
INFO = "INFO"
WARNING = "WARNING"
ERROR = "ERROR"
CRITICAL = "CRITICAL"
class MetricType(Enum):
"""Types of metrics collected."""
COUNTER = "counter"
GAUGE = "gauge"
HISTOGRAM = "histogram"
TIMER = "timer"
class TraceEventType(Enum):
"""Types of trace events."""
AGENT_START = "agent_start"
AGENT_END = "agent_end"
LLM_CALL = "llm_call"
TOOL_CALL = "tool_call"
TOOL_RESULT = "tool_result"
ERROR = "error"
RETRY = "retry"
@dataclass
class LogEntry:
"""Structured log entry."""
timestamp: str
level: str
message: str
context: dict = field(default_factory=dict)
def to_json(self) -> str:
return json.dumps({
"timestamp": self.timestamp,
"level": self.level,
"message": self.message,
"context": self.context
})
@dataclass
class MetricValue:
"""Metric value with metadata."""
name: str
value: float
metric_type: MetricType
labels: dict = field(default_factory=dict)
timestamp: str = field(default_factory=lambda: datetime.now().isoformat())
@dataclass
class TraceSpan:
"""Represents a single trace span."""
trace_id: str
span_id: str
parent_span_id: Optional[str]
operation_name: str
start_time: float
end_time: Optional[float] = None
tags: dict = field(default_factory=dict)
events: list = field(default_factory=list)
status: str = "ok"
def duration_ms(self) -> float:
if self.end_time:
return (self.end_time - self.start_time) * 1000
return 0.0
# ============================================================================
# Core Observability Components
# ============================================================================
class StructuredLogger:
"""Structured JSON logger for agent observability."""
def __init__(self, name: str = "agent", log_level: LogLevel = LogLevel.INFO):
self.name = name
self.log_level = log_level
self.handlers = []
self._lock = threading.Lock()
def _should_log(self, level: LogLevel) -> bool:
levels = [LogLevel.DEBUG, LogLevel.INFO, LogLevel.WARNING, LogLevel.ERROR, LogLevel.CRITICAL]
return levels.index(level) >= levels.index(self.log_level)
def log(self, level: LogLevel, message: str, **context) -> None:
"""Log a structured message."""
if not self._should_log(level):
return
entry = LogEntry(
timestamp=datetime.now().isoformat(),
level=level.value,
message=message,
context={
"service": self.name,
**context
}
)
with self._lock:
for handler in self.handlers:
handler(entry)
# Also print to console
print(f"[{level.value}] {message}")
if context:
print(f" Context: {json.dumps(context, indent=4)}")
def debug(self, message: str, **context) -> None:
self.log(LogLevel.DEBUG, message, **context)
def info(self, message: str, **context) -> None:
self.log(LogLevel.INFO, message, **context)
def warning(self, message: str, **context) -> None:
self.log(LogLevel.WARNING, message, **context)
def error(self, message: str, **context) -> None:
self.log(LogLevel.ERROR, message, **context)
def critical(self, message: str, **context) -> None:
self.log(LogLevel.CRITICAL, message, **context)
def add_handler(self, handler: Callable[[LogEntry], None]) -> None:
"""Add a log handler."""
self.handlers.append(handler)
class MetricsCollector:
"""Collects and aggregates metrics for the agent."""
def __init__(self):
self.counters: dict[str, float] = defaultdict(float)
self.gauges: dict[str, float] = {}
self.histograms: dict[str, list[float]] = defaultdict(list)
self._lock = threading.Lock()
def increment(self, name: str, value: float = 1.0, labels: dict = None) -> None:
"""Increment a counter metric."""
key = self._make_key(name, labels)
with self._lock:
self.counters[key] += value
def gauge(self, name: str, value: float, labels: dict = None) -> None:
"""Set a gauge metric."""
key = self._make_key(name, labels)
with self._lock:
self.gauges[key] = value
def histogram(self, name: str, value: float, labels: dict = None) -> None:
"""Record a histogram value."""
key = self._make_key(name, labels)
with self._lock:
self.histograms[key].append(value)
def timer(self, name: str) -> Callable:
"""Context manager for timing operations."""
return self._TimerContext(name, self)
def _make_key(self, name: str, labels: dict = None) -> str:
if not labels:
return name
label_str = ",".join(f"{k}={v}" for k, v in sorted(labels.items()))
return f"{name}{{{label_str}}}"
def get_stats(self) -> dict:
"""Get current metrics statistics."""
stats = {}
with self._lock:
# Compute counter stats
stats["counters"] = dict(self.counters)
# Compute gauge stats
stats["gauges"] = dict(self.gauges)
# Compute histogram stats
hist_stats = {}
for name, values in self.histograms.items():
if values:
hist_stats[name] = {
"count": len(values),
"min": min(values),
"max": max(values),
"avg": sum(values) / len(values),
"sum": sum(values)
}
stats["histograms"] = hist_stats
return stats
def reset(self) -> None:
"""Reset all metrics."""
with self._lock:
self.counters.clear()
self.gauges.clear()
self.histograms.clear()
class _TimerContext:
def __init__(self, name: str, collector: 'MetricsCollector'):
self.name = name
self.collector = collector
self.start_time = None
def __enter__(self):
self.start_time = time.time()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
duration_ms = (time.time() - self.start_time) * 1000
self.collector.histogram(self.name, duration_ms)
class DistributedTracer:
"""Distributed tracing for agent operations."""
def __init__(self):
self.spans: dict[str, TraceSpan] = {}
self._lock = threading.Lock()
self._span_counter = 0
def _generate_span_id(self) -> str:
self._span_counter += 1
return f"span-{self._span_counter:06d}"
def start_span(
self,
trace_id: str,
operation_name: str,
parent_span_id: Optional[str] = None,
tags: dict = None
) -> TraceSpan:
"""Start a new trace span."""
span = TraceSpan(
trace_id=trace_id,
span_id=self._generate_span_id(),
parent_span_id=parent_span_id,
operation_name=operation_name,
start_time=time.time(),
tags=tags or {}
)
with self._lock:
self.spans[span.span_id] = span
return span
def end_span(self, span_id: str, status: str = "ok", tags: dict = None) -> None:
"""End a trace span."""
with self._lock:
if span_id in self.spans:
span = self.spans[span_id]
span.end_time = time.time()
span.status = status
if tags:
span.tags.update(tags)
def add_event(self, span_id: str, event_name: str, tags: dict = None) -> None:
"""Add an event to a span."""
with self._lock:
if span_id in self.spans:
self.spans[span_id].events.append({
"name": event_name,
"timestamp": time.time(),
"tags": tags or {}
})
def get_trace(self, trace_id: str) -> list[TraceSpan]:
"""Get all spans for a trace."""
with self._lock:
return [s for s in self.spans.values() if s.trace_id == trace_id]
def get_all_traces(self) -> dict[str, list[TraceSpan]]:
"""Get all traces."""
traces = defaultdict(list)
with self._lock:
for span in self.spans.values():
traces[span.trace_id].append(span)
return dict(traces)
class HealthMonitor:
"""Monitors agent health and provides status."""
def __init__(self):
self.checks: dict[str, Callable[[], bool]] = {}
self.status = "healthy"
self.last_check_time = None
self._lock = threading.Lock()
def register_check(self, name: str, check_fn: Callable[[], bool]) -> None:
"""Register a health check."""
self.checks[name] = check_fn
def run_checks(self) -> dict:
"""Run all health checks."""
results = {}
all_healthy = True
with self._lock:
self.last_check_time = datetime.now().isoformat()
for name, check_fn in self.checks.items():
try:
healthy = check_fn()
results[name] = {"status": "healthy" if healthy else "unhealthy"}
if not healthy:
all_healthy = False
except Exception as e:
results[name] = {"status": "error", "error": str(e)}
all_healthy = False
self.status = "healthy" if all_healthy else "unhealthy"
return results
# ============================================================================
# Observability Manager - Main Entry Point
# ============================================================================
class ObservabilityManager:
"""
Central manager for all observability components.
Provides unified interface for logging, metrics, tracing, and health monitoring.
"""
def __init__(
self,
service_name: str = "agent",
log_level: LogLevel = LogLevel.INFO,
enable_tracing: bool = True,
enable_metrics: bool = True
):
self.service_name = service_name
self.logger = StructuredLogger(service_name, log_level)
self.metrics = MetricsCollector()
self.tracer = DistributedTracer() if enable_tracing else None
self.health = HealthMonitor()
# Counters for important events
self.total_requests = 0
self.total_errors = 0
# Setup default log handler
self.logger.add_handler(self._default_log_handler)
# Register default health checks
self._setup_default_health_checks()
def _default_log_handler(self, entry: LogEntry) -> None:
"""Default log handler that could write to file, etc."""
pass # Console output handled in StructuredLogger.log
def _setup_default_health_checks(self) -> None:
"""Setup default health checks."""
self.health.register_check("logging", lambda: True)
self.health.register_check("metrics", lambda: len(self.metrics.gauges) >= 0)
@contextmanager
def trace_operation(
self,
operation_name: str,
trace_id: str = None,
parent_span_id: str = None,
tags: dict = None
):
"""Context manager for tracing operations."""
if not self.tracer:
yield
return
trace_id = trace_id or f"trace-{int(time.time() * 1000)}"
span = self.tracer.start_span(trace_id, operation_name, parent_span_id, tags)
try:
yield span
except Exception as e:
self.tracer.end_span(span.span_id, status="error", tags={"error": str(e)})
self.error(f"Operation failed: {operation_name}", error=str(e))
raise
else:
self.tracer.end_span(span.span_id, status="ok")
def track_request(self, labels: dict = None):
"""Context manager for tracking request metrics."""
return _RequestTracker(self, labels)
def log(self, level: LogLevel, message: str, **context) -> None:
"""Log a message."""
self.logger.log(level, message, **context)
def debug(self, message: str, **context) -> None:
self.logger.debug(message, **context)
def info(self, message: str, **context) -> None:
self.logger.info(message, **context)
def warning(self, message: str, **context) -> None:
self.logger.warning(message, **context)
def error(self, message: str, **context) -> None:
self.logger.error(message, **context)
self.total_errors += 1
self.metrics.increment("agent_errors_total", labels=context.get("labels", {}))
def critical(self, message: str, **context) -> None:
self.logger.critical(message, **context)
def get_dashboard_summary(self) -> dict:
"""Get summary for dashboard display."""
stats = self.metrics.get_stats()
health_results = self.health.run_checks()
return {
"service": self.service_name,
"status": self.health.status,
"metrics": stats,
"health": health_results,
"uptime": self.total_requests,
"error_rate": self.total_errors / max(self.total_requests, 1)
}
class _RequestTracker:
"""Context manager for tracking request metrics."""
def __init__(self, observability: ObservabilityManager, labels: dict = None):
self.obs = observability
self.labels = labels or {}
self.start_time = None
def __enter__(self):
self.start_time = time.time()
self.obs.total_requests += 1
self.obs.metrics.increment("agent_requests_total", labels=self.labels)
return self
def __exit__(self, exc_type, exc_val, exc_tb):
duration_ms = (time.time() - self.start_time) * 1000
self.obs.metrics.histogram("agent_request_duration_ms", duration_ms, self.labels)
if exc_type:
self.obs.metrics.increment("agent_request_errors_total", labels=self.labels)
# ============================================================================
# Observable Agent Wrapper
# ============================================================================
class ObservableAgent:
"""Wraps an agent with observability capabilities."""
def __init__(self, agent: Any, observability: ObservabilityManager):
self.agent = agent
self.obs = observability
def run(self, *args, **kwargs):
"""Run agent with full observability."""
trace_id = f"trace-{int(time.time() * 1000)}"
with self.obs.trace_operation("agent.run", trace_id, tags={"agent": type(self.agent).__name__}):
with self.obs.track_request(labels={"agent": type(self.agent).__name__}):
self.obs.info("Starting agent execution", trace_id=trace_id)
try:
result = self.agent.run(*args, **kwargs)
self.obs.info("Agent execution completed", trace_id=trace_id)
return result
except Exception as e:
self.obs.error(
"Agent execution failed",
trace_id=trace_id,
error=str(e),
error_type=type(e).__name__
)
raise
# ============================================================================
# Demo - Observable LLM Client
# ============================================================================
class ObservableLLMClient:
"""LLM client with observability instrumentation."""
def __init__(self, provider: str = "ollama", observability: ObservabilityManager = None):
self.provider = provider
self.obs = observability or ObservabilityManager(provider)
if provider == "ollama" and OLLAMA_AVAILABLE:
self.client = None
else:
self.client = None
def generate(self, prompt: str, **kwargs) -> dict:
"""Generate with full observability."""
with self.obs.trace_operation("llm.generate") as span:
span.tags.update({
"provider": self.provider,
"prompt_length": len(prompt)
})
self.obs.info("LLM request started", prompt_length=len(prompt))
try:
with self.obs.metrics.timer("llm_response_time") as timer:
# Simulate LLM call
time.sleep(0.1) # Simulate API latency
result = {
"content": f"Response to: {prompt[:50]}...",
"tokens_used": len(prompt.split()) * 2
}
# Record metrics
self.obs.metrics.increment("llm_requests_total")
self.obs.metrics.histogram("llm_tokens_used", result["tokens_used"])
span.tags.update({
"status": "success",
"tokens_used": result["tokens_used"]
})
self.obs.info("LLM request completed", tokens=result["tokens_used"])
return result
except Exception as e:
self.obs.error("LLM request failed", error=str(e))
span.tags["status"] = "error"
raise
# ============================================================================
# Demo Execution
# ============================================================================
def run_observability_demo():
"""Run the observability demo."""
print("\n" + "="*60)
print("π AGENT OBSERVABILITY DEMO")
print("="*60)
# Initialize observability manager
obs = ObservabilityManager(
service_name="demo-agent",
log_level=LogLevel.INFO,
enable_tracing=True,
enable_metrics=True
)
# Add custom log handler
def file_log_handler(entry: LogEntry):
# In production, this would write to a file or log aggregation service
pass
obs.logger.add_handler(file_log_handler)
# =========================================================================
# Demo 1: Structured Logging
# =========================================================================
print("\n--- Demo 1: Structured Logging ---")
obs.info("Agent initialized", version="1.0.0", model="llama3.2")
obs.info("Processing request", request_id="req-123", user_id="user-456")
obs.warning("Rate limit approaching", current=95, limit=100)
obs.error("Request failed", request_id="req-123", error="Timeout")
obs.debug("Debug information", details={"key": "value"})
# =========================================================================
# Demo 2: Metrics Collection
# =========================================================================
print("\n--- Demo 2: Metrics Collection ---")
# Simulate multiple requests
for i in range(5):
with obs.track_request(labels={"endpoint": "chat", "status": "success"}):
time.sleep(0.05)
# Track different types of metrics
obs.metrics.increment("users_active_total", labels={"region": "us-east"})
obs.metrics.increment("users_active_total", labels={"region": "us-west"})
obs.metrics.gauge("memory_usage_mb", 256.5)
obs.metrics.histogram("request_size_bytes", 1024)
obs.metrics.histogram("request_size_bytes", 2048)
# Get metrics stats
stats = obs.metrics.get_stats()
print("\nπ Metrics Collected:")
print(f" Counters: {stats['counters']}")
print(f" Gauges: {stats['gauges']}")
print(f" Histograms: {stats['histograms']}")
# =========================================================================
# Demo 3: Distributed Tracing
# =========================================================================
print("\n--- Demo 3: Distributed Tracing ---")
trace_id = f"trace-{int(time.time() * 1000)}"
with obs.trace_operation("process_request", trace_id, tags={"user": "demo"}) as parent_span:
parent_span.tags["priority"] = "high"
# Sub-operation: Authentication
with obs.trace_operation("authenticate", trace_id, parent_span.span_id) as auth_span:
time.sleep(0.05)
auth_span.tags["method"] = "jwt"
# Sub-operation: LLM Processing
with obs.trace_operation("llm_processing", trace_id, parent_span.span_id) as llm_span:
time.sleep(0.1)
llm_span.tags["model"] = "llama3.2"
llm_span.tags["tokens"] = 150
# Sub-operation: Response formatting
with obs.trace_operation("format_response", trace_id, parent_span.span_id):
time.sleep(0.02)
# Get trace details
trace = obs.tracer.get_trace(trace_id)
print(f"\nπ Trace '{trace_id}' spans:")
for span in trace:
print(f" - {span.operation_name}: {span.duration_ms():.2f}ms [{span.status}]")
# =========================================================================
# Demo 4: Observable LLM Client
# =========================================================================
print("\n--- Demo 4: Observable LLM Client ---")
llm = ObservableLLMClient("ollama", obs)
response = llm.generate("What is the meaning of life?")
print(f" Response: {response['content']}")
# =========================================================================
# Demo 5: Health Monitoring
# =========================================================================
print("\n--- Demo 5: Health Monitoring ---")
# Register custom health check
obs.health.register_check("database", lambda: True)
obs.health.register_check("api", lambda: True)
health_results = obs.health.run_checks()
print(f"\nπ Health Status: {obs.health.status}")
for check, result in health_results.items():
print(f" - {check}: {result['status']}")
# =========================================================================
# Demo 6: Dashboard Summary
# =========================================================================
print("\n--- Demo 6: Dashboard Summary ---")
summary = obs.get_dashboard_summary()
print(f"\nπ Dashboard Summary:")
print(f" Service: {summary['service']}")
print(f" Status: {summary['status']}")
print(f" Total Requests: {summary['uptime']}")
print(f" Error Rate: {summary['error_rate']:.2%}")
print(f" Metrics:")
for metric_type, values in summary['metrics'].items():
if values:
print(f" {metric_type}: {json.dumps(values)[:100]}...")
# =========================================================================
# Final Summary
# =========================================================================
print("\n" + "="*60)
print("β
OBSERVABILITY DEMO COMPLETE")
print("="*60)
print("""
This demo showcased:
1. Structured Logging - JSON-formatted logs with context
2. Metrics Collection - Counters, gauges, histograms
3. Distributed Tracing - Trace spans with parent-child relationships
4. Health Monitoring - Custom health checks
5. Dashboard Summary - Unified metrics overview
All of these can be integrated with:
- Prometheus/Grafana for metrics visualization
- Jaeger/Zipkin for distributed tracing
- ELK Stack for log aggregation
- Custom alerting systems
""")
if __name__ == "__main__":
run_observability_demo()