Skip to content

Instantly share code, notes, and snippets.

@wickedchicken
Created October 6, 2011 01:03
Show Gist options
  • Save wickedchicken/1266207 to your computer and use it in GitHub Desktop.
Save wickedchicken/1266207 to your computer and use it in GitHub Desktop.
Sample Sinatra app to periodically submit hit data to metrics
# example code to submit data to the Librato Metrics API
# this is a small sinatra web application that keeps track of how many times
# the main page has been hit. when desired, the user can submit that data to metrics
# for more info visit http://dev.librato.com/
# 1.8.x compatibility
require 'rubygems'
require 'sinatra'
require 'rest-client'
# these are automatically set by the Heroku add-on
# if you already have a username and token, place them here
user = ENV["LIBRATO_METRICS_USER"]
token = ENV["LIBRATO_METRICS_TOKEN"]
metrics_api = RestClient::Resource.new 'https://metrics-api.librato.com', user, token
# start our hits counter off at 0
hits_counter = 0
get '/' do
hits_counter += 1
"<p>current count: #{hits_counter}</p>" +
"<p>reload to increase the count</p>" +
"<p>submit to metrics <a href=\"/submit\">here</a></p>"
end
get '/submit' do
# send some data to the counter
# available metrics are 'counters' and 'gauges'
# counters go continually up and wrap around ('total bytes sent')
# while gauges are realtime samples ('current temperature')
metrics_api['/v1/metrics.json'].post :counters => {"hits" => {:value => hits_counter}}
"<p>submitted: #{hits_counter}</p>" +
"<p><a href=\"/\">go back</a></p>" +
"<p>view uploaded data over time at <a href=\"http://metrics.librato.com\">http://metrics.librato.com</a></p>"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment