class Invoca::Metrics::DirectMetric

Directly reports metrics without sending through graphite. Does not add process information to metric names.

Constants

DEFAULT_HOST
DEFAULT_PORT
PERIOD

Attributes

name[R]
tick[R]
value[R]

Public Class Methods

generate_distribution(metric_prefix, metric_values, tick = nil) click to toggle source
# File lib/invoca/metrics/direct_metric.rb, line 30
def generate_distribution(metric_prefix, metric_values, tick = nil)
  fixed_tick = tick || rounded_tick
  sorted_values = metric_values.sort
  count = sorted_values.count

  if count == 0
    [
      new("#{metric_prefix}.count",    count,                    fixed_tick)
    ]
  else
    [
      new("#{metric_prefix}.count",    count,                    fixed_tick),
      new("#{metric_prefix}.max",      sorted_values[-1],        fixed_tick),
      new("#{metric_prefix}.min",      sorted_values[0],         fixed_tick),
      new("#{metric_prefix}.median",   sorted_values[count * 0.5], fixed_tick),
      new("#{metric_prefix}.upper_90", sorted_values[count * 0.9], fixed_tick)
    ]
  end
end
new(name, value, tick = nil) click to toggle source
# File lib/invoca/metrics/direct_metric.rb, line 9
def initialize(name, value, tick = nil)
  @name = name
  @value = value
  @tick = tick || self.class.rounded_tick
end
report(metrics) click to toggle source
# File lib/invoca/metrics/direct_metric.rb, line 24
def report(metrics)
  metrics_output = Array(metrics).map { |metric| "#{metric}\n" }.join

  send_to_metric_host(metrics_output)
end
rounded_tick() click to toggle source
# File lib/invoca/metrics/direct_metric.rb, line 50
def rounded_tick
  tick = Time.now.to_i
  tick - (tick % PERIOD)
end

Private Class Methods

send_to_metric_host(message) click to toggle source
# File lib/invoca/metrics/direct_metric.rb, line 57
def send_to_metric_host(message)
  host = ENV["DIRECT_METRIC_HOST"] || DEFAULT_HOST
  port = (ENV["DIRECT_METRIC_PORT"] || DEFAULT_PORT).to_i

  TCPSocket.open(host, port) do |tcp|
    tcp.send(message, 0)
  end
end

Public Instance Methods

to_s() click to toggle source
# File lib/invoca/metrics/direct_metric.rb, line 15
def to_s
  "#{name} #{value} #{tick}"
end