-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_app.rb
More file actions
108 lines (89 loc) · 2.59 KB
/
Copy pathtest_app.rb
File metadata and controls
108 lines (89 loc) · 2.59 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
#!/usr/bin/env ruby
# frozen_string_literal: true
# AIVory Ruby Agent Test Application
#
# Generates various exception types to test exception capture and local variable extraction.
#
# Usage:
# cd monitor-agents/agent-ruby
# bundle install
# AIVORY_API_KEY=test-key-123 AIVORY_BACKEND_URL=ws://localhost:19999/api/monitor/agent/v1 AIVORY_DEBUG=true ruby test_app.rb
require_relative 'lib/aivory_monitor'
class UserContext
attr_reader :user_id, :email, :active
def initialize(user_id, email, active: true)
@user_id = user_id
@email = email
@active = active
end
def to_s
"UserContext(user_id='#{user_id}', email='#{email}', active=#{active})"
end
end
def trigger_exception(iteration)
# Create some local variables to capture
test_var = "test-value-#{iteration}"
count = iteration * 10
items = %w[apple banana cherry]
metadata = {
iteration: iteration,
timestamp: Time.now.to_i,
nested: { key: 'value', count: count }
}
user = UserContext.new("user-#{iteration}", 'test@example.com')
case iteration
when 0
# NoMethodError (like NullPointerException)
puts 'Triggering NoMethodError...'
nil_value = nil
nil_value.upcase # NoMethodError here
when 1
# ArgumentError
puts 'Triggering ArgumentError...'
raise ArgumentError, "Invalid argument: test_var=#{test_var}"
when 2
# IndexError (like ArrayIndexOutOfBoundsException)
puts 'Triggering IndexError...'
arr = [1, 2, 3]
arr.fetch(10) # IndexError here
else
raise RuntimeError, "Unknown iteration: #{iteration}"
end
end
puts '==========================================='
puts 'AIVory Ruby Agent Test Application'
puts '==========================================='
# Initialize the agent
AIVoryMonitor.init(
debug: ENV.fetch('AIVORY_DEBUG', 'false').downcase == 'true'
)
# Set user context
AIVoryMonitor.set_user(
id: 'test-user-001',
email: 'tester@example.com',
username: 'tester'
)
# Wait for agent to connect
puts 'Waiting for agent to connect...'
sleep 3
puts "Starting exception tests...\n\n"
# Generate test exceptions
3.times do |i|
puts "--- Test #{i + 1} ---"
begin
trigger_exception(i)
rescue StandardError => e
puts "Caught: #{e.class} - #{e.message}"
# Also manually capture for testing
AIVoryMonitor.capture_exception(e, { test_iteration: i })
end
puts
sleep 3
end
puts '==========================================='
puts 'Test complete. Check database for exceptions.'
puts '==========================================='
# Keep running briefly to allow final messages to send
sleep 2
# Shutdown cleanly
AIVoryMonitor.shutdown