Class: HeapScope::Middleware

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

Overview

Opt-in Rack middleware. Lightweight sampling only — never full heap on every request.

Instance Method Summary collapse

Constructor Details

#initialize(app, sample_rate: 0.01, force_gc: false, mode: :lightweight, on_report: nil) ⇒ Middleware

Returns a new instance of Middleware.



6
7
8
9
10
11
12
# File 'lib/heapscope/middleware.rb', line 6

def initialize(app, sample_rate: 0.01, force_gc: false, mode: :lightweight, on_report: nil)
  @app = app
  @sample_rate = sample_rate
  @force_gc = force_gc
  @mode = mode
  @on_report = on_report
end

Instance Method Details

#call(env) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/heapscope/middleware.rb', line 14

def call(env)
  return @app.call(env) if @sample_rate <= 0 || rand > @sample_rate

  status = headers = body = nil
  report = HeapScope.measure(
    force_gc: @force_gc,
    mode: @mode,
    metadata: {
      kind: "rack_request",
      path: env["PATH_INFO"],
      method: env["REQUEST_METHOD"],
      pid: Process.pid
    }
  ) do
    status, headers, body = @app.call(env)
    true
  end
  @on_report&.call(report)
  [status, headers, body]
end