Skip to content

Instantly share code, notes, and snippets.

@wickedchicken
Created December 6, 2011 20:01
Show Gist options
  • Save wickedchicken/1439704 to your computer and use it in GitHub Desktop.
Save wickedchicken/1439704 to your computer and use it in GitHub Desktop.
sample EY Metrics application
# 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 'ey_config'
require 'rest-client'
require 'singleton'
class MetricsAPI
include Singleton
attr_reader :api
def initialize
# these are automatically set by the Librato Metrics Engine Yard Service
# if you already have a username and token, place them here
user = EY::Config.get('librato-metrics','LIBRATO_METRICS_USER')
token = EY::Config.get('librato-metrics','LIBRATO_METRICS_TOKEN')
@api = RestClient::Resource.new 'https://metrics-api.librato.com', user, token
end
end
class Counter
include Singleton
attr_accessor :count
def initialize
@count = 0
end
end
module Sample
class Application < Sinatra::Base
get '/' do
Counter.instance.count += 1
"<p>current count: #{Counter.instance.count}</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')
MetricsAPI.instance.api['/v1/metrics.json'].post :counters => {"hits" => {:value => Counter.instance.count}}
"<p>submitted: #{Counter.instance.count}</p>" +
"<p><a href=\"/\">go back</a></p>" +
"<p>view uploaded data over time by visiting Metrics through your EY dashboard </p>"
end
end
end
require 'bundler'
require File.expand_path(File.join(File.dirname(__FILE__), 'app.rb'))
map '/' do
run Sample::Application
end
# vim: filetype=ruby
source "http://rubygems.org"
gem "sinatra", "1.2.6", :require => "sinatra/base"
gem "thin", "1.2.7"
gem 'ey_config'
gem 'rest-client'
# vim: filetype=ruby
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment