Skip to content

Commit

Permalink
Initial version of gemified mongo instrumentation, taken from the ini…
Browse files Browse the repository at this point in the history
  • Loading branch information
tomafro committed Feb 19, 2011
0 parents commit cdf414d
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
@@ -0,0 +1,4 @@
*.gem
.bundle
Gemfile.lock
pkg/*
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source "http://rubygems.org"

# Specify your gem's dependencies in mongo-rails-instrumentation.gemspec
gemspec
12 changes: 12 additions & 0 deletions 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 changes: 2 additions & 0 deletions Rakefile
@@ -0,0 +1,2 @@
require 'bundler'
Bundler::GemHelper.install_tasks
1 change: 1 addition & 0 deletions lib/mongo-rails-instrumentation.rb
@@ -0,0 +1 @@
require 'mongo/rails/instrumentation/railtie'
10 changes: 10 additions & 0 deletions 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 changes: 32 additions & 0 deletions 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

This comment has been minimized.

Copy link
@codesnik

codesnik Sep 27, 2011

shouldn't condition look like that?...

... if mongo_runtime && mongo_runtime > 0

messages
end
end
end
end
20 changes: 20 additions & 0 deletions 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 changes: 34 additions & 0 deletions 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 changes: 7 additions & 0 deletions lib/mongo/rails/instrumentation/version.rb
@@ -0,0 +1,7 @@
module Mongo
module Rails
module Instrumentation
VERSION = "0.1.0"
end
end
end
21 changes: 21 additions & 0 deletions 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.