Sha256: fca833602d739df5dfd211818c2aea66013106031818c8b76f4ffd74e641ac26

Contents?: true

Size: 1.68 KB

Versions: 5

Compression:

Stored size: 1.68 KB

Contents

require 'statsd'
require 'faraday'




module Saddle
  module Middleware
    module Logging

      # 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
      class StatsdLogger < Faraday::Middleware
        attr_accessor :graphite_host, :graphite_port, :namespace

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

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

        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 = (
              ['saddle'] +
              [env[:request][:saddle][:client_name]] +
              env[:request][:saddle][:call_chain] +
              [env[:request][:saddle][:action]]
            ).join('.')
          else
            statsd_path = "saddle.raw.#{env[:host].to_s}.#{env[:path].to_s}"
          end

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

    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
saddle-0.0.23 lib/saddle/middleware/logging/statsd.rb
saddle-0.0.22 lib/saddle/middleware/logging/statsd.rb
saddle-0.0.21 lib/saddle/middleware/logging/statsd.rb
saddle-0.0.19 lib/saddle/middleware/logging/statsd.rb
saddle-0.0.18 lib/saddle/middleware/logging/statsd.rb