Sha256: 7fa6d8de09655bcc54dd31a1ed2f9f957ec75ed33ac3363912f213508078ee63

Contents?: true

Size: 1.34 KB

Versions: 4

Compression:

Stored size: 1.34 KB

Contents

require 'statsd'
require 'faraday'




module Saddle::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
        end
      end

      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
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
saddle-0.0.11 lib/saddle/middleware/logging/statsd.rb
saddle-0.0.10 lib/saddle/middleware/logging/statsd.rb
saddle-0.0.7 lib/saddle/middleware/logging/statsd.rb
saddle-0.0.6 lib/saddle/middleware/logging/statsd.rb