Sha256: b2f103779a8d9d3f777eb678c27e003b854516e5ff745a5ecfce17d06611e8c9

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

require "statsd-ruby"

module NSA
  module Statsd
    module Subscriber

      EXPECTED_RESPONDABLE_METHODS = %i( count decrement gauge increment set time timing ).freeze

      def statsd_subscribe(backend)
        unless backend_valid?(backend)
          fail "Backend must respond to the following methods:\n\t#{EXPECTED_RESPONDABLE_METHODS.join(", ")}"
        end

        ::ActiveSupport::Notifications.subscribe(/.statsd$/) do |name, start, finish, id, payload|
          __send_event_to_statsd(backend, name, start, finish, id, payload)
        end
      end

      def __send_event_to_statsd(backend, name, start, finish, id, payload)
        action = name.to_s.split(".").first || :count

        key_name = payload[:key]
        sample_rate = payload.fetch(:sample_rate, 1)

        case action.to_sym
        when :count, :timing, :set, :gauge then
          value = payload.fetch(:value) { 1 }
          backend.__send__(action, key_name, value, sample_rate)
        when :increment, :decrement then
          backend.__send__(action, key_name, sample_rate)
        end
      end

      def backend_valid?(backend)
        EXPECTED_RESPONDABLE_METHODS.all? do |method_name|
          backend.respond_to?(method_name)
        end
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
nsa-0.3.0 lib/nsa/statsd/subscriber.rb
nsa-0.2.8 lib/nsa/statsd/subscriber.rb
nsa-0.2.7 lib/nsa/statsd/subscriber.rb
nsa-0.2.5 lib/nsa/statsd/subscriber.rb
nsa-0.2.4 lib/nsa/statsd/subscriber.rb
nsa-0.2.3 lib/nsa/statsd/subscriber.rb