Class: Perfm::Middleware::SidekiqGvlInstrumentation

Inherits:
Object
  • Object
show all
Defined in:
lib/perfm/middleware/sidekiq_gvl_instrumentation.rb

Instance Method Summary collapse

Instance Method Details

#call(job_instance, job_payload, queue) ⇒ Object



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
# File 'lib/perfm/middleware/sidekiq_gvl_instrumentation.rb', line 6

def call(job_instance, job_payload, queue)
  before_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  before_gc_time = GC.total_time
  
  timer = GVLTiming.measure do
    yield
  end
  
  total_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - before_time
  gc_time = GC.total_time - before_gc_time

  begin
    data = {
      gc_ms: (gc_time / 1_000_000.0).round(2),
      run_ms: (timer.cpu_duration * 1000.0).round(2),
      idle_ms: (timer.idle_duration * 1000.0).round(2),
      stall_ms: (timer.stalled_duration * 1000.0).round(2),
      io_percent: (timer.idle_duration / total_time * 100.0).round(1),
      job_class: job_payload["class"],
      queue: queue
    }

    Perfm.agent.push_sidekiq_metrics(data)
  rescue => e
    puts "Sidekiq GVL metrics collection failed: #{e.message}"
  end
end