Sha256: 4412389b6b6da0035bfbe2c16a66f786de3c8614f040897e18a2a03f068687cf

Contents?: true

Size: 1.33 KB

Versions: 6

Compression:

Stored size: 1.33 KB

Contents

module NSA
  module Statsd
    module Publisher

      def statsd_count(key, value = 1, sample_rate = nil)
        __statsd_publish(:count, key, value, sample_rate)
      end

      def statsd_decrement(key, sample_rate = nil)
        __statsd_publish(:decrement, key, 1, sample_rate)
      end

      def statsd_gauge(key, value = 1, sample_rate = nil)
        __statsd_publish(:gauge, key, value, sample_rate)
      end

      def statsd_increment(key, sample_rate = nil)
        __statsd_publish(:increment, key, 1, sample_rate)
      end

      def statsd_set(key, value = 1, sample_rate = nil)
        __statsd_publish(:set, key, value, sample_rate)
      end

      def statsd_time(key, sample_rate = nil, &block)
        start = Time.now
        result = block.call unless block.nil?
        statsd_timing(key, ((Time.now - start) * 1000).round, sample_rate)
        result
      end

      def statsd_timing(key, value = 1, sample_rate = nil)
        __statsd_publish(:timing, key, value, sample_rate)
      end

      def __statsd_publish(stat_type, key, value = nil, sample_rate = nil)
        payload = { :key => key }
        payload.merge!({ :value => value }) if value
        payload.merge!({ :sample_rate => sample_rate }) if sample_rate

        ::ActiveSupport::Notifications.instrument("#{stat_type}.statsd", payload)
      end

    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

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