Skip to content

Instantly share code, notes, and snippets.

@eric
Created August 4, 2011 05:33
Show Gist options
  • Save eric/1124560 to your computer and use it in GitHub Desktop.
Save eric/1124560 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
Bundler.require :default
URL = ''
METRICS_USER = ''
METRICS_TOKEN = ''
def measure_request(url)
t0 = Time.now
http = EM::HttpRequest.new(url).get
http.callback do
submit_metric('web.response_time.root', t0, Time.now)
end
http.errback do
submit_metric('web.response_time.root', t0, Time.now)
end
end
def submit_metric(metric, t0, t1)
conn = EM::HttpRequest.new("https://metrics-api.librato.com/v1/gauges/#{metric}.json")
auth = "Basic %s" % Base64.encode64([ METRICS_USER, METRICS_TOKEN ].join(':')).split.join
head = { :authorization => auth }
body = { :value => (t1 - t0).to_f, :measure_time => t0.to_i }
http = conn.post :body => body, :head => head
http.callback do
puts "success: #{(t1 - t0).to_f}"
end
http.errback do
puts "error: #{http.response.inspect}"
end
end
EM.run do
EventMachine.add_periodic_timer(1) do
measure_request(URL)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment