Class: Metriks::Reporter::DogStatsd
- Inherits:
-
Object
- Object
- Metriks::Reporter::DogStatsd
- Defined in:
- lib/metriks/reporter/dogstatsd.rb,
lib/metriks/reporter/dogstatsd/version.rb
Overview
Adapter class for dogstatsd-ruby and the metriks metric collection system.
Defined Under Namespace
Modules: Version
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
Instance Method Summary collapse
-
#initialize(host = 'localhost', port = 8125, options = {}) ⇒ DogStatsd
constructor
A new instance of DogStatsd.
-
#restart ⇒ Object
Restart the backend thread.
-
#socket ⇒ Object
Get the socket reference to the backend server.
-
#socket=(io) ⇒ Object
Set the socket reference to the backend server.
- #spawn_threaded_write_system ⇒ Object
-
#start ⇒ Object
Start the backend thread.
-
#stop ⇒ Object
Kill and release the backend thread.
-
#write ⇒ Object
Flush the metrics being aggregated by the registry to the statistics collection backend.
- #write_metric(base_name, metric, type, keys, snapshot_keys = []) ⇒ Object
Constructor Details
#initialize(host = 'localhost', port = 8125, options = {}) ⇒ DogStatsd
Returns a new instance of DogStatsd.
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/metriks/reporter/dogstatsd.rb', line 11 def initialize host = 'localhost', port = 8125, ={} @client = Statsd.new(host, port) @prefix = [:prefix] @registry = [:registry] || Metriks::Registry.default @interval = [:interval] || 5 # Seconds @timeout_seconds = [:timeout_seconds] || 4 # Seconds @on_error = [:on_error] || proc{|exception| } # by default, do nothing and just reconnect to the backend Statsd.logger = [:logger] end |
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
9 10 11 |
# File 'lib/metriks/reporter/dogstatsd.rb', line 9 def client @client end |
Instance Method Details
#restart ⇒ Object
Restart the backend thread. This is implemented as a plain stop, followed by a plain start
72 73 74 75 |
# File 'lib/metriks/reporter/dogstatsd.rb', line 72 def restart stop start end |
#socket ⇒ Object
Get the socket reference to the backend server. This is broken out to simplify testing.
27 28 29 |
# File 'lib/metriks/reporter/dogstatsd.rb', line 27 def socket @client.instance_variable_get(:@socket) end |
#socket=(io) ⇒ Object
Set the socket reference to the backend server. This is broken out to simplify testing.
33 34 35 |
# File 'lib/metriks/reporter/dogstatsd.rb', line 33 def socket= io @client.instance_variable_set(:@socket, io) end |
#spawn_threaded_write_system ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/metriks/reporter/dogstatsd.rb', line 48 def spawn_threaded_write_system Thread.new do begin Timeout::timeout(@timeout_seconds) do write end # Yes, this is dumb, but I need to asynchronously pass the # exception up the thread stack and reconnect to the server seamlessly rescue Exception => ex @on_error.call(ex) end end end |
#start ⇒ Object
Start the backend thread. The backend thread handles connections to the dogstatsd daemon.
39 40 41 42 43 44 45 46 |
# File 'lib/metriks/reporter/dogstatsd.rb', line 39 def start @thread ||= Thread.new do loop do sleep @interval # don't peg the cpu or wakeup continuously spawn_threaded_write_system end end end |
#stop ⇒ Object
Kill and release the backend thread.
65 66 67 68 |
# File 'lib/metriks/reporter/dogstatsd.rb', line 65 def stop @thread.kill if @thread @thread = nil end |
#write ⇒ Object
Flush the metrics being aggregated by the registry to the statistics collection backend
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/metriks/reporter/dogstatsd.rb', line 79 def write @registry.each do |name, metric| case metric # handle all potential metric types when Metriks::Meter write_metric name, metric, :meter, [ # derived metrics :count, :one_minute_rate, :five_minute_rate, :fifteen_minute_rate, :mean_rate ] when Metriks::Counter write_metric name, metric, :counter, [ :count ] when Metriks::UtilizationTimer write_metric name, metric, :utilization_timer, [ :count, :one_minute_rate, :five_minute_rate, :fifteen_minute_rate, :mean_rate, :min, :max, :mean, :stddev ], [ # snapshot metrics :median, :get_95th_percentile ] when Metriks::Timer write_metric name, metric, :timer, [ :count, :one_minute_rate, :five_minute_rate, :fifteen_minute_rate, :mean_rate, :min, :max, :mean, :stddev ], [ :median, :get_95th_percentile ] when Metriks::Histogram write_metric name, metric, :histogram, [ :count, :min, :max, :mean, :stddev ], [ :median, :get_95th_percentile ] end end end |
#write_metric(base_name, metric, type, keys, snapshot_keys = []) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/metriks/reporter/dogstatsd.rb', line 114 def write_metric(base_name, metric, type, keys, snapshot_keys = []) # squash all metrics base name whitespace to underscores # the backend system doesn't do whitespace base_name = base_name.to_s.gsub(/ +/, '_') #prefix the base_name if we've got one set base_name = "#{@prefix}.#{base_name}" if @prefix keys.flatten.each do |key| name = key.to_s.gsub(/^get_/, '') value = metric.send(key) @client.gauge("#{base_name}.#{name}", value) end unless snapshot_keys.empty? snapshot = metric.snapshot snapshot_keys.flatten.each do |key| name = key.to_s.gsub(/^get_/, '') value = snapshot.send(key) @client.gauge("#{base_name}.#{name}", value) end end end |