-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_agent.py
More file actions
85 lines (64 loc) · 2.47 KB
/
Copy pathtest_agent.py
File metadata and controls
85 lines (64 loc) · 2.47 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
#!/usr/bin/env python3
"""Test script to start Python agent and trigger exceptions."""
import sys
import time
# Add the agent package to path
sys.path.insert(0, '.')
import aivory_monitor
# Initialize agent with local backend using REAL token from database
aivory_monitor.init(
api_key='aiv_mon_ae6dba5c7b6647c58eb1f27c', # Real token for org test_20
backend_url='ws://localhost:8080/ws/monitor/agent',
environment='development',
debug=True,
)
print("\n[Test] Agent initialized. Now triggering some exceptions...\n")
# Give time for connection
time.sleep(2)
def function_that_fails(user_id):
"""Simulate a function that throws an exception."""
users = {"1": "Alice", "2": "Bob"}
return users[user_id] # Will throw KeyError for invalid ID
def process_request(data):
"""Simulate request processing."""
result = data["value"] / data["divisor"] # ZeroDivisionError if divisor=0
return result
# Trigger exception 1: KeyError
print("[Test] Triggering KeyError...")
try:
function_that_fails("999")
except Exception as e:
aivory_monitor.capture_exception(e, context={"request_id": "req-001", "user_action": "get_user"})
print(f"[Test] Captured: {type(e).__name__}: {e}")
time.sleep(1)
# Trigger exception 2: ZeroDivisionError
print("[Test] Triggering ZeroDivisionError...")
try:
process_request({"value": 100, "divisor": 0})
except Exception as e:
aivory_monitor.capture_exception(e, context={"request_id": "req-002", "endpoint": "/api/calculate"})
print(f"[Test] Captured: {type(e).__name__}: {e}")
time.sleep(1)
# Trigger exception 3: AttributeError
print("[Test] Triggering AttributeError...")
try:
obj = None
obj.some_method()
except Exception as e:
aivory_monitor.capture_exception(e, context={"request_id": "req-003", "component": "user_service"})
print(f"[Test] Captured: {type(e).__name__}: {e}")
time.sleep(1)
# Trigger exception 4: ValueError
print("[Test] Triggering ValueError...")
try:
int("not_a_number")
except Exception as e:
aivory_monitor.capture_exception(e, context={"request_id": "req-004", "input_field": "age"})
print(f"[Test] Captured: {type(e).__name__}: {e}")
print("\n[Test] Done! Keeping agent running for 30 seconds to ensure delivery...")
print("[Test] Check the JetBrains plugin - Exceptions and Agents tabs should show data.\n")
# Keep running for a bit to ensure messages are sent
time.sleep(30)
print("[Test] Shutting down agent...")
aivory_monitor.shutdown()
print("[Test] Complete!")