Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaned up selection of librato metics #19

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions lib/metriks/reporter/librato_metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -43,7 +49,7 @@ def restart
start
end

def write
def prepare_metrics
gauges = []
@registry.each do |name, metric|
gauges << case metric
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
21 changes: 20 additions & 1 deletion test/librato_metrics_reporter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,23 @@ def test_write

@reporter.write
end
end

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