class TweekMetrics::Handler

Attributes

instruments[R]
key[R]

Public Class Methods

new(instruments, key, &block) click to toggle source
# File lib/tweek_metrics/handler.rb, line 7
def initialize(instruments, key, &block)
  @instruments = instruments
  @key = key
  @block = block

  handle!
end

Public Instance Methods

handle!() click to toggle source
# File lib/tweek_metrics/handler.rb, line 15
def handle!
  if @block
    begin
      @real_time = Benchmark.measure { @block.call }.real
    rescue => e
      @exception = e
    end
  end

  if @exception
    TweekMetrics.statsd.increment(create_key(TweekMetrics.namespace, :global, :exception, @exception.class.name))
    TweekMetrics.statsd.increment(create_key(TweekMetrics.namespace, TweekMetrics.client, :exception, @exception.class.name))
  else
    if instruments.include?(:counter)
      TweekMetrics.statsd.increment(create_key(TweekMetrics.namespace, :global, key))
      TweekMetrics.statsd.increment(create_key(TweekMetrics.namespace, TweekMetrics.client, key))
    end

    if instruments.include?(:time)
      TweekMetrics.statsd.timing(create_key(TweekMetrics.namespace, :global, key), @real_time)
      TweekMetrics.statsd.timing(create_key(TweekMetrics.namespace, TweekMetrics.client, key), @real_time)
    end
  end
end

Private Instance Methods

create_key(*items) click to toggle source
# File lib/tweek_metrics/handler.rb, line 42
def create_key(*items)
  items.flatten.compact.join(".")
end