Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial version of gemified mongo instrumentation, taken from the ini…
…tial experimental version here: http://tomafro.net/2011/02/experimental-mongo-instrumentation
- Loading branch information
0 parents
commit cdf414d
Showing
11 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
*.gem | ||
.bundle | ||
Gemfile.lock | ||
pkg/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
source "http://rubygems.org" | ||
|
||
# Specify your gem's dependencies in mongo-rails-instrumentation.gemspec | ||
gemspec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Mongo Rails 3 Instrumentation | ||
============================= | ||
|
||
Records how long your rails app spends in Mongo, and outputs it to the log, i.e.: | ||
|
||
Completed 200 OK in 1535ms (Views: 623.3ms | Mongo: 848.2ms) | ||
|
||
To use, just add the gem to your Gemfile and restart your app: | ||
|
||
gem 'mongo-rails-instrumentation', '~>0.1' | ||
|
||
This gem's home is on github at https://github.com/freerange/mongo-rails-instrumentation, so please add comments, suggestions and improvements there. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
require 'bundler' | ||
Bundler::GemHelper.install_tasks |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
require 'mongo/rails/instrumentation/railtie' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module Mongo | ||
module Rails | ||
module Instrumentation | ||
autoload :Version, 'mongo/rails/instrumentation/version' | ||
autoload :Railtie, 'mongo/rails/instrumentation/railtie' | ||
autoload :LogSubscriber, 'mongo/rails/instrumentation/log_subscriber' | ||
autoload :ControllerRuntime, 'mongo/rails/instrumentation/controller_runtime' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
require 'mongo/rails/instrumentation' | ||
|
||
module Mongo::Rails::Instrumentation | ||
module ControllerRuntime | ||
extend ActiveSupport::Concern | ||
|
||
protected | ||
|
||
attr_internal :mongo_runtime | ||
|
||
def cleanup_view_runtime | ||
mongo_rt_before_render = LogSubscriber.reset_runtime | ||
runtime = super | ||
mongo_rt_after_render = LogSubscriber.reset_runtime | ||
self.mongo_runtime = mongo_rt_before_render + mongo_rt_after_render | ||
runtime - mongo_rt_after_render | ||
end | ||
|
||
def append_info_to_payload(payload) | ||
super | ||
payload[:mongo_runtime] = mongo_runtime | ||
end | ||
|
||
module ClassMethods | ||
def log_process_action(payload) | ||
messages, mongo_runtime = super, payload[:mongo_runtime] | ||
messages << ("Mongo: %.1fms" % mongo_runtime.to_f) if mongo_runtime | ||
This comment has been minimized.
Sorry, something went wrong. |
||
messages | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'mongo/rails/instrumentation' | ||
|
||
class Mongo::Rails::Instrumentation::LogSubscriber < ActiveSupport::LogSubscriber | ||
def self.runtime=(value) | ||
Thread.current["mongo_mongo_runtime"] = value | ||
end | ||
|
||
def self.runtime | ||
Thread.current["mongo_mongo_runtime"] ||= 0 | ||
end | ||
|
||
def self.reset_runtime | ||
rt, self.runtime = runtime, 0 | ||
rt | ||
end | ||
|
||
def mongo(event) | ||
self.class.runtime += event.duration | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require 'mongo/rails/instrumentation' | ||
|
||
module Mongo::Rails::Instrumentation | ||
class Railtie < Rails::Railtie | ||
initializer "mongo.rails.instrumentation" do |app| | ||
instrument Mongo::Connection, [ | ||
:send_message, | ||
:send_message_with_safe_check, | ||
:receive_message | ||
] | ||
|
||
ActiveSupport.on_load(:action_controller) do | ||
include ControllerRuntime | ||
end | ||
|
||
LogSubscriber.attach_to :mongo | ||
end | ||
|
||
def instrument(clazz, methods) | ||
clazz.module_eval do | ||
methods.each do |m| | ||
class_eval %{def #{m}_with_instrumentation(*args, &block) | ||
ActiveSupport::Notifications.instrumenter.instrument "mongo.mongo", :name => "#{m}" do | ||
#{m}_without_instrumentation(*args, &block) | ||
end | ||
end | ||
} | ||
|
||
alias_method_chain m, :instrumentation | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Mongo | ||
module Rails | ||
module Instrumentation | ||
VERSION = "0.1.0" | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# -*- encoding: utf-8 -*- | ||
$:.push File.expand_path("../lib", __FILE__) | ||
require "mongo/rails/instrumentation/version" | ||
|
||
Gem::Specification.new do |s| | ||
s.name = "mongo-rails-instrumentation" | ||
s.version = Mongo::Rails::Instrumentation::VERSION | ||
s.platform = Gem::Platform::RUBY | ||
s.authors = ["Tom Ward"] | ||
s.email = ["tom@popdog.net"] | ||
s.homepage = "http://tomafro.net" | ||
s.summary = %q{Records time spent in mongo and adds to request logs} | ||
s.description = %q{Records time spent in mongo and adds to request logs} | ||
|
||
s.files = `git ls-files`.split("\n") | ||
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") | ||
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } | ||
s.require_paths = ["lib"] | ||
|
||
s.add_dependency 'rails', '~>3.0.0' | ||
end |
shouldn't condition look like that?...
... if mongo_runtime && mongo_runtime > 0