mongo-rails-instrumentation 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in mongo-rails-instrumentation.gemspec
4
+ gemspec
data/README ADDED
@@ -0,0 +1,12 @@
1
+ Mongo Rails 3 Instrumentation
2
+ =============================
3
+
4
+ Records how long your rails app spends in Mongo, and outputs it to the log, i.e.:
5
+
6
+ Completed 200 OK in 1535ms (Views: 623.3ms | Mongo: 848.2ms)
7
+
8
+ To use, just add the gem to your Gemfile and restart your app:
9
+
10
+ gem 'mongo-rails-instrumentation', '~>0.1'
11
+
12
+ This gem's home is on github at https://github.com/freerange/mongo-rails-instrumentation, so please add comments, suggestions and improvements there.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1 @@
1
+ require 'mongo/rails/instrumentation/railtie'
@@ -0,0 +1,10 @@
1
+ module Mongo
2
+ module Rails
3
+ module Instrumentation
4
+ autoload :Version, 'mongo/rails/instrumentation/version'
5
+ autoload :Railtie, 'mongo/rails/instrumentation/railtie'
6
+ autoload :LogSubscriber, 'mongo/rails/instrumentation/log_subscriber'
7
+ autoload :ControllerRuntime, 'mongo/rails/instrumentation/controller_runtime'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,32 @@
1
+ require 'mongo/rails/instrumentation'
2
+
3
+ module Mongo::Rails::Instrumentation
4
+ module ControllerRuntime
5
+ extend ActiveSupport::Concern
6
+
7
+ protected
8
+
9
+ attr_internal :mongo_runtime
10
+
11
+ def cleanup_view_runtime
12
+ mongo_rt_before_render = LogSubscriber.reset_runtime
13
+ runtime = super
14
+ mongo_rt_after_render = LogSubscriber.reset_runtime
15
+ self.mongo_runtime = mongo_rt_before_render + mongo_rt_after_render
16
+ runtime - mongo_rt_after_render
17
+ end
18
+
19
+ def append_info_to_payload(payload)
20
+ super
21
+ payload[:mongo_runtime] = mongo_runtime
22
+ end
23
+
24
+ module ClassMethods
25
+ def log_process_action(payload)
26
+ messages, mongo_runtime = super, payload[:mongo_runtime]
27
+ messages << ("Mongo: %.1fms" % mongo_runtime.to_f) if mongo_runtime
28
+ messages
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,20 @@
1
+ require 'mongo/rails/instrumentation'
2
+
3
+ class Mongo::Rails::Instrumentation::LogSubscriber < ActiveSupport::LogSubscriber
4
+ def self.runtime=(value)
5
+ Thread.current["mongo_mongo_runtime"] = value
6
+ end
7
+
8
+ def self.runtime
9
+ Thread.current["mongo_mongo_runtime"] ||= 0
10
+ end
11
+
12
+ def self.reset_runtime
13
+ rt, self.runtime = runtime, 0
14
+ rt
15
+ end
16
+
17
+ def mongo(event)
18
+ self.class.runtime += event.duration
19
+ end
20
+ end
@@ -0,0 +1,34 @@
1
+ require 'mongo/rails/instrumentation'
2
+
3
+ module Mongo::Rails::Instrumentation
4
+ class Railtie < Rails::Railtie
5
+ initializer "mongo.rails.instrumentation" do |app|
6
+ instrument Mongo::Connection, [
7
+ :send_message,
8
+ :send_message_with_safe_check,
9
+ :receive_message
10
+ ]
11
+
12
+ ActiveSupport.on_load(:action_controller) do
13
+ include ControllerRuntime
14
+ end
15
+
16
+ LogSubscriber.attach_to :mongo
17
+ end
18
+
19
+ def instrument(clazz, methods)
20
+ clazz.module_eval do
21
+ methods.each do |m|
22
+ class_eval %{def #{m}_with_instrumentation(*args, &block)
23
+ ActiveSupport::Notifications.instrumenter.instrument "mongo.mongo", :name => "#{m}" do
24
+ #{m}_without_instrumentation(*args, &block)
25
+ end
26
+ end
27
+ }
28
+
29
+ alias_method_chain m, :instrumentation
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,7 @@
1
+ module Mongo
2
+ module Rails
3
+ module Instrumentation
4
+ VERSION = "0.1.0"
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "mongo/rails/instrumentation/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "mongo-rails-instrumentation"
7
+ s.version = Mongo::Rails::Instrumentation::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Tom Ward"]
10
+ s.email = ["tom@popdog.net"]
11
+ s.homepage = "http://tomafro.net"
12
+ s.summary = %q{Records time spent in mongo and adds to request logs}
13
+ s.description = %q{Records time spent in mongo and adds to request logs}
14
+
15
+ s.files = `git ls-files`.split("\n")
16
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
+ s.require_paths = ["lib"]
19
+
20
+ s.add_dependency 'rails', '~>3.0.0'
21
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongo-rails-instrumentation
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Tom Ward
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-02-19 00:00:00 +00:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ version: 3.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ description: Records time spent in mongo and adds to request logs
38
+ email:
39
+ - tom@popdog.net
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ extra_rdoc_files: []
45
+
46
+ files:
47
+ - .gitignore
48
+ - Gemfile
49
+ - README
50
+ - Rakefile
51
+ - lib/mongo-rails-instrumentation.rb
52
+ - lib/mongo/rails/instrumentation.rb
53
+ - lib/mongo/rails/instrumentation/controller_runtime.rb
54
+ - lib/mongo/rails/instrumentation/log_subscriber.rb
55
+ - lib/mongo/rails/instrumentation/railtie.rb
56
+ - lib/mongo/rails/instrumentation/version.rb
57
+ - mongo-rails-instrumentation.gemspec
58
+ has_rdoc: true
59
+ homepage: http://tomafro.net
60
+ licenses: []
61
+
62
+ post_install_message:
63
+ rdoc_options: []
64
+
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ hash: 3
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ hash: 3
82
+ segments:
83
+ - 0
84
+ version: "0"
85
+ requirements: []
86
+
87
+ rubyforge_project:
88
+ rubygems_version: 1.5.2
89
+ signing_key:
90
+ specification_version: 3
91
+ summary: Records time spent in mongo and adds to request logs
92
+ test_files: []
93
+