From 47350ac51c20f8910f23415acfd082fdd84acb5a Mon Sep 17 00:00:00 2001 From: ryanlower Date: Wed, 18 Apr 2012 10:00:35 -0700 Subject: [PATCH 1/3] Allow selection of which metrics to send to Librato metrics --- lib/metriks/reporter/librato_metrics.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/metriks/reporter/librato_metrics.rb b/lib/metriks/reporter/librato_metrics.rb index 9ea0183..4a22566 100644 --- a/lib/metriks/reporter/librato_metrics.rb +++ b/lib/metriks/reporter/librato_metrics.rb @@ -12,6 +12,12 @@ def initialize(email, token, options = {}) @registry = options[:registry] || Metriks::Registry.default @interval = 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 @@ -121,6 +127,15 @@ def prepare_metric(base_name, metric, keys, snapshot_keys = []) base_name = "#{@prefix}.#{base_name}" end + if @only + keys = keys & @only + snapshot_keys = snapshot_keys & @only + end + if @except + keys = keys - @except + snapshot_keys = snapshot_keys - @except + end + keys.flatten.each do |key| name = key.to_s.gsub(/^get_/, '') value = metric.send(key) From fa1524d53ac570a632d451b3177c3304f021b65c Mon Sep 17 00:00:00 2001 From: Justin Love Date: Fri, 27 Jul 2012 14:42:12 -0500 Subject: [PATCH 2/3] fix unset values and add tests for librato filters --- lib/metriks/reporter/librato_metrics.rb | 11 +++++++---- test/librato_metrics_reporter_test.rb | 21 ++++++++++++++++++++- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/lib/metriks/reporter/librato_metrics.rb b/lib/metriks/reporter/librato_metrics.rb index ab807f7..fa24980 100644 --- a/lib/metriks/reporter/librato_metrics.rb +++ b/lib/metriks/reporter/librato_metrics.rb @@ -49,7 +49,7 @@ def restart start end - def write + def prepare_metrics gauges = [] @registry.each do |name, metric| gauges << case metric @@ -90,9 +90,12 @@ def write end gauges.flatten! + end + def write + gauges = prepare_metrics unless gauges.empty? - submit(form_data(gauges.flatten)) + submit(form_data(gauges)) end end @@ -152,11 +155,11 @@ def prepare_metric(base_name, metric, keys, snapshot_keys = []) base_name = "#{@prefix}.#{base_name}" end - if @only + if @only.any? keys = keys & @only snapshot_keys = snapshot_keys & @only end - if @except + if @except.any? keys = keys - @except snapshot_keys = snapshot_keys - @except end 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 From 4437808905b4ab3accb8b500944bcc250259ac04 Mon Sep 17 00:00:00 2001 From: Justin Love Date: Fri, 27 Jul 2012 22:18:34 -0500 Subject: [PATCH 3/3] return value from flatten appears to differ by ruby version --- lib/metriks/reporter/librato_metrics.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/metriks/reporter/librato_metrics.rb b/lib/metriks/reporter/librato_metrics.rb index fa24980..ba9f55d 100644 --- a/lib/metriks/reporter/librato_metrics.rb +++ b/lib/metriks/reporter/librato_metrics.rb @@ -90,6 +90,7 @@ def prepare_metrics end gauges.flatten! + gauges end def write