Peek

Build Status Gem Version Inline docs

Take a peek into your Rails application.

Preview

This is a profiling tool originally built at GitHub to help us get an insight into our application. Now, we have extracted this into Peek, so that other Rails applications can experience the same benefit.

Peek puts a little bar on top of your application to show you all sorts of helpful information about your application. From the screenshot above, you can see that Peek provides information about database queries, cache, Resque workers and more. However, this is only part of Peek's beauty.

The true beauty of Peek lies in the fact that it is an extensible platform. If there are some performance metrics that you need but are not available on Peek, you can find them in the list of available Peek Views and integrate them into Peek. Even if you do not find what you want on Peek Views, you can always create your own.

Installation

Add this line to your application's Gemfile:

gem 'peek'

And then execute:

$ bundle

Or install it yourself as:

$ gem install peek

Usage

Now that Peek is installed, you'll need to mount the engine within your config/routes.rb file:

Some::Application.routes.draw do
  mount Peek::Railtie => '/peek'
  root to: 'home#show'
end

To pick which views you want to see in your Peek bar, just create a file at config/initializers/peek.rb that has a list of the views you'd like to include:

Peek.into Peek::Views::Git, nwo: 'github/janky'
Peek.into Peek::Views::Mysql2
Peek.into Peek::Views::Redis
Peek.into Peek::Views::Dalli

Feel free to pick and install from the list or create your own. The order they are added to Peek is the order they will appear in your bar.

Next, to render the Peek bar in your application, add the following snippet just after the opening <body> tag in your application layout.

<%= render 'peek/bar' %>

It will look like:

<html>
  <head>
    <title>Application</title>
  </head>
  <body>
    <%= render 'peek/bar' %>
    <%= yield %>
  </body>
</html>

Peek fetches the data collected throughout your requests by using the unique request id that was assigned to the request by Rails. It will call out to its own controller at Peek::ResultsController which will render the data and be inserted into the bar.

Now that you have the partials in your application, you will need to include the CSS and JS that help make Peek :sparkles:

In app/assets/stylesheets/application.scss:

//= require peek

In app/assets/javascripts/application.coffee:

#= require jquery
#= require jquery_ujs
#= require peek

Note: Each additional view may have their own CSS and JS that you may need to require which should be stated in their usage documentation.

Configuring the default adapter

For Peek to work, it keeps track of all requests made in your application so it can report back and display that information in the Peek bar. By default it stores this information in memory, which is not recommended for production environments.

In production environments you may have application servers on multiple hosts. Peek will not be able to access the request data if it was saved in memory on another host. Peek provides additional adapters for multi server environments.

You can configure which adapter Peek uses by updating your application config or an individual environment config file. We'll use production as an example.

Note: Peek does not provide the dependencies for each of these adapters. If you use these adapters be sure to include their dependencies in your application.

Peeked::Application.configure do
  # ...

  # Redis with no options
  config.peek.adapter = :redis

  # Redis with options
  config.peek.adapter = :redis, {
    client: Redis.new,
    expires_in: 60 * 30 # => 30 minutes in seconds
  }

  # Memcache with no options
  config.peek.adapter = :memcache

  # Memcache with options
  config.peek.adapter = :memcache, {
    client: Dalli::Client.new,
    expires_in: 60 * 30 # => 30 minutes in seconds
  }

  # Elasticsearch with no options
  config.peek.adapter = :elasticsearch

  # Elasticsearch with options
  config.peek.adapter = :elasticsearch, {
    client: Elasticsearch::Client.new,
    expires_in: 60 * 30, # => 30 minutes in seconds
    index: 'peek_requests_index',
    type: 'peek_request'
  }

  # ...
end

Peek doesn't persist the request data forever. It uses a safe 30 minute cache length so that data will be available if you'd like to aggregate it or use it for other Peek views. You can update this to be 30 seconds if you don't want the data to be available for these or other uses.

Customizing the bar

You can customize the appearance of the bar by customizing it in your own application's CSS.

One common example is fixing the peek bar to the bottom, rather than top, of a page, for use with Bootstrap:

#peek {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 999;
}

Using Peek with PJAX

It just works.

It just works.

Access Control

Peek will only render in development and staging environments. If you'd like to whitelist a select number of users to view Peek in production you can override the peek_enabled? guard in ApplicationController:

class ApplicationController < ActionController::Base
  def peek_enabled?
    current_user.staff?
  end
end

Available Peek views

Feel free to submit a Pull Request adding your own Peek item to this list.

Creating your own Peek item

Each Peek item is a self contained Rails engine which gives you the power to use all features of Ruby on Rails to dig in deep within your application and report it back to the Peek bar. A Peek item is just a custom class that is responsible for fetching and building the data that should be reported back to the user.

There are still some docs to be written, but if you'd like to check out a simple example of how to create your own, just checkout peek-git. To just look at an example view, there is Peek::Views::Git.

Contributing

  1. Fork it
  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