Class: Metriksd::LibratoMetricsReporter
- Inherits:
-
Object
- Object
- Metriksd::LibratoMetricsReporter
- Defined in:
- lib/metriksd/librato_metrics_reporter.rb
Defined Under Namespace
Classes: TimesliceRollup
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
Instance Method Summary collapse
- #flush ⇒ Object
-
#initialize(registry, options = {}) ⇒ LibratoMetricsReporter
constructor
A new instance of LibratoMetricsReporter.
- #join ⇒ Object
- #sleep_until_deadline ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(registry, options = {}) ⇒ LibratoMetricsReporter
Returns a new instance of LibratoMetricsReporter.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/metriksd/librato_metrics_reporter.rb', line 7 def initialize(registry, = {}) missing_keys = %w(email api_key) - .keys.map(&:to_s) unless missing_keys.empty? raise ArgumentError, "Missing required options: #{missing_keys * ', '}" end @registry = registry @client = Librato::Metrics::Client.new @client.authenticate [:email], [:api_key] @queue = @client.new_queue @interval = [:interval] || @registry.interval @intervel_offset = [:interval_offset] || 2 end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
5 6 7 |
# File 'lib/metriksd/librato_metrics_reporter.rb', line 5 def client @client end |
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
5 6 7 |
# File 'lib/metriksd/librato_metrics_reporter.rb', line 5 def queue @queue end |
Instance Method Details
#flush ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/metriksd/librato_metrics_reporter.rb', line 45 def flush timeslices = @registry.dirty_timeslices timeslices.each do |timeslice| rollup = Metriksd::LibratoMetricsReporter::TimesliceRollup.new(timeslice) @queue.add rollup.to_hash end unless queue.empty? attempts = 3 begin @queue.submit rescue => e if attempts > 0 puts "Exception from librato metrics. retrying: #{e.class}: #{e..to_s[0..500]}\n#{e.backtrace.join("\n\t")}" sleep attempts + 1 attempts -= 1 retry else puts "Exception from librato metrics. dropping: #{e.class}: #{e..to_s[0..500]}\n#{e.backtrace.join("\n\t")}" end end end end |
#join ⇒ Object
39 40 41 42 43 |
# File 'lib/metriksd/librato_metrics_reporter.rb', line 39 def join if @thread @thread.join end end |
#sleep_until_deadline ⇒ Object
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/metriksd/librato_metrics_reporter.rb', line 71 def sleep_until_deadline now = Time.now.to_f rounded = now - (now % @interval) next_rounded = rounded + @interval + @intervel_offset sleep_time = next_rounded - Time.now.to_f # Allow this to be interrupted while sleep_time > 0 && @running s = [ sleep_time, 1 ].min sleep(s) sleep_time = next_rounded - Time.now.to_f end end |
#start ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/metriksd/librato_metrics_reporter.rb', line 22 def start @thread = Thread.new do Thread.current.abort_on_exception = true @running = true while @running sleep_until_deadline flush end end end |
#stop ⇒ Object
35 36 37 |
# File 'lib/metriksd/librato_metrics_reporter.rb', line 35 def stop @running = false end |