debug-agent 0.5.1 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +86 -5
- data/lib/debug_agent/inspectors/build_info.rb +87 -0
- data/lib/debug_agent/inspectors/config_inspector.rb +137 -0
- data/lib/debug_agent/inspectors/cpu_profile.rb +192 -0
- data/lib/debug_agent/inspectors/endpoint_test.rb +284 -0
- data/lib/debug_agent/inspectors/feature_flags.rb +215 -0
- data/lib/debug_agent/inspectors/leak_detector.rb +161 -0
- data/lib/debug_agent/inspectors/locks.rb +343 -0
- data/lib/debug_agent/inspectors/migration.rb +150 -0
- data/lib/debug_agent/inspectors/pool_inspector.rb +320 -0
- data/lib/debug_agent/inspectors/service_registry.rb +88 -0
- data/lib/debug_agent/inspectors/snapshot.rb +206 -0
- data/lib/debug_agent/version.rb +1 -1
- data/lib/debug_agent.rb +20 -0
- metadata +12 -1
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
require 'time'
|
|
2
|
+
|
|
3
|
+
module DebugAgent
|
|
4
|
+
# Register connection pools for deep-dive inspection.
|
|
5
|
+
#
|
|
6
|
+
# DebugAgent.register_pool(:primary, ActiveRecord::Base.connection_pool)
|
|
7
|
+
@registered_pools = {}
|
|
8
|
+
@pool_stats = {}
|
|
9
|
+
@pool_meta_lock = Mutex.new
|
|
10
|
+
|
|
11
|
+
class << self
|
|
12
|
+
attr_reader :registered_pools, :pool_stats
|
|
13
|
+
|
|
14
|
+
def register_pool(name, pool)
|
|
15
|
+
pool_name = name.to_s
|
|
16
|
+
@registered_pools[pool_name] = pool
|
|
17
|
+
@pool_meta_lock.synchronize do
|
|
18
|
+
@pool_stats[pool_name] ||= {
|
|
19
|
+
acquire_times: [],
|
|
20
|
+
last_checked_at: nil
|
|
21
|
+
}
|
|
22
|
+
end
|
|
23
|
+
pool
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
class << self
|
|
28
|
+
private
|
|
29
|
+
|
|
30
|
+
def ar_pool_stats
|
|
31
|
+
return nil unless defined?(::ActiveRecord::Base)
|
|
32
|
+
|
|
33
|
+
begin
|
|
34
|
+
pool = ::ActiveRecord::Base.connection_pool
|
|
35
|
+
stats = {}
|
|
36
|
+
|
|
37
|
+
# ActiveRecord::ConnectionPool#stats (Rails 5+)
|
|
38
|
+
if pool.respond_to?(:stats)
|
|
39
|
+
stats = pool.stats
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
result = {
|
|
43
|
+
size: pool.respond_to?(:size) ? pool.size : nil,
|
|
44
|
+
class: pool.class.name,
|
|
45
|
+
stats: stats
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
# Try to get more detail via instance variables if available
|
|
49
|
+
if pool.instance_variable_defined?(:@available)
|
|
50
|
+
available = pool.instance_variable_get(:@available)
|
|
51
|
+
if available.respond_to?(:size)
|
|
52
|
+
result[:available_connections] = available.size
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
if pool.instance_variable_defined?(:@connections)
|
|
57
|
+
connections = pool.instance_variable_get(:@connections)
|
|
58
|
+
if connections.is_a?(Array)
|
|
59
|
+
result[:total_connections] = connections.size
|
|
60
|
+
result[:connections] = connections.map do |conn|
|
|
61
|
+
conn_info(conn)
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
result
|
|
67
|
+
rescue => e
|
|
68
|
+
{ error: "ActiveRecord pool inspection failed: #{e.message}" }
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
def conn_info(conn)
|
|
73
|
+
info = { object_id: conn.object_id, class: conn.class.name }
|
|
74
|
+
if conn.respond_to?(:active?)
|
|
75
|
+
begin
|
|
76
|
+
info[:active] = conn.active?
|
|
77
|
+
rescue
|
|
78
|
+
nil
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
if conn.respond_to?(:in_use?)
|
|
82
|
+
begin
|
|
83
|
+
info[:in_use] = conn.in_use?
|
|
84
|
+
rescue
|
|
85
|
+
nil
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
if conn.respond_to?(:seconds_idle)
|
|
89
|
+
begin
|
|
90
|
+
info[:seconds_idle] = conn.seconds_idle
|
|
91
|
+
rescue
|
|
92
|
+
nil
|
|
93
|
+
end end
|
|
94
|
+
info
|
|
95
|
+
rescue
|
|
96
|
+
{ object_id: conn&.object_id, class: conn&.class&.name }
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def pool_details_for(name, pool)
|
|
100
|
+
if defined?(::ActiveRecord::ConnectionAdapters::AbstractAdapter) &&
|
|
101
|
+
pool.is_a?(::ActiveRecord::ConnectionAdapters::AbstractAdapter)
|
|
102
|
+
# Direct adapter, not a pool
|
|
103
|
+
return {
|
|
104
|
+
name: name,
|
|
105
|
+
class: pool.class.name,
|
|
106
|
+
type: 'adapter',
|
|
107
|
+
active: (pool.active? if pool.respond_to?(:active?)),
|
|
108
|
+
in_use: (pool.in_use? if pool.respond_to?(:in_use?)),
|
|
109
|
+
seconds_idle: (pool.seconds_idle if pool.respond_to?(:seconds_idle))
|
|
110
|
+
}
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
# ActiveRecord ConnectionPool
|
|
114
|
+
if pool.respond_to?(:stats) || pool.respond_to?(:size)
|
|
115
|
+
stats = {}
|
|
116
|
+
stats[:size] = pool.size if pool.respond_to?(:size)
|
|
117
|
+
stats[:stats] = pool.stats if pool.respond_to?(:stats)
|
|
118
|
+
|
|
119
|
+
if pool.instance_variable_defined?(:@connections)
|
|
120
|
+
conns = pool.instance_variable_get(:@connections)
|
|
121
|
+
if conns.is_a?(Array)
|
|
122
|
+
stats[:total_connections] = conns.size
|
|
123
|
+
stats[:connections] = conns.map { |c| conn_info(c) }
|
|
124
|
+
stats[:busy_connections] = conns.count { |c| c.respond_to?(:in_use?) ? c.in_use? : false }
|
|
125
|
+
stats[:idle_connections] = conns.count { |c| c.respond_to?(:in_use?) ? !c.in_use? : true }
|
|
126
|
+
stats[:dead_connections] = conns.count do |c|
|
|
127
|
+
c.respond_to?(:active?) ? !c.active? : false
|
|
128
|
+
rescue
|
|
129
|
+
true
|
|
130
|
+
end
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
return { name: name, class: pool.class.name, type: 'active_record_pool' }.merge(stats)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
# Custom pool — best-effort introspection
|
|
138
|
+
info = { name: name, class: pool.class.name, type: 'custom' }
|
|
139
|
+
%i[size available busy idle dead count total active checkout checkin].each do |m|
|
|
140
|
+
if pool.respond_to?(m)
|
|
141
|
+
begin
|
|
142
|
+
val = pool.public_send(m)
|
|
143
|
+
info[m] = val unless val.nil?
|
|
144
|
+
rescue
|
|
145
|
+
nil
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
info
|
|
150
|
+
end
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
register_tool('get_pool_details',
|
|
154
|
+
'Connection pool deep dive: size, busy, dead, idle connections. ' \
|
|
155
|
+
'Auto-detects ActiveRecord::Base.connection_pool or uses registered pools') do
|
|
156
|
+
pools = []
|
|
157
|
+
|
|
158
|
+
# Registered pools
|
|
159
|
+
registered_pools.each do |name, pool|
|
|
160
|
+
pools << pool_details_for(name, pool)
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
# Auto-detect ActiveRecord pool if not already registered
|
|
164
|
+
if defined?(::ActiveRecord::Base) && registered_pools.none? { |_, p| p == ::ActiveRecord::Base.connection_pool rescue false }
|
|
165
|
+
begin
|
|
166
|
+
ar = ar_pool_stats
|
|
167
|
+
if ar && !ar[:error]
|
|
168
|
+
pools << ar.merge(name: 'active_record_default', source: 'auto_detected')
|
|
169
|
+
end
|
|
170
|
+
rescue
|
|
171
|
+
nil
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
if pools.empty?
|
|
176
|
+
next {
|
|
177
|
+
error: 'No connection pools available. Load ActiveRecord or register a pool ' \
|
|
178
|
+
'with DebugAgent.register_pool(:name, pool).'
|
|
179
|
+
}
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
{
|
|
183
|
+
total_pools: pools.size,
|
|
184
|
+
pools: pools
|
|
185
|
+
}
|
|
186
|
+
rescue => e
|
|
187
|
+
{ error: e.message }
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
register_tool('detect_pool_leaks',
|
|
191
|
+
'Heuristic connection pool leak detection: flags connections checked out ' \
|
|
192
|
+
'for more than 30 seconds (configurable threshold)') do
|
|
193
|
+
threshold_seconds = 30
|
|
194
|
+
leaks = []
|
|
195
|
+
checked_out = []
|
|
196
|
+
|
|
197
|
+
all_pools = registered_pools.dup
|
|
198
|
+
|
|
199
|
+
# Auto-detect ActiveRecord pool
|
|
200
|
+
if defined?(::ActiveRecord::Base)
|
|
201
|
+
begin
|
|
202
|
+
all_pools['active_record_default'] = ::ActiveRecord::Base.connection_pool
|
|
203
|
+
rescue
|
|
204
|
+
nil
|
|
205
|
+
end
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
all_pools.each do |name, pool|
|
|
209
|
+
pool_info = { name: name, connections: [], leak_count: 0 }
|
|
210
|
+
|
|
211
|
+
# ActiveRecord ConnectionPool — access connections via instance variable
|
|
212
|
+
if pool.instance_variable_defined?(:@connections)
|
|
213
|
+
conns = pool.instance_variable_get(:@connections)
|
|
214
|
+
if conns.is_a?(Array)
|
|
215
|
+
conns.each do |conn|
|
|
216
|
+
conn_data = conn_info(conn)
|
|
217
|
+
|
|
218
|
+
# Check if connection has been checked out for too long
|
|
219
|
+
if conn.respond_to?(:in_use?) && conn.in_use?
|
|
220
|
+
idle_secs = (conn.respond_to?(:seconds_idle) ? conn.seconds_idle : nil)
|
|
221
|
+
conn_data[:status] = 'checked_out'
|
|
222
|
+
conn_data[:idle_seconds] = idle_secs
|
|
223
|
+
checked_out << { pool: name }.merge(conn_data)
|
|
224
|
+
|
|
225
|
+
if idle_secs && idle_secs > threshold_seconds
|
|
226
|
+
conn_data[:leak_suspect] = true
|
|
227
|
+
conn_data[:leak_reason] = "Checked out for #{idle_secs}s (threshold: #{threshold_seconds}s)"
|
|
228
|
+
pool_info[:leak_count] += 1
|
|
229
|
+
end
|
|
230
|
+
end
|
|
231
|
+
|
|
232
|
+
pool_info[:connections] << conn_data
|
|
233
|
+
end
|
|
234
|
+
end
|
|
235
|
+
end
|
|
236
|
+
|
|
237
|
+
leaks << pool_info if pool_info[:connections].any?
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
total_leaks = leaks.sum { |p| p[:leak_count] }
|
|
241
|
+
|
|
242
|
+
{
|
|
243
|
+
threshold_seconds: threshold_seconds,
|
|
244
|
+
total_leak_suspects: total_leaks,
|
|
245
|
+
pools_inspected: leaks.size,
|
|
246
|
+
checked_out_connections: checked_out.size,
|
|
247
|
+
pools: leaks,
|
|
248
|
+
recommendation: if total_leaks > 0
|
|
249
|
+
"#{total_leaks} potential connection leak(s) detected. " \
|
|
250
|
+
"Check for missing connection.release or unclosed transactions."
|
|
251
|
+
elsif checked_out.any?
|
|
252
|
+
"#{checked_out.size} connection(s) checked out but within normal threshold."
|
|
253
|
+
else
|
|
254
|
+
'No connection leaks detected.'
|
|
255
|
+
end
|
|
256
|
+
}
|
|
257
|
+
rescue => e
|
|
258
|
+
{ error: e.message }
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
register_tool('get_pool_wait_stats',
|
|
262
|
+
'Connection pool acquisition wait time statistics: how long threads wait ' \
|
|
263
|
+
'to acquire a database connection from the pool') do
|
|
264
|
+
all_pools = registered_pools.dup
|
|
265
|
+
|
|
266
|
+
# Auto-detect ActiveRecord pool
|
|
267
|
+
if defined?(::ActiveRecord::Base)
|
|
268
|
+
begin
|
|
269
|
+
all_pools['active_record_default'] = ::ActiveRecord::Base.connection_pool
|
|
270
|
+
rescue
|
|
271
|
+
nil
|
|
272
|
+
end
|
|
273
|
+
end
|
|
274
|
+
|
|
275
|
+
pool_stats = all_pools.map do |name, pool|
|
|
276
|
+
stats = { name: name, class: pool.class.name }
|
|
277
|
+
|
|
278
|
+
# ActiveRecord connection pool metrics
|
|
279
|
+
if pool.respond_to?(:stats)
|
|
280
|
+
begin
|
|
281
|
+
raw = pool.stats
|
|
282
|
+
stats[:raw_stats] = raw
|
|
283
|
+
stats[:size] = raw[:size] if raw.is_a?(Hash) && raw[:size]
|
|
284
|
+
stats[:busy] = raw[:busy] if raw.is_a?(Hash) && raw[:busy]
|
|
285
|
+
stats[:dead] = raw[:dead] if raw.is_a?(Hash) && raw[:dead]
|
|
286
|
+
stats[:idle] = raw[:idle] if raw.is_a?(Hash) && raw[:idle]
|
|
287
|
+
stats[:waiting] = raw[:waiting] if raw.is_a?(Hash) && raw[:waiting]
|
|
288
|
+
rescue
|
|
289
|
+
nil
|
|
290
|
+
end
|
|
291
|
+
end
|
|
292
|
+
|
|
293
|
+
# Size info
|
|
294
|
+
stats[:pool_size] = pool.size if pool.respond_to?(:size)
|
|
295
|
+
|
|
296
|
+
# Count threads waiting (approximate: scan for threads blocked on connection checkout)
|
|
297
|
+
waiting_threads = Thread.list.select do |t|
|
|
298
|
+
next false unless t.alive? && t.status == 'sleep'
|
|
299
|
+
bt = begin
|
|
300
|
+
t.backtrace || []
|
|
301
|
+
rescue
|
|
302
|
+
[]
|
|
303
|
+
end
|
|
304
|
+
bt.any? { |line| line =~ /connection_pool|with_connection|checkout|acquire/i }
|
|
305
|
+
end
|
|
306
|
+
stats[:threads_waiting_for_connection] = waiting_threads.size
|
|
307
|
+
stats[:waiting_thread_ids] = waiting_threads.map(&:object_id)
|
|
308
|
+
|
|
309
|
+
stats
|
|
310
|
+
end
|
|
311
|
+
|
|
312
|
+
{
|
|
313
|
+
total_pools: pool_stats.size,
|
|
314
|
+
total_waiting_threads: pool_stats.sum { |p| p[:threads_waiting_for_connection] || 0 },
|
|
315
|
+
pools: pool_stats
|
|
316
|
+
}
|
|
317
|
+
rescue => e
|
|
318
|
+
{ error: e.message }
|
|
319
|
+
end
|
|
320
|
+
end
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
module DebugAgent
|
|
2
|
+
register_tool('get_registered_services',
|
|
3
|
+
'List all registered debug agent tools grouped by inspector category. ' \
|
|
4
|
+
'Shows tool count and names per group.') do
|
|
5
|
+
all_tools = registry.names
|
|
6
|
+
|
|
7
|
+
# Categorize tools by known inspector groups
|
|
8
|
+
categories = {
|
|
9
|
+
'runtime' => %w[get_gc_stats get_memory_summary trigger_gc get_thread_summary get_runtime_info get_object_allocations],
|
|
10
|
+
'gc' => %w[],
|
|
11
|
+
'object_space' => %w[],
|
|
12
|
+
'threads' => %w[],
|
|
13
|
+
'process' => %w[],
|
|
14
|
+
'system' => %w[],
|
|
15
|
+
'http_tracker' => %w[],
|
|
16
|
+
'routes' => %w[],
|
|
17
|
+
'redis' => %w[],
|
|
18
|
+
'rails' => %w[],
|
|
19
|
+
'sidekiq' => %w[],
|
|
20
|
+
'puma' => %w[],
|
|
21
|
+
'logging' => %w[],
|
|
22
|
+
'cache' => %w[],
|
|
23
|
+
'http_client' => %w[],
|
|
24
|
+
'metrics' => %w[],
|
|
25
|
+
'active_record_stats' => %w[],
|
|
26
|
+
'faraday' => %w[],
|
|
27
|
+
'concurrent' => %w[],
|
|
28
|
+
'security' => %w[],
|
|
29
|
+
'health' => %w[],
|
|
30
|
+
'scheduler' => %w[],
|
|
31
|
+
'error_tracking' => %w[],
|
|
32
|
+
'websocket' => %w[],
|
|
33
|
+
'locks' => %w[],
|
|
34
|
+
'migration' => %w[],
|
|
35
|
+
'config' => %w[],
|
|
36
|
+
'feature_flags' => %w[],
|
|
37
|
+
'endpoint_test' => %w[],
|
|
38
|
+
'pool_inspector' => %w[],
|
|
39
|
+
'cpu_profile' => %w[],
|
|
40
|
+
'leak_detector' => %w[],
|
|
41
|
+
'build_info' => %w[],
|
|
42
|
+
'snapshot' => %w[],
|
|
43
|
+
'service_registry' => %w[]
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
# Since we cannot reliably categorize at runtime without a lookup table,
|
|
47
|
+
# we list all tools alphabetically with their descriptions
|
|
48
|
+
tools_with_desc = all_tools.sort.map do |name|
|
|
49
|
+
tool = registry.get(name)
|
|
50
|
+
{
|
|
51
|
+
name: name,
|
|
52
|
+
description: tool&.respond_to?(:description) ? tool.description : nil
|
|
53
|
+
}
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
{
|
|
57
|
+
total_tools: all_tools.size,
|
|
58
|
+
tools: tools_with_desc
|
|
59
|
+
}
|
|
60
|
+
rescue => e
|
|
61
|
+
{ error: e.message }
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
register_tool('get_service_dependencies',
|
|
65
|
+
'Show all loaded gems and their versions from Gem.loaded_specs') do
|
|
66
|
+
specs = Gem.loaded_specs.values.sort_by(&:name)
|
|
67
|
+
|
|
68
|
+
gems = specs.map do |spec|
|
|
69
|
+
{
|
|
70
|
+
name: spec.name,
|
|
71
|
+
version: spec.version.to_s,
|
|
72
|
+
loaded_from: spec.loaded_from,
|
|
73
|
+
dependencies: spec.dependencies.map { |d| "#{d.name} (#{d.requirement})" }
|
|
74
|
+
}
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
{
|
|
78
|
+
total_gems: gems.size,
|
|
79
|
+
ruby_version: RUBY_VERSION,
|
|
80
|
+
ruby_engine: RUBY_ENGINE,
|
|
81
|
+
rubygems_version: Gem::VERSION,
|
|
82
|
+
bundler_version: defined?(Bundler) ? Bundler::VERSION : nil,
|
|
83
|
+
gems: gems
|
|
84
|
+
}
|
|
85
|
+
rescue => e
|
|
86
|
+
{ error: e.message }
|
|
87
|
+
end
|
|
88
|
+
end
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
require 'objspace'
|
|
2
|
+
|
|
3
|
+
module DebugAgent
|
|
4
|
+
# Snapshot & Diff inspector. Collects metrics across all inspectors at a
|
|
5
|
+
# point in time and allows comparing snapshots to detect changes.
|
|
6
|
+
@metric_snapshots = {}
|
|
7
|
+
@metric_snapshot_counter = 0
|
|
8
|
+
|
|
9
|
+
class << self
|
|
10
|
+
attr_reader :metric_snapshots
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
register_tool('take_snapshot',
|
|
14
|
+
'Take a cross-inspector snapshot: thread count, fiber count, memory (RSS), ' \
|
|
15
|
+
'GC stats, object count, DB pool stats, cache stats, error count. Returns snapshot ID.') do
|
|
16
|
+
gc = GC.stat
|
|
17
|
+
obj_counts = ObjectSpace.count_objects
|
|
18
|
+
rss = `ps -o rss= -p #{Process.pid}`.to_i
|
|
19
|
+
|
|
20
|
+
metrics = {
|
|
21
|
+
taken_at: Time.now,
|
|
22
|
+
threads: Thread.list.size,
|
|
23
|
+
alive_threads: Thread.list.count(&:alive?),
|
|
24
|
+
fiber_count: Fiber.respond_to?(:list) ? Fiber.list.size : count_fibers,
|
|
25
|
+
rss_mb: (rss / 1024.0).round(2),
|
|
26
|
+
gc: {
|
|
27
|
+
count: gc[:count],
|
|
28
|
+
major_gc_count: gc[:major_gc_count],
|
|
29
|
+
minor_gc_count: gc[:minor_gc_count],
|
|
30
|
+
heap_live_slots: gc[:heap_live_slots],
|
|
31
|
+
heap_free_slots: gc[:heap_free_slots],
|
|
32
|
+
total_allocated_objects: gc[:total_allocated_objects],
|
|
33
|
+
total_freed_objects: gc[:total_freed_objects],
|
|
34
|
+
old_objects: gc[:old_objects]
|
|
35
|
+
},
|
|
36
|
+
objects: {
|
|
37
|
+
total: obj_counts[:TOTAL] || obj_counts.values.sum,
|
|
38
|
+
free_slots: obj_counts[:FREE] || 0,
|
|
39
|
+
t_string: obj_counts[:T_STRING] || 0,
|
|
40
|
+
t_array: obj_counts[:T_ARRAY] || 0,
|
|
41
|
+
t_hash: obj_counts[:T_HASH] || 0,
|
|
42
|
+
t_object: obj_counts[:T_OBJECT] || 0,
|
|
43
|
+
t_data: obj_counts[:T_DATA] || 0
|
|
44
|
+
},
|
|
45
|
+
db_pool: gather_db_pool_stats,
|
|
46
|
+
cache: gather_cache_stats,
|
|
47
|
+
error_count: gather_error_count
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
@metric_snapshot_counter += 1
|
|
51
|
+
snapshot_id = "snap-#{@metric_snapshot_counter}"
|
|
52
|
+
@metric_snapshots[snapshot_id] = metrics
|
|
53
|
+
|
|
54
|
+
{
|
|
55
|
+
snapshot_id: snapshot_id,
|
|
56
|
+
taken_at: metrics[:taken_at].iso8601,
|
|
57
|
+
summary: {
|
|
58
|
+
threads: metrics[:threads],
|
|
59
|
+
fibers: metrics[:fiber_count],
|
|
60
|
+
rss_mb: metrics[:rss_mb],
|
|
61
|
+
total_objects: metrics[:objects][:total],
|
|
62
|
+
gc_count: metrics[:gc][:count],
|
|
63
|
+
live_slots: metrics[:gc][:heap_live_slots]
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
rescue => e
|
|
67
|
+
{ error: e.message }
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
register_tool('compare_snapshots',
|
|
71
|
+
'Compare two cross-inspector snapshots. Returns all changed values ' \
|
|
72
|
+
'with deltas.',
|
|
73
|
+
snapshot_a: { type: 'string', description: 'First snapshot ID', required: true },
|
|
74
|
+
snapshot_b: { type: 'string', description: 'Second snapshot ID', required: true }) do |snapshot_a:, snapshot_b:|
|
|
75
|
+
snap_a = @metric_snapshots[snapshot_a.to_s]
|
|
76
|
+
snap_b = @metric_snapshots[snapshot_b.to_s]
|
|
77
|
+
|
|
78
|
+
next { error: "Snapshot '#{snapshot_a}' not found. Available: #{@metric_snapshots.keys.join(', ')}" } unless snap_a
|
|
79
|
+
next { error: "Snapshot '#{snapshot_b}' not found. Available: #{@metric_snapshots.keys.join(', ')}" } unless snap_b
|
|
80
|
+
|
|
81
|
+
changes = compute_snapshot_diff(snap_a, snap_b, '')
|
|
82
|
+
|
|
83
|
+
{
|
|
84
|
+
snapshot_a: snapshot_a,
|
|
85
|
+
snapshot_b: snapshot_b,
|
|
86
|
+
time_between_seconds: (snap_b[:taken_at] - snap_a[:taken_at]).round(2),
|
|
87
|
+
changes: changes
|
|
88
|
+
}
|
|
89
|
+
rescue => e
|
|
90
|
+
{ error: e.message }
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
register_tool('list_snapshots',
|
|
94
|
+
'List all stored cross-inspector snapshots') do
|
|
95
|
+
if @metric_snapshots.empty?
|
|
96
|
+
next { message: 'No snapshots taken yet. Call take_snapshot first.', count: 0, snapshots: [] }
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
{
|
|
100
|
+
count: @metric_snapshots.size,
|
|
101
|
+
snapshots: @metric_snapshots.map do |id, snap|
|
|
102
|
+
{
|
|
103
|
+
snapshot_id: id,
|
|
104
|
+
taken_at: snap[:taken_at].iso8601,
|
|
105
|
+
threads: snap[:threads],
|
|
106
|
+
rss_mb: snap[:rss_mb],
|
|
107
|
+
total_objects: snap[:objects][:total],
|
|
108
|
+
gc_count: snap[:gc][:count]
|
|
109
|
+
}
|
|
110
|
+
end
|
|
111
|
+
}
|
|
112
|
+
rescue => e
|
|
113
|
+
{ error: e.message }
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# --- Helpers ---
|
|
117
|
+
|
|
118
|
+
class << self
|
|
119
|
+
private
|
|
120
|
+
|
|
121
|
+
def count_fibers
|
|
122
|
+
# Best-effort fiber count via ObjectSpace
|
|
123
|
+
count = 0
|
|
124
|
+
ObjectSpace.each_object(Fiber) { count += 1 }
|
|
125
|
+
count
|
|
126
|
+
rescue
|
|
127
|
+
0
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def gather_db_pool_stats
|
|
131
|
+
return nil unless defined?(::ActiveRecord::Base)
|
|
132
|
+
|
|
133
|
+
begin
|
|
134
|
+
pool = ::ActiveRecord::Base.connection_pool
|
|
135
|
+
stats = pool.respond_to?(:stats) ? pool.stats : {}
|
|
136
|
+
{
|
|
137
|
+
size: pool.respond_to?(:size) ? pool.size : nil,
|
|
138
|
+
stats: stats
|
|
139
|
+
}
|
|
140
|
+
rescue
|
|
141
|
+
nil
|
|
142
|
+
end
|
|
143
|
+
end
|
|
144
|
+
|
|
145
|
+
def gather_cache_stats
|
|
146
|
+
return nil unless defined?(::Rails)
|
|
147
|
+
|
|
148
|
+
begin
|
|
149
|
+
cache = ::Rails.cache
|
|
150
|
+
return nil unless cache
|
|
151
|
+
stats = if cache.respond_to?(:stats)
|
|
152
|
+
cache.stats
|
|
153
|
+
elsif cache.respond_to?(:info)
|
|
154
|
+
cache.info
|
|
155
|
+
else
|
|
156
|
+
{}
|
|
157
|
+
end
|
|
158
|
+
{ backend: cache.class.name, stats: stats }
|
|
159
|
+
rescue
|
|
160
|
+
nil
|
|
161
|
+
end
|
|
162
|
+
end
|
|
163
|
+
|
|
164
|
+
def gather_error_count
|
|
165
|
+
# ErrorTracking inspector stores error counts if available
|
|
166
|
+
if defined?(@error_log) && @error_log.is_a?(Array)
|
|
167
|
+
@error_log.size
|
|
168
|
+
else
|
|
169
|
+
0
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def compute_snapshot_diff(a, b, prefix)
|
|
174
|
+
changes = []
|
|
175
|
+
|
|
176
|
+
if a.is_a?(Hash) && b.is_a?(Hash)
|
|
177
|
+
all_keys = (a.keys | b.keys)
|
|
178
|
+
all_keys.each do |key|
|
|
179
|
+
va = a[key]
|
|
180
|
+
vb = b[key]
|
|
181
|
+
path = prefix.empty? ? key.to_s : "#{prefix}.#{key}"
|
|
182
|
+
|
|
183
|
+
if va.is_a?(Hash) && vb.is_a?(Hash)
|
|
184
|
+
changes.concat(compute_snapshot_diff(va, vb, path))
|
|
185
|
+
elsif va != vb
|
|
186
|
+
changes << {
|
|
187
|
+
metric: path,
|
|
188
|
+
before: va,
|
|
189
|
+
after: vb,
|
|
190
|
+
delta: (va.is_a?(Numeric) && vb.is_a?(Numeric)) ? (vb - va) : nil
|
|
191
|
+
}
|
|
192
|
+
end
|
|
193
|
+
end
|
|
194
|
+
elsif a != b
|
|
195
|
+
changes << {
|
|
196
|
+
metric: prefix,
|
|
197
|
+
before: a,
|
|
198
|
+
after: b,
|
|
199
|
+
delta: (a.is_a?(Numeric) && b.is_a?(Numeric)) ? (b - a) : nil
|
|
200
|
+
}
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
changes
|
|
204
|
+
end
|
|
205
|
+
end
|
|
206
|
+
end
|
data/lib/debug_agent/version.rb
CHANGED
data/lib/debug_agent.rb
CHANGED
|
@@ -35,10 +35,30 @@ require_relative 'debug_agent/inspectors/health'
|
|
|
35
35
|
require_relative 'debug_agent/inspectors/scheduler'
|
|
36
36
|
require_relative 'debug_agent/inspectors/error_tracking'
|
|
37
37
|
require_relative 'debug_agent/inspectors/websocket'
|
|
38
|
+
require_relative 'debug_agent/inspectors/locks'
|
|
39
|
+
require_relative 'debug_agent/inspectors/migration'
|
|
40
|
+
require_relative 'debug_agent/inspectors/config_inspector'
|
|
41
|
+
require_relative 'debug_agent/inspectors/feature_flags'
|
|
42
|
+
require_relative 'debug_agent/inspectors/endpoint_test'
|
|
43
|
+
require_relative 'debug_agent/inspectors/pool_inspector'
|
|
44
|
+
require_relative 'debug_agent/inspectors/cpu_profile'
|
|
45
|
+
require_relative 'debug_agent/inspectors/leak_detector'
|
|
46
|
+
require_relative 'debug_agent/inspectors/build_info'
|
|
47
|
+
require_relative 'debug_agent/inspectors/snapshot'
|
|
48
|
+
require_relative 'debug_agent/inspectors/service_registry'
|
|
38
49
|
|
|
39
50
|
module DebugAgent
|
|
40
51
|
class Error < StandardError; end
|
|
41
52
|
|
|
53
|
+
# ggcode promotional message — shown once on first require
|
|
54
|
+
unless defined?(@_ggcode_banner_shown)
|
|
55
|
+
@_ggcode_banner_shown = true
|
|
56
|
+
puts ""
|
|
57
|
+
puts " This package was built with ggcode — an AI coding assistant."
|
|
58
|
+
puts " https://github.com/topcheer/ggcode"
|
|
59
|
+
puts ""
|
|
60
|
+
end
|
|
61
|
+
|
|
42
62
|
# Process start time for uptime tracking
|
|
43
63
|
PROCESS_START_TIME = Time.now
|
|
44
64
|
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: debug-agent
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.7.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- ggcode
|
|
@@ -165,18 +165,27 @@ files:
|
|
|
165
165
|
- lib/debug_agent/context_compressor.rb
|
|
166
166
|
- lib/debug_agent/engine.rb
|
|
167
167
|
- lib/debug_agent/inspectors/active_record_stats.rb
|
|
168
|
+
- lib/debug_agent/inspectors/build_info.rb
|
|
168
169
|
- lib/debug_agent/inspectors/cache.rb
|
|
169
170
|
- lib/debug_agent/inspectors/concurrent.rb
|
|
171
|
+
- lib/debug_agent/inspectors/config_inspector.rb
|
|
170
172
|
- lib/debug_agent/inspectors/core_ext.rb
|
|
173
|
+
- lib/debug_agent/inspectors/cpu_profile.rb
|
|
174
|
+
- lib/debug_agent/inspectors/endpoint_test.rb
|
|
171
175
|
- lib/debug_agent/inspectors/error_tracking.rb
|
|
172
176
|
- lib/debug_agent/inspectors/faraday.rb
|
|
177
|
+
- lib/debug_agent/inspectors/feature_flags.rb
|
|
173
178
|
- lib/debug_agent/inspectors/gc.rb
|
|
174
179
|
- lib/debug_agent/inspectors/health.rb
|
|
175
180
|
- lib/debug_agent/inspectors/http_client.rb
|
|
176
181
|
- lib/debug_agent/inspectors/http_tracker.rb
|
|
182
|
+
- lib/debug_agent/inspectors/leak_detector.rb
|
|
183
|
+
- lib/debug_agent/inspectors/locks.rb
|
|
177
184
|
- lib/debug_agent/inspectors/logging.rb
|
|
178
185
|
- lib/debug_agent/inspectors/metrics.rb
|
|
186
|
+
- lib/debug_agent/inspectors/migration.rb
|
|
179
187
|
- lib/debug_agent/inspectors/object_space.rb
|
|
188
|
+
- lib/debug_agent/inspectors/pool_inspector.rb
|
|
180
189
|
- lib/debug_agent/inspectors/process_info.rb
|
|
181
190
|
- lib/debug_agent/inspectors/puma.rb
|
|
182
191
|
- lib/debug_agent/inspectors/rails.rb
|
|
@@ -185,7 +194,9 @@ files:
|
|
|
185
194
|
- lib/debug_agent/inspectors/runtime.rb
|
|
186
195
|
- lib/debug_agent/inspectors/scheduler.rb
|
|
187
196
|
- lib/debug_agent/inspectors/security.rb
|
|
197
|
+
- lib/debug_agent/inspectors/service_registry.rb
|
|
188
198
|
- lib/debug_agent/inspectors/sidekiq.rb
|
|
199
|
+
- lib/debug_agent/inspectors/snapshot.rb
|
|
189
200
|
- lib/debug_agent/inspectors/system.rb
|
|
190
201
|
- lib/debug_agent/inspectors/threads.rb
|
|
191
202
|
- lib/debug_agent/inspectors/websocket.rb
|