Opbeat

Build status

Installation

Add the following to your Gemfile:

gem 'opbeat', git: 'https://github.com/opbeat/opbeat-ruby'

The Opbeat gem adhere to Semantic Versioning and so you can safely trust all minor and patch versions (e.g. 3.x.x) to be backwards compatible.

Usage

Rails 3 and Rails 4

Add the following to your config/environments/production.rb:

Rails.application.configure do |config|
  # ...
  config.opbeat.organization_id = 'XXX'
  config.opbeat.app_id = 'XXX'
  config.opbeat.secret_token = 'XXX'
end

Rack

require 'opbeat'

# set up an Opbeat configuration
config = Opbeat::Configuration.new do |conf|
  conf.organization_id = 'XXX'
  conf.app_id = 'XXX'
  conf.secret_token = 'XXX'
end

# start the Opbeat client
Opbeat.start! config

# install the Opbeat middleware
use Opbeat::Middleware

Configuration

Opbeat works with just the authentication configuration but of course there are other knobs to turn. For a complete list, see configuration.rb.

Ignore specific exceptions

config.opbeat.excluded_exceptions += %w{
  ActiveRecord::RecordNotFound
  ActionController::RoutingError
}

Enable in development and other environments

As a default Opbeat only runs in production. You can make it run in other environments by adding them to the enabled_environments whitelist.

config.opbeat.enabled_environments += %w{development}

Sanitizing data

Opbeat can strip certain data points from the reports it sends like passwords or other sensitive information. If you're on Rails the list will automatically include what you have in config.filter_parameters.

Add or modify the list using the filter_parameters configuration:

config.opbeat.filter_parameters += [/regex(p)?/, "string", :symbol]

User information

Opbeat can automatically add user information to errors. By default it looks for at method called current_user on the current controller. To change the method use current_user_method.

config.opbeat.current_user_method = :current_employee

Background processing

Opbeat automatically catches exceptions in delayed_job or sidekiq.

To enable Opbeat for resque, add the following (for example in config/initializers/opbeat_resque.rb):

require "resque/failure/multiple"
require "opbeat/integration/resque"

Resque::Failure::Multiple.classes = [Resque::Failure::Opbeat]
Resque::Failure.backend = Resque::Failure::Multiple

Manual profiling

It's easy to add performance tracking wherever you want using the Opbeat module.

Here, for example is how you could profile a Sidekiq worker job:

class MyWorker
  include Sidekiq::Worker

  def perform
    Opbeat.transaction "MyWorker#perform", "worker.sidekiq" do
      User.find_each do |user|
        Opbeat.trace 'run!' do 
          user.sync_with_payment_provider!
        end
      end
    end.submit(true)
    # `true` here is the result of the transaction
    # eg 200, 404 and so on for web requests but
    # anything that translates into JSON works

    Opbeat.flush_transactions # send transactions right away
  end
end

Everything that Opbeat knows will also be automatically traced, like ActiveRecord, Redis or Net::HTTP.

Testing and development

$ bundle install
$ rspec spec

Legacy

Be aware that 3.0 is a almost complete rewrite of the Opbeat ruby client. It is not api compliant with version 2 and below.

Resources