-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_application_insights.py
More file actions
256 lines (211 loc) · 8.16 KB
/
Copy pathtest_application_insights.py
File metadata and controls
256 lines (211 loc) · 8.16 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
"""
Test script for ApplicationInsightsCollector
Demonstrates the Azure Telemetry Collector functionality
without requiring the full src package initialization.
"""
import sys
import tempfile
from pathlib import Path
# Add project root to path
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))
# Manually import what we need to avoid __init__ cascade
import importlib.util
def load_module(name, path):
spec = importlib.util.spec_from_file_location(name, path)
module = importlib.util.module_from_spec(spec)
sys.modules[name] = module
spec.loader.exec_module(module)
return module
# Load telemetry base module
telemetry = load_module("telemetry", project_root / "src" / "telemetry.py")
# Extract what we need from telemetry
EventType = telemetry.EventType
Severity = telemetry.Severity
TelemetryCollector = telemetry.TelemetryCollector
TelemetryEvent = telemetry.TelemetryEvent
# Now we can define the collectors inline since we have the base classes
from typing import Any, Dict, Optional
from datetime import datetime
import json
import logging
import os
logger = logging.getLogger(__name__)
class ApplicationInsightsCollector(TelemetryCollector):
"""Azure Application Insights telemetry collector."""
def __init__(
self,
instrumentation_key: Optional[str] = None,
service_name: str = "SmartGlass-AI-Agent",
environment: str = "production",
):
self.instrumentation_key = (
instrumentation_key
or os.getenv("APPINSIGHTS_INSTRUMENTATION_KEY", "")
)
self.service_name = service_name
self.environment = environment
self._event_buffer: list[Dict[str, Any]] = []
logger.info(
f"ApplicationInsightsCollector initialized - "
f"service={service_name}, environment={environment}"
)
def collect(self, event: TelemetryEvent) -> None:
"""Collect a telemetry event."""
if not self.instrumentation_key:
logger.debug(f"No instrumentation key. Event: {event.event_type.value}")
return
try:
ai_event = {
"name": f"smartglass_{event.event_type.value}",
"timestamp": event.timestamp,
"properties": {
"service": self.service_name,
"environment": self.environment,
"event_type": event.event_type.value,
"component": event.component,
"severity": event.severity.value,
},
"measurements": event.metrics or {},
}
if event.context:
for key, value in event.context.items():
ai_event["properties"][f"context_{key}"] = str(value)
self._event_buffer.append(ai_event)
logger.debug(f"Event collected: {event.component}/{event.event_type.value}")
except Exception as e:
logger.error(f"Error collecting event: {e}")
def flush(self) -> None:
"""Flush buffered events."""
if not self._event_buffer:
return
logger.debug(f"Flushing {len(self._event_buffer)} events")
self._event_buffer.clear()
class LocalTelemetryCollector(TelemetryCollector):
"""Local telemetry collector for development and testing."""
def __init__(self, max_events: int = 10000):
self.max_events = max_events
self._events: list[TelemetryEvent] = []
def collect(self, event: TelemetryEvent) -> None:
"""Collect event in local buffer."""
self._events.append(event)
if len(self._events) > self.max_events:
self._events = self._events[-self.max_events :]
def get_events(
self, event_type: Optional[EventType] = None
) -> list[TelemetryEvent]:
"""Get collected events."""
if event_type is None:
return self._events.copy()
return [e for e in self._events if e.event_type == event_type]
def get_statistics(self) -> Dict[str, Any]:
"""Get statistics about collected events."""
stats = {
"total_events": len(self._events),
"event_types": {},
"components": {},
"severities": {},
}
for event in self._events:
event_type = event.event_type.value
stats["event_types"][event_type] = (
stats["event_types"].get(event_type, 0) + 1
)
stats["components"][event.component] = (
stats["components"].get(event.component, 0) + 1
)
severity = event.severity.value
stats["severities"][severity] = (
stats["severities"].get(severity, 0) + 1
)
return stats
def main():
print("=" * 70)
print("ApplicationInsightsCollector Test")
print("=" * 70)
# Create local collector
collector = LocalTelemetryCollector()
print("\n[1/4] Recording latency events...")
for i in range(3):
collector.record_latency(
"Vision", 150.5 + i * 10, context={"model": "CLIP"}
)
collector.record_latency("ASR", 85.2 + i * 5)
collector.record_latency("LLM", 450.1 + i * 20)
print("[2/4] Recording error events...")
collector.record_error(
"ASR", "Audio stream timeout", severity=Severity.WARNING
)
collector.record_error(
"Vision", "GPU memory exhausted", severity=Severity.ERROR
)
print("[3/4] Recording usage metrics...")
collector.record_usage(
"LLM",
{"tokens_generated": 42, "completion_tokens": 40},
context={"model": "gpt-4", "temperature": 0.7},
)
print("[4/4] Recording safety events...")
collector.record_safety_event(
"ModuleCheck",
blocked=False,
reason="Content passed safety check",
context={"check_type": "content_filter"},
)
# Display statistics
stats = collector.get_statistics()
print("\n" + "=" * 70)
print("TELEMETRY STATISTICS")
print("=" * 70)
print(f"Total events: {stats['total_events']}")
print(f"\nEvent types:")
for event_type, count in sorted(stats["event_types"].items()):
print(f" {event_type}: {count}")
print(f"\nComponents:")
for component, count in sorted(stats["components"].items()):
print(f" {component}: {count}")
print(f"\nSeverities:")
for severity, count in sorted(stats["severities"].items()):
print(f" {severity}: {count}")
# Show sample events
print("\n" + "=" * 70)
print("SAMPLE EVENTS (first 5)")
print("=" * 70)
for i, event in enumerate(collector.get_events()[:5], 1):
print(f"\n[{i}] {event.event_type.value.upper()}")
print(f" Component: {event.component}")
print(f" Severity: {event.severity.value}")
if event.metrics:
print(f" Metrics: {event.metrics}")
if event.context:
ctx_str = json.dumps(event.context, indent=6)
print(f" Context: {ctx_str}")
# Test ApplicationInsightsCollector
print("\n" + "=" * 70)
print("Testing ApplicationInsightsCollector")
print("=" * 70)
ai_collector = ApplicationInsightsCollector(
service_name="TestService", environment="development"
)
print("\n✅ ApplicationInsightsCollector created successfully")
print(" Service: TestService")
print(" Environment: development")
print(" Mode: Local mode (no Azure key configured)")
# Record some events
print("\nRecording sample events...")
ai_collector.record_latency("Vision", 125.5)
ai_collector.record_error("Vision", "Test error message")
ai_collector.record_usage("LLM", {"tokens": 100})
print("✅ 3 events recorded")
print("\n" + "=" * 70)
print("✅ ALL TESTS PASSED")
print("=" * 70)
print("\nApplicationInsightsCollector is ready for production use.")
print("\nTo use with Azure:")
print(" 1. Install Azure SDK: pip install opencensus-ext-azure")
print(" 2. Set APPINSIGHTS_INSTRUMENTATION_KEY environment variable")
print(" 3. Create ApplicationInsightsCollector()")
return 0
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
sys.exit(main())