/mongo_profiler

Mongo profiling tool which matches queries with code

Primary LanguageRubyMIT LicenseMIT

Mongo Profiler

Build Status

Mongo profiling tool which matches queries with code

Database profiling tools are awesome and always useful. I love Mongo profiling. But unfortunately these tools don't match the queries with the source code they are profiling, making hard to find where the slow queries are executed.

The Mongo Profiler is a refinement patch in the moped driver to log all executed queries and their respective callers (Ruby backtrace).

It isn't replacement for the Mongo's built-in profiling, it is just a complementary tool to profile the queries with their respective source code.

An interesting feature in the Mongo Profiler is that we can group queries by "life cycles". For example, in a web application it can be the request.uuid or the request.url, so you will be able to see how many queries, how long did they take, the explain plans etc for each request or url.

Sample App

You can see how it works through the Sample Dashboard and Sample App (source code).

Installation

Add this line to your application's Gemfile:

gem 'mongo_profiler'

And then execute:

$ bundle

Or install it yourself as:

$ gem install mongo_profiler

To run the Dashboard you will need also to install sinatra.

gem 'sinatra', require: nil

Usage

Rails application

Gemfile

gem 'mongo_profiler', github: 'phstc/mongo_profiler', require: nil
gem 'sinatra', require: nil

application_controller.rb

# app/controllers/application_controller.rb

class ApplicationController < ActionController::Base

  before_filter unless: -> { Rails.env.production? } do |controller|
    require 'mongo_profiler'
    Thread.current['mongo_profiler_group_name'] = request.url
  end
end

routes.rb

# config/routes.rb

MyApplication::Application.routes.draw do
  unless Rails.env.production?
    require 'mongo_profiler'
    require 'mongo_profiler/web'
    mount MongoProfiler::Web => '/mongo_profiler'
    # Security with Devise
    # authenticate :user do
    #  mount MongoProfiler::Web => '/mongo_profiler'
    # end
    #
    # authenticate :user, lambda { |u| u.admin? } do
    #  mount MongoProfiler::Web => '/mongo_profiler'
    # end
  end
end

Contributing

  1. Fork it ( http://github.com/phstc/mongo_profiler/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request