rails_instrument 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore CHANGED
@@ -2,3 +2,4 @@
2
2
  .bundle
3
3
  Gemfile.lock
4
4
  pkg/*
5
+ doc
@@ -1,3 +1,3 @@
1
1
  module RailsInstrument
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,19 +1,53 @@
1
1
  require "rails_instrument/version"
2
2
 
3
3
  module RailsInstrument
4
- class Middleware
4
+ class <<self
5
+ # Resets the instrument statistics and counts
6
+ def reset!
7
+ $rails_instrument = {}
8
+ end
9
+
10
+ def init #:nodoc:
11
+ $rails_instrument ||= {}
12
+ $rails_instrument[:sql_count] ||= 0
13
+ end
14
+
15
+ def data #:nodoc:
16
+ $rails_instrument
17
+ end
18
+
19
+ # Return the number of sql fired from the last reset
20
+ def sql_count
21
+ data[:sql_count]
22
+ end
23
+
24
+ # Taken a block and return the instrument object for the operation done on the block.
25
+ # TODO: Make it to work with nested instrument blocks
26
+ def instrument(&block)
27
+ raise "A block is not passed" unless block_given?
28
+ RailsInstrument.reset!
29
+ yield
30
+ self
31
+ end
32
+
33
+ def increment_sql_count #:nodoc:
34
+ data[:sql_count] += 1
35
+ end
36
+ end
37
+
38
+ class Middleware #:nodoc:
5
39
  def initialize(app, options = {})
6
40
  @app = app
7
41
  end
8
42
 
9
43
  def call(env)
10
- $rails_instrument = {}
44
+ RailsInstrument.reset!
11
45
  status, headers, body = @app.call(env)
12
46
  begin
13
- headers["X-View-Runtime"] = ($rails_instrument["process_action.action_controller"][:view_runtime] / 1000).to_s
14
- headers["X-DB-Runtime"] = ($rails_instrument["process_action.action_controller"][:db_runtime] / 1000).to_s
15
- headers["X-DB-Query-Count"] = $rails_instrument[:sql_count].to_s
16
- rescue
47
+ headers["X-View-Runtime"] = (RailsInstrument.data["process_action.action_controller"][:view_runtime] / 1000).to_s
48
+ headers["X-DB-Runtime"] = (RailsInstrument.data["process_action.action_controller"][:db_runtime] / 1000).to_s
49
+ headers["X-DB-Query-Count"] = RailsInstrument.sql_count.to_s
50
+ rescue => e
17
51
  # Do nothing
18
52
  end
19
53
 
@@ -21,19 +55,18 @@ module RailsInstrument
21
55
  end
22
56
  end
23
57
 
24
- class Engine < ::Rails::Engine
58
+ class Engine < ::Rails::Engine #:nodoc:
25
59
  initializer "my_engine.add_middleware" do |app|
26
60
  app.middleware.use RailsInstrument::Middleware
27
61
 
28
62
  ActiveSupport::Notifications.subscribe("process_action.action_controller") do |name, start, finish, id, payload|
29
- $rails_instrument ||= {}
63
+ RailsInstrument.init
30
64
  $rails_instrument[name] = payload
31
65
  end
32
66
 
33
67
  ActiveSupport::Notifications.subscribe("sql.active_record") do |name, start, finish, id, payload|
34
- $rails_instrument ||= {}
35
- $rails_instrument[:sql_count] ||= 0
36
- $rails_instrument[:sql_count] += 1 if payload[:name] != "SCHEMA"
68
+ RailsInstrument.init
69
+ RailsInstrument.increment_sql_count unless (payload[:name] == "SCHEMA" || %w(BEGIN COMMIT ROLLBACK).include?(payload[:sql]))
37
70
  end
38
71
  end
39
72
  end
data/readme.md ADDED
@@ -0,0 +1,31 @@
1
+ # Rails Instrument
2
+
3
+ This gem is a middleware which add some instrumentation details like db
4
+ runtime, view runtime, numner of sqls fired for each request in the
5
+ response headers.
6
+
7
+ ## Response headers added
8
+
9
+ * X-View-Runtime
10
+ * X-DB-Runtime
11
+ * X-DB-Query-Count
12
+
13
+
14
+ ## Installation
15
+
16
+ Install the latest stable release:
17
+
18
+ [sudo] gem install rails_instrument
19
+
20
+ In Rails >= 3, add it to your Gemfile:
21
+
22
+ ``` ruby
23
+ gem 'carrierwave'
24
+ ```
25
+
26
+
27
+ ## TODO
28
+ * Create chrome extension to show this information inline in the page
29
+ * Add helper methods for tests. Ex: The number of sqls fired can be
30
+ asserted. - wip
31
+ * Add tests coverage.
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_instrument
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-13 00:00:00.000000000Z
12
+ date: 2011-09-15 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: Middleware to show instrumentation information in http headers
15
15
  email:
@@ -24,6 +24,7 @@ files:
24
24
  - lib/rails_instrument.rb
25
25
  - lib/rails_instrument/version.rb
26
26
  - rails_instrument.gemspec
27
+ - readme.md
27
28
  homepage: https://github.com/selvakn/rails_instrument
28
29
  licenses: []
29
30
  post_install_message: