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.



19
20
21
22
23
24
# File 'lib/saddle/middleware/logging/statsd.rb', line 19

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.



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

def graphite_host
  @graphite_host
end

#graphite_portObject

Returns the value of attribute graphite_port.



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

def graphite_port
  @graphite_port
end

#namespaceObject

Returns the value of attribute namespace.



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

def namespace
  @namespace
end

Instance Method Details

#call(env) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/saddle/middleware/logging/statsd.rb', line 34

def call(env)
  # Try to build up a path for the STATSD logging
  if env[:request][:statsd_path]
    statsd_path = env[:request][:statsd_path]
  elsif env[:request][:saddle]
    statsd_path_components = [
      'saddle',
      env[:request][:saddle][:client].name.underscore,
    ]
    if env[:request][:saddle][:call_chain] && env[:request][:saddle][:action]
      statsd_path_components += env[:request][:saddle][:call_chain]
      statsd_path_components << env[:request][:saddle][:action]
    else
      statsd_path_components << 'raw'
      statsd_path_components << "#{env[:url].host}#{env[:url].path}"
    end
    statsd_path = statsd_path_components.join('.')
  end

  # If we have a path, wrap the call
  if statsd_path
    self.statsd.time(sanitize_path(statsd_path)) do
      @app.call(env)
    end
  else
    @app.call(env)
  end
end

#statsdObject



26
27
28
29
30
31
32
# File 'lib/saddle/middleware/logging/statsd.rb', line 26

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