Class: Singed::RackMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/singed/rack_middleware.rb

Constant Summary collapse

TRUTHY_STRINGS =
["true", "1", "yes"].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ RackMiddleware

Returns a new instance of RackMiddleware.



7
8
9
# File 'lib/singed/rack_middleware.rb', line 7

def initialize(app)
  @app = app
end

Class Method Details

.always_capture?Boolean

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/singed/rack_middleware.rb', line 29

def self.always_capture?
  return @always_capture if defined?(@always_capture)

  @always_capture = TRUTHY_STRINGS.include?(ENV.fetch("SINGED_MIDDLEWARE_ALWAYS_CAPTURE", "false"))
end

Instance Method Details

#call(env) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/singed/rack_middleware.rb', line 11

def call(env)
  status, headers, body = if capture_flamegraph?(env)
    flamegraph do
      @app.call(env)
    end
  else
    @app.call(env)
  end

  [status, headers, body]
end

#capture_flamegraph?(env) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/singed/rack_middleware.rb', line 23

def capture_flamegraph?(env)
  self.class.always_capture? || env["HTTP_X_SINGED"] == "true"
end