diff --git a/lib/metriks/reporter/librato_metrics.rb b/lib/metriks/reporter/librato_metrics.rb index 3299e8e..ba9f55d 100644 --- a/lib/metriks/reporter/librato_metrics.rb +++ b/lib/metriks/reporter/librato_metrics.rb @@ -15,6 +15,12 @@ def initialize(email, token, options = {}) @registry = options[:registry] || Metriks::Registry.default @time_tracker = Metriks::TimeTracker.new(options[:interval] || 60) @on_error = options[:on_error] || proc { |ex| } + + if options[:only] and options[:except] + raise 'Can only specify one of :only or :except' + end + @only = options[:only] || [] + @except = options[:except] || [] end def start @@ -43,7 +49,7 @@ def restart start end - def write + def prepare_metrics gauges = [] @registry.each do |name, metric| gauges << case metric @@ -84,9 +90,13 @@ def write end gauges.flatten! + gauges + end + def write + gauges = prepare_metrics unless gauges.empty? - submit(form_data(gauges.flatten)) + submit(form_data(gauges)) end end @@ -146,6 +156,15 @@ def prepare_metric(base_name, metric, keys, snapshot_keys = []) base_name = "#{@prefix}.#{base_name}" end + if @only.any? + keys = keys & @only + snapshot_keys = snapshot_keys & @only + end + if @except.any? + keys = keys - @except + snapshot_keys = snapshot_keys - @except + end + keys.flatten.each do |key| name = key.to_s.gsub(/^get_/, '') value = metric.send(key) diff --git a/test/librato_metrics_reporter_test.rb b/test/librato_metrics_reporter_test.rb index 2553ba1..d487c86 100644 --- a/test/librato_metrics_reporter_test.rb +++ b/test/librato_metrics_reporter_test.rb @@ -31,4 +31,23 @@ def test_write @reporter.write end -end \ No newline at end of file + + def assert_generated(count, options) + @registry.timer('timer.testing').update(1.5) + + metrics = build_reporter(options).prepare_metrics + assert_equal(count, metrics.length) + end + + def test_no_filters + assert_generated(11, {}) + end + + def test_only + assert_generated(2, {:only => [:count, :median]}) + end + + def test_except + assert_generated(9, {:except => [:count, :median]}) + end +end