Class: Perfm::Middleware::GvlInstrumentation

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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ GvlInstrumentation

Returns a new instance of GvlInstrumentation.



8
9
10
11
# File 'lib/perfm/middleware/gvl_instrumentation.rb', line 8

def initialize(app)
  @app = app
  @puma_max_threads = nil
end

Instance Method Details

#call(env) ⇒ Object



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

def call(env)
  response = nil
  before_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  before_gc_time = GC.total_time
  
  timer = GVLTiming.measure do
    response = @app.call(env)
  end
  
  total_time = Process.clock_gettime(Process::CLOCK_MONOTONIC) - before_time
  gc_time = GC.total_time - before_gc_time

  begin
    @puma_max_threads ||= get_puma_max_threads if defined?(::Puma)
    
    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),
      method: env["REQUEST_METHOD"],
      controller: nil,
      action: nil,
      puma_max_threads: @puma_max_threads
    }
    
    if (controller = env["action_controller.instance"])
      data[:controller] = controller.controller_path
      data[:action] = controller.action_name
    end

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

  response
end