24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/shieldpress_tracker/collectors.rb', line 24
def flush
records = @mutex.synchronize { rows = @records; @records = []; rows }
return nil if records.empty?
now = Process.clock_gettime(Process::CLOCK_MONOTONIC); elapsed = [now - @started, 0.001].max; @started = now
durations, statuses, grouped = records.map(&:last), Hash.new(0), Hash.new { |h, k| h[k] = [] }
records.each { |path, method, status, ms| statuses[status.to_s] += 1; grouped[[method, path]] << [status, ms] }
top = grouped.map do |(method, path), rows|
times = rows.map(&:last)
{ path: path, method: method, count: rows.length, avgMs: (times.sum / times.length).round,
p95Ms: Helpers.percentile(times, 95), errorCount: rows.count { |r| r[0] >= 400 } }
end.sort_by { |x| -x[:count] }.first(20)
{ totalRequests: records.length, totalErrors: records.count { |r| r[2] >= 500 }, avgResponseMs: (durations.sum / durations.length).round,
p50ResponseMs: Helpers.percentile(durations, 50), p95ResponseMs: Helpers.percentile(durations, 95),
p99ResponseMs: Helpers.percentile(durations, 99), maxResponseMs: durations.max, statusCodes: statuses,
topPaths: top, requestsPerSecond: (records.length / elapsed).round(2) }
rescue StandardError
nil
end
|