Class: Metriks::OpenTSDBReporter
- Inherits:
-
Object
- Object
- Metriks::OpenTSDBReporter
- Defined in:
- lib/metriks/opentsdb_reporter.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#hostname ⇒ Object
Returns the value of attribute hostname.
-
#logger ⇒ Object
Returns the value of attribute logger.
-
#prefix ⇒ Object
Returns the value of attribute prefix.
-
#source ⇒ Object
Returns the value of attribute source.
-
#tags ⇒ Object
Returns the value of attribute tags.
Instance Method Summary collapse
- #create_datapoints(base_name, metric, time, keys, snapshot_keys = []) ⇒ Object
- #flush ⇒ Object
- #get_datapoints ⇒ Object
-
#initialize(h, t, options = {}) ⇒ OpenTSDBReporter
constructor
A new instance of OpenTSDBReporter.
- #log(level, msg) ⇒ Object
- #restart ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #submit(datapoints) ⇒ Object
Constructor Details
#initialize(h, t, options = {}) ⇒ OpenTSDBReporter
Returns a new instance of OpenTSDBReporter.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/metriks/opentsdb_reporter.rb', line 9 def initialize(h, t, = {}) @hostname = h = t @prefix = [:prefix] @source = [:source] @logger = [:logger] || nil @batch_size = [:batch_size] || 50 @registry = [:registry] || Metriks::Registry.default @interval = [:interval] || 60 @time_tracker = Metriks::TimeTracker.new(@interval) @on_error = [:on_error] || proc { |ex| } if [:percentiles] @percentiles = [:percentiles] else @percentiles = [ 0.95, 0.99] end @mutex = Mutex.new @running = false end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
7 8 9 |
# File 'lib/metriks/opentsdb_reporter.rb', line 7 def data @data end |
#hostname ⇒ Object
Returns the value of attribute hostname.
7 8 9 |
# File 'lib/metriks/opentsdb_reporter.rb', line 7 def hostname @hostname end |
#logger ⇒ Object
Returns the value of attribute logger.
7 8 9 |
# File 'lib/metriks/opentsdb_reporter.rb', line 7 def logger @logger end |
#prefix ⇒ Object
Returns the value of attribute prefix.
7 8 9 |
# File 'lib/metriks/opentsdb_reporter.rb', line 7 def prefix @prefix end |
#source ⇒ Object
Returns the value of attribute source.
7 8 9 |
# File 'lib/metriks/opentsdb_reporter.rb', line 7 def source @source end |
#tags ⇒ Object
Returns the value of attribute tags.
7 8 9 |
# File 'lib/metriks/opentsdb_reporter.rb', line 7 def end |
Instance Method Details
#create_datapoints(base_name, metric, time, keys, snapshot_keys = []) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/metriks/opentsdb_reporter.rb', line 159 def create_datapoints(base_name, metric, time, keys, snapshot_keys = []) datapoints = [] keys.flatten.each do |key| name = key.to_s.gsub(/^get_/, '') datapoints << { :metric => "#{base_name}.#{name}", :timestamp => time, :value => metric.send(key), :tags => } end unless snapshot_keys.empty? snapshot = metric.snapshot snapshot_keys.flatten.each do |key| name = key.to_s.gsub(/^get_/, '') datapoints << { :metric => "#{base_name}.#{name}", :timestamp => time, :value => snapshot.send(key), :tags => } end end datapoints end |
#flush ⇒ Object
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/metriks/opentsdb_reporter.rb', line 70 def flush begin @mutex.synchronize do log "debug", "Flushing metrics" submit get_datapoints end rescue Exception => ex log "error",ex. @on_error[ex] rescue nil end end |
#get_datapoints ⇒ Object
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/metriks/opentsdb_reporter.rb', line 104 def get_datapoints time = @time_tracker.now_floored datapoints = [] @registry.each do |name, metric| next if name.nil? || name.empty? name = name.to_s.gsub(/ +/, '_') if @prefix name = "#{@prefix}.#{name}" end case metric when Metriks::Meter datapoints |= create_datapoints name, metric, time, [ :count, :one_minute_rate, :five_minute_rate, :fifteen_minute_rate, :mean_rate ] when Metriks::Counter datapoints |= create_datapoints name, metric, time, [ :count ] when Metriks::Gauge datapoints |= create_datapoints name, metric, time, [ :value ] when Metriks::UtilizationTimer datapoints |= create_datapoints name, metric, time, [ :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 datapoints |= create_datapoints name, metric, time, [ :count, :one_minute_rate, :five_minute_rate, :fifteen_minute_rate, :mean_rate, :min, :max, :mean, :stddev ], [ :median, :get_95th_percentile ] when Metriks::Histogram datapoints |= create_datapoints name, metric, time, [ :count, :min, :max, :mean, :stddev ], [ :median, :get_95th_percentile ] end end datapoints end |
#log(level, msg) ⇒ Object
33 34 35 36 37 |
# File 'lib/metriks/opentsdb_reporter.rb', line 33 def log(level, msg) if !@logger.nil? @logger.send level, msg end end |
#restart ⇒ Object
65 66 67 68 |
# File 'lib/metriks/opentsdb_reporter.rb', line 65 def restart stop start end |
#start ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/metriks/opentsdb_reporter.rb', line 39 def start if @thread && @thread.alive? return end @running = true @thread = Thread.new do while @running @time_tracker.sleep Thread.new do flush end end end end |
#stop ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/metriks/opentsdb_reporter.rb', line 56 def stop @running = false if @thread @thread.join @thread = nil end end |
#submit(datapoints) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/metriks/opentsdb_reporter.rb', line 82 def submit(datapoints) return if datapoints.empty? index = 0 length = @batch_size while index < datapoints.size to_send = nil if datapoints.size < (index + length) length = datapoints.size - index else length = @batch_size end jsonstr = datapoints[index, length].to_json RestClient.post "#{@hostname}/api/put", jsonstr, :content_type => :json, :accept => :json log "debug", "Sent #{length} metrics from #{index}" index += length end log "info", "Sent #{datapoints.size} metrics" end |