Ruby on Rails — Lescopr SDK

Learn how to integrate the Lescopr SDK into your Ruby on Rails application for automatic error and exception monitoring.

Introduction

The Lescopr gem integrates seamlessly with Ruby on Rails to provide automatic exception capture and real-time monitoring.

Once configured, every unhandled exception in your application is automatically reported to your Lescopr dashboard — including context such as the request, user, backtrace, and environment.

lescopr is available on RubyGems.org and is compatible with Rails 6.0+.

Requirements

Before installing the Lescopr gem, make sure your environment meets the following requirements:

  • Ruby >= 2.7
  • Rails >= 6.0
  • A valid LESCOPR_SDK_KEY (available from your Lescopr dashboard)
  • Bundler (recommended)

Installation

Add the lescopr gem to your Gemfile:

Then run Bundler to install the dependency:

Alternatively, you can install the gem directly from RubyGems:

Once installed, run the Lescopr installer to generate the initializer:

Gemfile
ruby
gem 'lescopr'
Terminal
bash
bundle install
Terminal
bash
bundle exec rails generate lescopr:install

Configuration

Edit the generated initializer file config/initializers/lescopr.rb to provide your SDK and API keys:

You can also restrict reporting to specific environments:

Available options:

  • sdk_key — Your Lescopr SDK key (required)
  • api_key — Your Lescopr API key (required)
  • environment — The current environment (production, staging, development)
  • enabled_environments — Array of environments where reporting is active (default: ['production', 'staging'])
  • report_rescued_exceptions — Also report exceptions caught with rescue (default: false)
  • logger — Custom logger instance (default: Rails.logger)
config/initializers/lescopr.rb
ruby
Lescopr.configure do |config|
  config.app_key     = ENV['LESCOPR_APP_KEY']
  config.environment = Rails.env
end
config/initializers/lescopr.rb
ruby
Lescopr.configure do |config|
  config.app_key              = ENV['LESCOPR_APP_KEY']
  config.environment          = Rails.env
  config.enabled_environments = %w[production staging]
end

Usage

Once configured, Lescopr automatically captures all unhandled exceptions in your Rails application.

You can also manually report an exception anywhere in your code:

You can attach custom context (user, metadata) to improve traceability:

Context is thread-safe and is cleared automatically after each request.

app/controllers/application_controller.rb
ruby
begin
  # risky operation
rescue => e
  Lescopr.report(e)
  raise
end
app/controllers/application_controller.rb
ruby
before_action :set_lescopr_context

def set_lescopr_context
  Lescopr.set_context(
    user: { id: current_user&.id, email: current_user&.email }
  )
end

Advanced

Filtering exceptions

You can ignore specific exception types that you do not want to report:

Breadcrumbs (user action trail)

Lescopr supports breadcrumbs to track user actions before an error:

Sidekiq integration

The lescopr gem automatically integrates with Sidekiq if it is present. Background job errors are captured with their job context.

config/initializers/lescopr.rb
ruby
Lescopr.configure do |config|
  config.app_key            = ENV['LESCOPR_APP_KEY']
  config.ignored_exceptions = %w[
    ActiveRecord::RecordNotFound
    ActionController::RoutingError
  ]
end
app/models/order.rb
ruby
Lescopr.add_breadcrumb(
  message:  'User clicked checkout',
  category: 'ui.click',
  level:    :info,
  data:     { order_id: id }
)