Class: Saddle::Middleware::Logging::StatsdLogger

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
lib/saddle/middleware/logging/statsd.rb

Overview

Public: Wraps request with statsd logging Expects statsd_path in request options. However, if using saddle and no statsd_path is specified will read call_chain and action and use them to construct a statsd_path

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, graphite_host, graphite_port = nil, namespace = nil) ⇒ StatsdLogger

Returns a new instance of StatsdLogger.



16
17
18
19
20
21
# File 'lib/saddle/middleware/logging/statsd.rb', line 16

def initialize(app, graphite_host, graphite_port=nil, namespace=nil)
  super(app)
  @graphite_host = graphite_host
  @graphite_port = graphite_port
  @namespace = namespace
end

Instance Attribute Details

#graphite_hostObject

Returns the value of attribute graphite_host.



14
15
16
# File 'lib/saddle/middleware/logging/statsd.rb', line 14

def graphite_host
  @graphite_host
end

#graphite_portObject

Returns the value of attribute graphite_port.



14
15
16
# File 'lib/saddle/middleware/logging/statsd.rb', line 14

def graphite_port
  @graphite_port
end

#namespaceObject

Returns the value of attribute namespace.



14
15
16
# File 'lib/saddle/middleware/logging/statsd.rb', line 14

def namespace
  @namespace
end

Instance Method Details

#call(env) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/saddle/middleware/logging/statsd.rb', line 30

def call(env)
  statsd_path = nil
  if env[:request][:statsd_path]
    statsd_path = env[:request][:statsd_path]
  elsif env[:request][:saddle] && env[:request][:saddle][:call_chain] && env[:request][:saddle][:action]
    statsd_path = (env[:request][:saddle][:call_chain] + [env[:request][:saddle][:action]]).join('.')
  end

  if statsd_path
    self.statsd.time statsd_path do
      @app.call(env)
    end
  else
    @app.call(env)
  end
end

#statsdObject



23
24
25
26
27
28
# File 'lib/saddle/middleware/logging/statsd.rb', line 23

def statsd
  @statsd ||= begin
    client = ::Statsd.new(@graphite_host, @graphite_port)
    client.namespace = @namespace if @namespace
  end
end