Class: Slimy::Rack::SLIMiddleware

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

Overview

rack middleware for tracking http request SLIs

Constant Summary collapse

MIDDLEWARE_CONTEXT_KEY =
"slimy.milddeware.context"

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ SLIMiddleware

Returns a new instance of SLIMiddleware.



9
10
11
12
13
14
15
16
# File 'lib/slimy/rack/middleware.rb', line 9

def initialize(app, options = {})
  @app = app
  @reporter = if options.key? :reporter
                options[:reporter]
              else
                Slimy::Configuration.default.reporter
              end
end

Instance Method Details

#call(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/slimy/rack/middleware.rb', line 23

def call(env)
  context = init_context(env)
  response = nil
  begin
    response = @app.call(env)
    context.result_error! if response[0] >= 500
  rescue StandardError => e
    context.result_error!
    raise e
  ensure
    context.finish
    report(context)
  end
  response
end

#init_context(env) ⇒ Object



18
19
20
21
# File 'lib/slimy/rack/middleware.rb', line 18

def init_context(env)
  context = Slimy::Context.new(deadline: 200, type: "rack")
  env[MIDDLEWARE_CONTEXT_KEY] = context
end

#report(context) ⇒ Object



39
40
41
# File 'lib/slimy/rack/middleware.rb', line 39

def report(context)
  @reporter&.report(context)
end