Skip to content
Browse files

Initial version of gemified mongo instrumentation, taken from the ini…

  • Loading branch information...
0 parents commit cdf414decbd0e05201af161b65040e9e386b599f @tomafro committed Feb 19, 2011
4 .gitignore
@@ -0,0 +1,4 @@
+*.gem
+.bundle
+Gemfile.lock
+pkg/*
4 Gemfile
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in mongo-rails-instrumentation.gemspec
+gemspec
12 README
@@ -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.
2 Rakefile
@@ -0,0 +1,2 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
1 lib/mongo-rails-instrumentation.rb
@@ -0,0 +1 @@
+require 'mongo/rails/instrumentation/railtie'
10 lib/mongo/rails/instrumentation.rb
@@ -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
32 lib/mongo/rails/instrumentation/controller_runtime.rb
@@ -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
@codesnik
codesnik added a note Sep 27, 2011

shouldn't condition look like that?...

... if mongo_runtime && mongo_runtime > 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ messages
+ end
+ end
+ end
+end
20 lib/mongo/rails/instrumentation/log_subscriber.rb
@@ -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
34 lib/mongo/rails/instrumentation/railtie.rb
@@ -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
7 lib/mongo/rails/instrumentation/version.rb
@@ -0,0 +1,7 @@
+module Mongo
+ module Rails
+ module Instrumentation
+ VERSION = "0.1.0"
+ end
+ end
+end
21 mongo-rails-instrumentation.gemspec
@@ -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

0 comments on commit cdf414d

Please sign in to comment.
Something went wrong with that request. Please try again.