module StatsD::Instrument::StrictMetaprogramming

Public Instance Methods

statsd_count(method, name, sample_rate: nil, tags: nil, no_prefix: false, client: StatsD.singleton_client) click to toggle source
Calls superclass method
# File lib/statsd/instrument/strict.rb, line 221
def statsd_count(method, name, sample_rate: nil, tags: nil,
  no_prefix: false, client: StatsD.singleton_client)

  check_method_and_metric_name(method, name)

  # Unfortunately, we have to inline the new method implementation because we have to fix the
  # Stats.increment call to not use the `prefix` argument.

  add_to_method(method, name, :count) do
    define_method(method) do |*args, &block|
      key = StatsD::Instrument.generate_metric_name(nil, name, self, *args)
      client.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
      super(*args, &block)
    end
  end
end
statsd_count_if(method, name, sample_rate: nil, tags: nil, no_prefix: false, client: StatsD.singleton_client) { |result| ... } click to toggle source
Calls superclass method
# File lib/statsd/instrument/strict.rb, line 187
def statsd_count_if(method, name, sample_rate: nil, tags: nil,
  no_prefix: false, client: StatsD.singleton_client)

  check_method_and_metric_name(method, name)

  # Unfortunately, we have to inline the new method implementation because we have to fix the
  # Stats.increment call to not use the `prefix` argument.

  add_to_method(method, name, :count_if) do
    define_method(method) do |*args, &block|
      begin
        truthiness = result = super(*args, &block)
      rescue
        truthiness = false
        raise
      else
        if block_given?
          begin
            truthiness = yield(result)
          rescue
            truthiness = false
          end
        end
        result
      ensure
        if truthiness
          key = StatsD::Instrument.generate_metric_name(nil, name, self, *args)
          client.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
        end
      end
    end
  end
end
statsd_count_success(method, name, sample_rate: nil, tags: nil, no_prefix: false, client: StatsD.singleton_client) { |result| ... } click to toggle source
Calls superclass method
# File lib/statsd/instrument/strict.rb, line 154
def statsd_count_success(method, name, sample_rate: nil, tags: nil,
  no_prefix: false, client: StatsD.singleton_client)

  check_method_and_metric_name(method, name)

  # Unfortunately, we have to inline the new method implementation because we have to fix the
  # Stats.increment call to not use the `prefix` argument.

  add_to_method(method, name, :count_success) do
    define_method(method) do |*args, &block|
      begin
        truthiness = result = super(*args, &block)
      rescue
        truthiness = false
        raise
      else
        if block_given?
          begin
            truthiness = yield(result)
          rescue
            truthiness = false
          end
        end
        result
      ensure
        suffix = truthiness == false ? 'failure' : 'success'
        key = "#{StatsD::Instrument.generate_metric_name(nil, name, self, *args)}.#{suffix}"
        client.increment(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix)
      end
    end
  end
end
statsd_distribution(method, name, sample_rate: nil, tags: nil, no_prefix: false, client: StatsD.singleton_client) click to toggle source
Calls superclass method
# File lib/statsd/instrument/strict.rb, line 136
def statsd_distribution(method, name, sample_rate: nil, tags: nil,
  no_prefix: false, client: StatsD.singleton_client)

  check_method_and_metric_name(method, name)

  # Unfortunately, we have to inline the new method implementation because we have to fix the
  # Stats.distribution call to not use the `prefix` argument.

  add_to_method(method, name, :distribution) do
    define_method(method) do |*args, &block|
      key = StatsD::Instrument.generate_metric_name(nil, name, self, *args)
      client.distribution(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) do
        super(*args, &block)
      end
    end
  end
end
statsd_measure(method, name, sample_rate: nil, tags: nil, no_prefix: false, client: StatsD.singleton_client) click to toggle source
Calls superclass method
# File lib/statsd/instrument/strict.rb, line 119
def statsd_measure(method, name, sample_rate: nil, tags: nil,
  no_prefix: false, client: StatsD.singleton_client)

  check_method_and_metric_name(method, name)

  # Unfortunately, we have to inline the new method implementation because we have to fix the
  # Stats.measure call to not use the `as_dist` and `prefix` arguments.
  add_to_method(method, name, :measure) do
    define_method(method) do |*args, &block|
      key = StatsD::Instrument.generate_metric_name(nil, name, self, *args)
      client.measure(key, sample_rate: sample_rate, tags: tags, no_prefix: no_prefix) do
        super(*args, &block)
      end
    end
  end
end

Private Instance Methods

check_method_and_metric_name(method, metric_name) click to toggle source
# File lib/statsd/instrument/strict.rb, line 240
def check_method_and_metric_name(method, metric_name)
  unless method.is_a?(Symbol)
    raise ArgumentError, "The method name should be provided as symbol, got #{method.inspect}"
  end

  unless metric_name.is_a?(String) || metric_name.is_a?(Proc)
    raise ArgumentError, "The metric name should be a proc or string, got #{metric_name.inspect}"
  end
end