From 9527d16f6142d146fddfa2fcc1f997450721f6e6 Mon Sep 17 00:00:00 2001 From: Mathieu Ravaux Date: Thu, 13 Sep 2012 20:15:33 +0200 Subject: [PATCH] Added support for Librato Metrics counters --- .../timeslice_rollup.rb | 33 ++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/metriksd/librato_metrics_reporter/timeslice_rollup.rb b/lib/metriksd/librato_metrics_reporter/timeslice_rollup.rb index e22c761..6a35e62 100644 --- a/lib/metriksd/librato_metrics_reporter/timeslice_rollup.rb +++ b/lib/metriksd/librato_metrics_reporter/timeslice_rollup.rb @@ -1,5 +1,29 @@ module Metriksd class LibratoMetricsReporter::TimesliceRollup + + class Counter + attr_accessor :name, :source, :value + def initialize(name, source) + @name = name + @source = source + @value = 0.0 + end + + def increment(val) + @value += val + end + + def to_hash + { + :name => name, + :source => source, + :value => value, + :type => 'counter' + } + end + end + + class AverageGauge attr_accessor :name, :source, :count, :sum, :sum_of_squares, :min, :max @@ -69,6 +93,7 @@ def initialize(timeslice) def process return if @gauges @gauges = {} + @counters = {} @timeslice.flush.each do |data| case data[:type] @@ -89,7 +114,7 @@ def process def to_hash process - @gauges.map do |name, gauge| + @gauges.merge(@counters).map do |name, gauge| [ gauge.name, gauge.to_hash.merge(:measure_time => @timeslice.time) ] end end @@ -135,6 +160,12 @@ def average_gauge(name, source, value) @gauges[key].mark(value) end + def counter(name, source, increment) + key = [ name, source ].join('/') + @counters[key] ||= Counter.new(name, source) + @counters[key].increment(increment) + end + def sum_gauge(name, source, value) key = [ name, source ].join('/') @gauges[key] ||= SumGauge.new(name, source)