Class: Metriksd::LibratoMetricsReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/metriksd/librato_metrics_reporter.rb

Defined Under Namespace

Classes: TimesliceRollup

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  missing_keys = %w(email api_key) - options.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 options[:email], options[:api_key]
  @queue = @client.new_queue

  @interval        = options[:interval] || @registry.interval
  @intervel_offset = options[:interval_offset] || 2
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/metriksd/librato_metrics_reporter.rb', line 5

def client
  @client
end

#queueObject (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

#flushObject



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.message.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.message.to_s[0..500]}\n#{e.backtrace.join("\n\t")}"
      end
    end
  end
end

#joinObject



39
40
41
42
43
# File 'lib/metriksd/librato_metrics_reporter.rb', line 39

def join
  if @thread
    @thread.join
  end
end

#sleep_until_deadlineObject



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

#startObject



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

#stopObject



35
36
37
# File 'lib/metriksd/librato_metrics_reporter.rb', line 35

def stop
  @running = false
end