Skip to content

Instantly share code, notes, and snippets.

@gorenje
Created January 30, 2013 18:47
Show Gist options
  • Save gorenje/4675639 to your computer and use it in GitHub Desktop.
Save gorenje/4675639 to your computer and use it in GitHub Desktop.
===> old code
def send_gauge_metric(gauge, time, source, value)
uri = URI.parse("https://metrics-api.librato.com/v1/gauges/#{gauge}.json")
req = Net::HTTP::Post.new(uri.path)
req.basic_auth ApiKeys.librato.username, ApiKeys.librato.password
req.set_form_data('measure_time' => time.to_i,
'period' => 60,
'value' => value,
'source' => source)
begin
HttpClient.request(uri, req)
rescue Exception => e
puts "#{Time.now} - Couldn't send to librato"
puts "#{Time.now} - #{e}"
OpenStruct.new :code => 501
end
end
===> new code
def send_off_to_librato
time, gauges = Time.now, []
@counter.dup.each do |key, value|
_, action, status = LibratoClient.split(key)
gauges << { ("mops.consumers." +
"#{ApiKeys.librato.prefix}." +
"#{action}.#{status}") => {
'measure_time' => time.to_i,
'period' => 60,
'value' => value,
'source' => key.gsub(/;/,'.')
}
}
inc(key, 0)
end
LibratoClient.send_gauge_metric_bulk(gauges)
end
def send_gauge_metric_bulk(gauges)
uri = URI.parse("https://metrics-api.librato.com/v1/metrics")
y gauges
req = Net::HTTP::Post.new(uri.path)
req.add_field("Content-Type", "application/json")
req.basic_auth ApiKeys.librato.username, ApiKeys.librato.password
req.set_form_data('gauges' => gauges)
begin
HttpClient.request(uri, req)
rescue Exception => e
puts "#{Time.now} - Couldn't send to librato"
puts "#{Time.now} - #{e}"
OpenStruct.new :code => 501
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment