dawanda/rack-profiler

Name: rack-profiler

Owner: DaWanda Engineering Team

Description: A simple profiler for Rack applications

Created: 2015-01-26 15:52:17.0

Updated: 2016-01-14 14:18:31.0

Pushed: 2016-10-02 19:34:23.0

Homepage: null

Size: 50

Language: Ruby

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Rack::Profiler

Build Status Code Climate Test Coverage Gem Version

Simple profiler for Rack applications (Sinatra, Ruby on Rails, or Grape for example). It helps providing an answer to common questions like:

And more.

Rack::Profiler uses the Active Support Instrumentation API and subscribes by default to the following hooks:

Rack::Profiler also automatically subscribes to Grape's Active Support Instrumentation notifications

On top of this, you can also define your own events, by wrapping your code with the Rack::Profiler.step.

Rack::Profiler is easy to integrate in any Rack application and it produces a JSON response with the results. It also exposes a simple web dashboard to directly issue HTTP requests to your application and see the results of the profiling.

Rack::Profiler Web Dashboard

Installation

Add this line to your application's Gemfile:

'rack-profiler'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rack-profiler
Rack/Sinatra/Grape

In your config.ru use the Rack::Profiler middleware at the beginning of your middleware stack:

ire 'rack/profiler'
Rack::Profiler

NOTE: you should not expose the profiler publicly in the production environment, as it may contain sensitive information. Refer to the authorization section on how to protect it.

Rails

You can add the Rack::Profiler middleware at the beginning of your config.ru like in the Rack/Sinatra installation or insert it in the middlewares stack configuration in your config/environments/<env>.rb files:

App.configure do |config|
...

nfig.middleware.insert 0, Rack::Profiler

NOTE: you should not expose the profiler publicly in the production environment, as it may contain sensitive information. Refer to the authorization section for on to protect it.

Configuration

You can configure Rack::Profiler passing a block to use (or middleware.insert in Rails configuration). In the block you can subscribe to more notifications and change some defaults:

Rack::Profiler do |profiler|
Subscribe to email delivery in a Rails app
ofiler.subscribe('deliver.action_mailer')

You can also exclude lines that are not interesting from the backtrace
For example, exclude gems from the backtrace:
ofiler.filter_backtrace { |line| !line.include? '/gems/' }

Authorization

You typically do not want to expose profiling publicly, as it may contain sensible information about your data and app. To protect your data, the easiest option is to only enable the profiler in the development environment:

NV['RACK_ENV'] == 'development'
quire 'rack/profiler'
e Rack::Profiler

Sometimes though, you might want to run the profiler in the production environment, in order to get results in a real setting (including caching and optimizations). In this case, you can configure your custom authorization logic, which can rely on the Rack env:

Rack::Profiler do |profiler|
ofiler.authorize do |env|
# env is the Rack environment of the request. This block should return a
# truthy value when the request is allowed to be profiled, falsy otherwise.
env['rack-profiler-enabled'] == true
d


.then in your app:
re do
v['rack-profiler-enabled'] = true if current_user.admin?

Usage
Custom steps

By default Rack::Profiler will subscribe to ActiveRecord SQL queries, ActionView rendering events (templates and partials), ActionController actions and to steps you define in your code with:

::Profiler.step('your-step-name') do
Do stuff. The profiler will tell you how long it took to perform this step

Web Dashboard

A graphical interface to profile your application pages/endpoints and display the results is automatically mounted at this route:

http://<your-app-url>/rack-profiler

Just select the HTTP verb, insert the relative path to your app and add some optional parameters like POST/PUT data: Rack::Profiler automatically send the request to your app with the rack-profiler param and display the results in a nice graphical way.

Raw JSON result

If you want to access the results of the profiling as raw JSON data, you can just add the rack-profiler parameter (it can be null) to any HTTP request to your app (GET, POST, PUT, PATCH, DELETE): Rack::Profiler will execute the request and return a JSON response containing the results along with the original response.

http://<your-app-url>/<path>?rack-profiler
Contributing
  1. Fork it ( https://github.com/dawanda/rack-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 a new Pull Request

This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.