forked from eric/metriks
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
module Metriks::Reporter | ||
class Riemann | ||
require 'riemann/client' | ||
|
||
attr_accessor :client | ||
def initialize(options = {}) | ||
@client = ::Riemann::Client.new( | ||
:host => options[:host], | ||
:port => options[:port] | ||
) | ||
@registry = options[:registry] || Metrics::Registry.default | ||
@interval = options[:interval] || 60 | ||
@on_error = options[:on_error] || proc { |ex| } | ||
|
||
@default_event = options[:default_event] || {} | ||
@default_event[:ttl] ||= @interval * 1.5 | ||
end | ||
|
||
def start | ||
@thread ||= Thread.new do | ||
loop do | ||
sleep @interval | ||
|
||
Thread.new do | ||
begin | ||
write | ||
rescue Exception => ex | ||
@on_error[ex] rescue nil | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
def stop | ||
@thread.kill if @thread | ||
@thread = nil | ||
end | ||
|
||
def restart | ||
stop | ||
start | ||
end | ||
|
||
def flush | ||
# Is this supposed to take interval into account? --aphyr | ||
if !@last_write || @last_write.min != Time.now.min | ||
write | ||
end | ||
end | ||
|
||
def write | ||
@last_write = Time.now | ||
|
||
@registry.each do |name, metric| | ||
case metric | ||
when Metriks::Meter | ||
send_metric name, 'meter', metric, [ | ||
:count, :one_minute_rate, :five_minute_rate, | ||
:fifteen_minute_rate, :mean_rate | ||
] | ||
when Metriks::Counter | ||
send_metric name, 'counter', metric, [ | ||
:count | ||
] | ||
when Metriks::UtilizationTimer | ||
send_metric name, 'utilization_timer', metric, [ | ||
:count, :one_minute_rate, :five_minute_rate, | ||
:fifteen_minute_rate, :mean_rate, | ||
:min, :max, :mean, :stddev, | ||
:one_minute_utilization, :five_minute_utilization, | ||
:fifteen_minute_utilization, :mean_utilization, | ||
], [ | ||
:median, :get_95th_percentile | ||
] | ||
when Metriks::Timer | ||
send_metric name, 'timer', metric, [ | ||
:count, :one_minute_rate, :five_minute_rate, | ||
:fifteen_minute_rate, :mean_rate, | ||
:min, :max, :mean, :stddev | ||
], [ | ||
:median, :get_95th_percentile | ||
] | ||
end | ||
end | ||
end | ||
|
||
def send_metric(name, type, metric, keys, snapshot_keys = []) | ||
keys.each do |key| | ||
@client << @default_event.merge( | ||
:service => "#{name} #{key}", | ||
:metric => metric.send(key), | ||
:tags => [type] | ||
) | ||
end | ||
|
||
unless snapshot_keys.empty? | ||
snapshot = metric.snapshot | ||
snapshot_keys.each do |key| | ||
@client << @default_event.merge( | ||
:service => "#{name} #{key}", | ||
:metric => snapshot.send(key), | ||
:tags => [type] | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
require 'test_helper' | ||
|
||
# riemann only works in 1.9 | ||
if RUBY_VERSION > '1.9' | ||
|
||
require 'metriks/reporter/riemann' | ||
|
||
class RiemannReporterTest < Test::Unit::TestCase | ||
def setup | ||
@registry = Metriks::Registry.new | ||
@reporter = Metriks::Reporter::Riemann.new( | ||
:host => "foo", | ||
:port => 1234, | ||
:registry => @registry, | ||
:default_event => {:host => "h"} | ||
) | ||
end | ||
|
||
def teardown | ||
@reporter.stop | ||
@registry.stop | ||
end | ||
|
||
def test_init | ||
assert_equal @reporter.client.host, "foo" | ||
assert_equal @reporter.client.port, 1234 | ||
end | ||
|
||
def test_write | ||
@registry.meter('meter.testing').mark | ||
@registry.counter('counter.testing').increment | ||
@registry.timer('timer.testing').update(1.5) | ||
@registry.utilization_timer('utilization_timer.testing').update(1.5) | ||
|
||
@reporter.client.expects(:<<).at_least_once | ||
@reporter.client.expects(:<<).with( | ||
:host => "h", | ||
:service => "meter.testing count", | ||
:metric => 1, | ||
:tags => ["meter"], | ||
:ttl => 90 | ||
) | ||
@reporter.client.expects(:<<).with( | ||
:host => "h", | ||
:service => "counter.testing count", | ||
:metric => 1, | ||
:tags => ["counter"], | ||
:ttl => 90 | ||
) | ||
@reporter.client.expects(:<<).with( | ||
:host => "h", | ||
:service => "timer.testing max", | ||
:metric => 1.5, | ||
:tags => ["timer"], | ||
:ttl => 90 | ||
) | ||
@reporter.client.expects(:<<).with( | ||
:host => "h", | ||
:service => "utilization_timer.testing mean", | ||
:metric => 1.5, | ||
:tags => ["utilization_timer"], | ||
:ttl => 90 | ||
) | ||
|
||
@reporter.write | ||
end | ||
end | ||
|
||
end |