class Saddle::Middleware::Logging::StatsdLogger

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

Attributes

graphite_host[RW]
graphite_port[RW]
namespace[RW]

Public Class Methods

new(app, graphite_host, graphite_port=nil, namespace=nil) click to toggle source
Calls superclass method
# 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

Public Instance Methods

call(env) click to toggle source
# File lib/saddle/middleware/logging/statsd.rb, line 34
def call(env)
  # Try to build up a path for the STATSD logging
  if env[:saddle][:statsd_path]
    statsd_path = env[:saddle][:statsd_path]
  elsif env[:saddle][:client]
    statsd_path_components = [
      'saddle',
      env[:saddle][:client].name.underscore,
    ]
    if env[:saddle][:call_chain] && env[:saddle][:action]
      statsd_path_components += env[:saddle][:call_chain]
      statsd_path_components << env[: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
statsd() click to toggle source
# 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

Private Instance Methods

sanitize_path(path) click to toggle source
# File lib/saddle/middleware/logging/statsd.rb, line 64
def sanitize_path(path)
  path.gsub(/\s+/, '_').gsub(/\//, '-').gsub(/[^a-zA-Z_\-0-9\.]/, '')
end