Permalink
Browse files

Better organization of code, added rdoc and readme. Basic helper meth…

…od for using in tests
  • Loading branch information...
1 parent 58c71e1 commit a3b424378329cd66825501ab7e1229c717c91981 @selvakn committed Sep 15, 2011
Showing with 77 additions and 12 deletions.
  1. +1 −0 .gitignore
  2. +44 −11 lib/rails_instrument.rb
  3. +1 −1 lib/rails_instrument/version.rb
  4. +31 −0 readme.md
View
@@ -2,3 +2,4 @@
.bundle
Gemfile.lock
pkg/*
+doc
View
@@ -1,39 +1,72 @@
require "rails_instrument/version"
module RailsInstrument
- class Middleware
+ class <<self
+ # Resets the instrument statistics and counts
+ def reset!
+ $rails_instrument = {}
+ end
+
+ def init #:nodoc:
+ $rails_instrument ||= {}
+ $rails_instrument[:sql_count] ||= 0
+ end
+
+ def data #:nodoc:
+ $rails_instrument
+ end
+
+ # Return the number of sql fired from the last reset
+ def sql_count
+ data[:sql_count]
+ end
+
+ # Taken a block and return the instrument object for the operation done on the block.
+ # TODO: Make it to work with nested instrument blocks
+ def instrument(&block)
+ raise "A block is not passed" unless block_given?
+ RailsInstrument.reset!
+ yield
+ self
+ end
+
+ def increment_sql_count #:nodoc:
+ data[:sql_count] += 1
+ end
+ end
+
+ class Middleware #:nodoc:
def initialize(app, options = {})
@app = app
end
def call(env)
- $rails_instrument = {}
+ RailsInstrument.reset!
status, headers, body = @app.call(env)
begin
- headers["X-View-Runtime"] = ($rails_instrument["process_action.action_controller"][:view_runtime] / 1000).to_s
- headers["X-DB-Runtime"] = ($rails_instrument["process_action.action_controller"][:db_runtime] / 1000).to_s
- headers["X-DB-Query-Count"] = $rails_instrument[:sql_count].to_s
- rescue
+ headers["X-View-Runtime"] = (RailsInstrument.data["process_action.action_controller"][:view_runtime] / 1000).to_s
+ headers["X-DB-Runtime"] = (RailsInstrument.data["process_action.action_controller"][:db_runtime] / 1000).to_s
+ headers["X-DB-Query-Count"] = RailsInstrument.sql_count.to_s
+ rescue => e
# Do nothing
end
[status, headers, body]
end
end
- class Engine < ::Rails::Engine
+ class Engine < ::Rails::Engine #:nodoc:
initializer "my_engine.add_middleware" do |app|
app.middleware.use RailsInstrument::Middleware
ActiveSupport::Notifications.subscribe("process_action.action_controller") do |name, start, finish, id, payload|
- $rails_instrument ||= {}
+ RailsInstrument.init
$rails_instrument[name] = payload
end
ActiveSupport::Notifications.subscribe("sql.active_record") do |name, start, finish, id, payload|
- $rails_instrument ||= {}
- $rails_instrument[:sql_count] ||= 0
- $rails_instrument[:sql_count] += 1 if payload[:name] != "SCHEMA"
+ RailsInstrument.init
+ RailsInstrument.increment_sql_count unless (payload[:name] == "SCHEMA" || %w(BEGIN COMMIT ROLLBACK).include?(payload[:sql]))
end
end
end
@@ -1,3 +1,3 @@
module RailsInstrument
- VERSION = "0.0.1"
+ VERSION = "0.0.2"
end
View
@@ -0,0 +1,31 @@
+# Rails Instrument
+
+This gem is a middleware which add some instrumentation details like db
+runtime, view runtime, numner of sqls fired for each request in the
+response headers.
+
+## Response headers added
+
+* X-View-Runtime
+* X-DB-Runtime
+* X-DB-Query-Count
+
+
+## Installation
+
+Install the latest stable release:
+
+ [sudo] gem install rails_instrument
+
+In Rails >= 3, add it to your Gemfile:
+
+``` ruby
+gem 'carrierwave'
+```
+
+
+## TODO
+* Create chrome extension to show this information inline in the page
+* Add helper methods for tests. Ex: The number of sqls fired can be
+ asserted. - wip
+* Add tests coverage.

0 comments on commit a3b4243

Please sign in to comment.