Class: ThreeThings::Middleware

Inherits:
Object
  • Object
show all
Defined in:
lib/three-things/middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, statsd, prefix = "") ⇒ Middleware

Returns a new instance of Middleware.



3
4
5
6
7
8
9
10
# File 'lib/three-things/middleware.rb', line 3

def initialize(app, statsd, prefix = "")
  @app = app
  @statsd = statsd

  # This is actually better dealt with using Statsd.tap, but it all depends
  # on your statsd implementation.
  @prefix = prefix
end

Instance Method Details

#call(env) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/three-things/middleware.rb', line 20

def call(env)
  req = Rack::Request.new(env)

  # Request count
  @statsd.increment path("request.count"), tags: ["path:#{req.path}"]

  # Reqest time
  status, headers, body = @statsd.time path("request.time"), tags: ["path:#{req.path}"] do
    @app.call env
  end

  # Request errors
  if status > 308
    @statsd.increment path("request.error"), tags: ["status_code:#{status}", "path:#{req.path}"]
  end

  [status, headers, body]
end

#path(postfix) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/three-things/middleware.rb', line 12

def path(postfix)
  if @prefix != ""
    "#{@prefix}.#{postfix}"
  else
    postfix
  end
end