Class: Metriks::Reporter::Instrumental

Inherits:
Object
  • Object
show all
Defined in:
lib/metriks/reporter/instrumental.rb

Overview

Reports metrics to Instrumental (instrumentalapp.com/)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Instrumental

You MUST provide either :api_token or :agent as an argument to this method.

options:

:api_token

Your Instrumental API token

:agent

A specific instance of the Instrumental Agent to use with this reporter

:prefix

A string prefix to prepend to all your metrics

:registry

The Metriks registry that will be providing your metrics

:interval

How often to report metrics to Instrumental (default value is every 60 seconds, cannot be lower)

:on_error

A callable object to be executed when an error occurs. This WILL be called from a separate thread, you must ensure that your provided code will be thread safe.



22
23
24
25
26
27
28
29
30
31
# File 'lib/metriks/reporter/instrumental.rb', line 22

def initialize(options = {})
  raise "You must provide either :agent or :api_token as an option" unless options[:agent] || options[:api_token]
  @agent        = options[:agent] || ::Instrumental::Agent.new(options[:api_token])
  @prefix       = options[:prefix]
  @registry     = options[:registry] || Metriks::Registry.default
  interval      = options[:interval] || 60
  interval      = [interval, 60].max
  @time_tracker = Metriks::TimeTracker.new(interval)
  @on_error     = options[:on_error] || proc { |ex| }
end

Instance Attribute Details

#agentObject

Returns the value of attribute agent.



8
9
10
# File 'lib/metriks/reporter/instrumental.rb', line 8

def agent
  @agent
end

#prefixObject

Returns the value of attribute prefix.



8
9
10
# File 'lib/metriks/reporter/instrumental.rb', line 8

def prefix
  @prefix
end

#sourceObject

Returns the value of attribute source.



8
9
10
# File 'lib/metriks/reporter/instrumental.rb', line 8

def source
  @source
end

Instance Method Details

#restartObject



54
55
56
57
# File 'lib/metriks/reporter/instrumental.rb', line 54

def restart
  stop
  start
end

#send_metric(base_name, metric, keys, snapshot_keys = []) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/metriks/reporter/instrumental.rb', line 99

def send_metric(base_name, metric, keys, snapshot_keys = [])
  time = @time_tracker.now_floored

  base_name = base_name.to_s.gsub(/ +/, '_')
  if @prefix
    base_name = "#{@prefix}.#{base_name}"
  end

  keys.flatten.each do |key|
    name = key.to_s.gsub(/^get_/, '').to_s
    full_name = "#{base_name}.#{name}"
    value = metric.send(key)
    if name == "count"
      @agent.increment(full_name, value, time)
    else
      @agent.gauge(full_name, value, time)
    end
  end

  unless snapshot_keys.empty?
    snapshot = metric.snapshot
    snapshot_keys.flatten.each do |key|
      name = key.to_s.gsub(/^get_/, '').to_s
      full_name = "#{base_name}.#{name}"
      value = snapshot.send(key)
      if name == "count"
        @agent.increment(full_name, value, time)
      else
        @agent.gauge(full_name, value, time)
      end
    end
  end

end

#startObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/metriks/reporter/instrumental.rb', line 33

def start
  @thread ||= Thread.new do
    loop do
      @time_tracker.sleep

      Thread.new do
        begin
          write
        rescue Exception => ex
          @on_error[ex] rescue nil
        end
      end
    end
  end
end

#stopObject



49
50
51
52
# File 'lib/metriks/reporter/instrumental.rb', line 49

def stop
  @thread.kill if @thread
  @thread = nil
end

#writeObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/metriks/reporter/instrumental.rb', line 59

def write
  @registry.each do |name, metric|
    case metric
    when Metriks::Meter
      send_metric name, metric, [
        :count, :one_minute_rate, :five_minute_rate,
        :fifteen_minute_rate, :mean_rate
      ]
    when Metriks::Counter
      send_metric name, metric, [
        :count
      ]
    when Metriks::UtilizationTimer
      send_metric name, 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, metric, [
        :count, :one_minute_rate, :five_minute_rate,
        :fifteen_minute_rate, :mean_rate,
        :min, :max, :mean, :stddev
      ], [
        :median, :get_95th_percentile
      ]
    when Metriks::Histogram
      send_metric name, metric, [
        :count, :min, :max, :mean, :stddev
      ], [
        :median, :get_95th_percentile
      ]
    end
  end
end