Class: Metriks::LibratoMetricsReporter
- Inherits:
-
Object
- Object
- Metriks::LibratoMetricsReporter
- Defined in:
- lib/metriks/librato_metrics_reporter.rb
Defined Under Namespace
Classes: InvalidKeyError, RequestFailedError
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#prefix ⇒ Object
Returns the value of attribute prefix.
-
#source ⇒ Object
Returns the value of attribute source.
Instance Method Summary collapse
- #datapoint(name, value, time, attributes = {}) ⇒ Object
- #flush ⇒ Object
-
#initialize(email, token, options = {}) ⇒ LibratoMetricsReporter
constructor
A new instance of LibratoMetricsReporter.
- #restart ⇒ Object
- #sanitize_name(name) ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #submit ⇒ Object
- #write ⇒ Object
Constructor Details
#initialize(email, token, options = {}) ⇒ LibratoMetricsReporter
Returns a new instance of LibratoMetricsReporter.
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/metriks/librato_metrics_reporter.rb', line 8 def initialize(email, token, = {}) @email = email @token = token @prefix = [:prefix] @source = [:source] @registry = [:registry] || Metriks::Registry.default @interval = [:interval] || 60 @time_tracker = Metriks::TimeTracker.new(@interval) @on_error = [:on_error] || proc { |ex| } @sanitizer = [:sanitize] || proc { |key| key.to_s.gsub(/ +/, '_') } @timeout = [:timeout] || 10 @data = {} @sent = {} @last = Hash.new { |h,k| h[k] = 0 } if [:percentiles] @percentiles = [:percentiles] else @percentiles = [ 0.95, 0.999 ] end @mutex = Mutex.new @running = false end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
6 7 8 |
# File 'lib/metriks/librato_metrics_reporter.rb', line 6 def data @data end |
#prefix ⇒ Object
Returns the value of attribute prefix.
6 7 8 |
# File 'lib/metriks/librato_metrics_reporter.rb', line 6 def prefix @prefix end |
#source ⇒ Object
Returns the value of attribute source.
6 7 8 |
# File 'lib/metriks/librato_metrics_reporter.rb', line 6 def source @source end |
Instance Method Details
#datapoint(name, value, time, attributes = {}) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/metriks/librato_metrics_reporter.rb', line 156 def datapoint(name, value, time, attributes = {}) idx = @data.length if prefix name = "#{prefix}.#{name}" end @data["gauges[#{idx}][name]"] = name @data["gauges[#{idx}][measure_time]"] = time.to_i @data["gauges[#{idx}][value]"] = value unless @source.to_s.empty? @data["gauges[#{idx}][source]"] = @source end unless @sent[name] @sent[name] = true @data["gauges[#{idx}][period]"] = @interval @data["gauges[#{idx}][attributes][aggregate]"] = true attributes.each do |k, v| @data["gauges[#{idx}][attributes][#{k}]"] = v end end end |
#flush ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/metriks/librato_metrics_reporter.rb', line 68 def flush begin @mutex.synchronize do write end rescue Exception => ex @on_error[ex] rescue nil end end |
#restart ⇒ Object
63 64 65 66 |
# File 'lib/metriks/librato_metrics_reporter.rb', line 63 def restart stop start end |
#sanitize_name(name) ⇒ Object
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/metriks/librato_metrics_reporter.rb', line 183 def sanitize_name(name) case @sanitizer when String return name.gsub(/[^.:_\-0-9a-zA-Z]/, @sanitizer)[0...255] when Proc sanitized = @sanitizer.call(name) else raise RuntimeError, "The :sanitize option must be a replacement " \ "string or a Proc that will be passed the metric name to sanitize." end if sanitized.size > 255 || sanitized =~ /[^.:_\-0-9a-zA-Z]/ raise InvalidKeyError, "Librato metric names must match " \ "/[.:_\-0-9a-zA-Z]+/, and must be less than 255 characters." else sanitized end end |
#start ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/metriks/librato_metrics_reporter.rb', line 37 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
54 55 56 57 58 59 60 61 |
# File 'lib/metriks/librato_metrics_reporter.rb', line 54 def stop @running = false if @thread @thread.join @thread = nil end end |
#submit ⇒ Object
78 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 |
# File 'lib/metriks/librato_metrics_reporter.rb', line 78 def submit return if @data.empty? url = URI.parse('https://metrics-api.librato.com/v1/metrics') req = Net::HTTP::Post.new(url.path) req.basic_auth(@email, @token) req.set_form_data(@data) store = OpenSSL::X509::Store.new store.set_default_paths http = Net::HTTP.new(url.host, url.port) http.verify_mode = OpenSSL::SSL::VERIFY_PEER http.use_ssl = true http.open_timeout = @timeout http.read_timeout = @timeout http.cert_store = store case res = http.start { |http| http.request(req) } when Net::HTTPSuccess, Net::HTTPRedirection # OK else raise RequestFailedError.new(req, res, @data.dup) end ensure @data.clear end |
#write ⇒ Object
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 |
# File 'lib/metriks/librato_metrics_reporter.rb', line 106 def write time = @time_tracker.now_floored @registry.each do |name, metric| next if name.nil? || name.empty? name = sanitize_name(name) case metric when Metriks::Meter count = metric.count datapoint(name, count - @last[name], time, :display_min => 0, :summarize_function => 'sum') @last[name] = count when Metriks::Counter datapoint(name, metric.count, time, :summarize_function => 'average') when Metriks::Gauge datapoint(name, metric.value, time, :summarize_function => 'average') when Metriks::Histogram, Metriks::Timer, Metriks::UtilizationTimer if Metriks::UtilizationTimer === metric || Metriks::Timer === metric count = metric.count datapoint(name, count - @last[name], time, :display_min => 0, :summarize_function => 'sum') @last[name] = count end if Metriks::UtilizationTimer === metric datapoint("#{name}.one_minute_utilization", metric.one_minute_utilization, time, :display_min => 0, :summarize_function => 'average') end snapshot = metric.snapshot datapoint("#{name}.median", snapshot.median, time, :display_min => 0, :summarize_function => 'average') @percentiles.each do |percentile| percentile_name = (percentile * 100).to_f.to_s.gsub(/0+$/, '').gsub('.', '') datapoint("#{name}.#{percentile_name}th_percentile", snapshot.value(percentile), time, :display_min => 0, :summarize_function => 'max') end end end if @data.length > 0 submit end end |