Permalink
Browse files

moved from gist and added readme

  • Loading branch information...
1 parent 6f30cba commit 985053ea52d09a0a6750946f0d460ea6c634564b Ryan Smith (ace hacker) committed Apr 11, 2012
Showing with 198 additions and 0 deletions.
  1. +89 −0 lib/instruments.rb
  2. +109 −0 readme.md
View
@@ -1,2 +1,91 @@
module Instruments
+ def self.defaults=(args)
+ @logger = args[:logger] || Kernel
+ @method = args[:method] || :puts
+ @default_data = args[:data] || {}
+ end
+
+ def self.write(data)
+ @logger.send(@method, data.merge(@default_data))
+ end
+
+ if defined?(::Sinatra)
+ module ::Sinatra
+ module Instrumentation
+ def route(verb, action, *)
+ condition {@instrumented_route = action}
+ super
+ end
+
+ def instrument_routes
+ before do
+ @start_request = Time.now
+ end
+ after do
+ t = Integer((Time.now - @start_request)*1000)
+ Instruments.write({
+ :lib => "sinatra",
+ :action => "http-request",
+ :route => @instrumented_route,
+ :elapsed => t,
+ :method => env["REQUEST_METHOD"].downcase,
+ :status => response.status
+ }.merge(params))
+ end
+ end
+ end
+ register Instrumentation
+ end
+ end
+
+ if defined?(::Sequel)
+ module ::Sequel
+ class Database
+ def log_yield(sql, args=nil)
+ sql = "#{sql}; #{args.inspect}" if args
+ t0 = Time.now
+ begin
+ yield
+ rescue => e
+ log_exception(e, sql)
+ raise
+ ensure
+ t1 = Time.now
+ log_duration(Integer((t1-t0)*1000), sql) unless e
+ end
+ end
+
+ def log_duration(t, sql)
+ Instruments.write(:info => true, :action => action(sql), :elapsed => t, :sql => sql)
+ end
+
+ def log_exception(e, sql)
+ Instruments.write(:error => true, :exception => e.class, :sql => sql)
+ end
+
+ def action(sql)
+ sql[/(\w+){1}/].downcase
+ end
+ end
+ end
+ end
+
+ if defined?(::Excon)
+ module ::Excon
+ module Instrumentation
+ def self.instrument(name, params={}, &blk)
+ t0 = Time.now
+ res = yield if block_given?
+ t1 = Time.now
+ Instruments.write(
+ :lib => "excon",
+ :action => "http-request",
+ :elapsed => Integer((t1-t0)*1000)
+ )
+ end
+ end
+ end
+ Excon.defaults[:instrumentor] = ::Excon::Instrumentation
+ end
+
end
View
109 readme.md
@@ -0,0 +1,109 @@
+# Instruments
+
+Instruments enables out-of-the-box instrumentation on database & HTTP
+activities. Instruments supports the following libraries:
+
+* sinatra
+* sequel
+* excon
+
+## Usage
+
+Provide Instruments with an object (or module) and a method
+and it will call the method passing a Hash containing the
+instrumentation data for each time instruments records a metric.
+
+### Sinatra
+
+#### Modular Application
+
+```ruby
+require "sinatra/base"
+require "instruments"
+Instruments.defaults = {
+ :logger => Kernel,
+ :method => :puts
+}
+
+class API < Sinatra::Base
+ register Sinatra::Instruments
+ instrument_routes
+
+ get "/hello/:name" do
+ params[:name]
+ end
+end
+```
+
+#### Classic Application
+
+```ruby
+require "sinatra"
+require "instruments"
+Instruments.defaults = {
+ :logger => Kernel,
+ :method => :puts
+}
+
+instrument_routes
+get "/hello/:name" do
+ params[:name]
+end
+```
+
+When you hit this endpoint, you will see the following
+in your log stream:
+
+```
+lib=sinatra action="http-request" method="get" route="/hello/:name" status=200 elapsed=0.001
+```
+
+### Sequel
+
+```ruby
+require "sequel"
+require "instruments"
+
+db = Sequel.connect(ENV["DATABASE_URL"])
+db.execute("select 1")
+```
+
+Will produce:
+
+```
+lib=sequel action=select elapsed_time=0.1 sql="select 1"
+```
+
+### Excon
+
+```ruby
+require "excon"
+require "instruments"
+
+conn = Excon.new("https://www.heroku.com")
+conn.get
+```
+
+Will produce:
+
+```
+lib=excon action=http-request elapsed=0
+```
+
+## TODO
+
+* rest-client
+* queue_classic
+* redis
+
+## Links
+
+* https://github.com/sinatra/sinatra/issues/499
+* https://github.com/jeremyevans/sequel/pull/465
+
+## Contributors
+
+* @konstantinhaase
+* @mmcgrana
+* @nzoschke
+* @jeremyevans

0 comments on commit 985053e

Please sign in to comment.