Sinatra — Lescopr SDK

Integrate the Lescopr SDK into your Sinatra application for automatic exception capture and real-time monitoring, from installation to configuration.

Introduction

The Lescopr gem supports Sinatra through a dedicated extension — Lescopr::Integrations::Sinatra::Extension.

Once registered, all unhandled exceptions in your Sinatra routes are automatically captured and reported to your Lescopr dashboard.

lescopr is available on RubyGems.org and works with Sinatra 2.0+.

Requirements

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

  • Ruby >= 2.7
  • Sinatra >= 2.0
  • A valid LESCOPR_SDK_KEY and LESCOPR_API_KEY (available from your Lescopr dashboard)
  • Bundler (recommended)

Installation

Add the lescopr gem to your Gemfile:

Then run Bundler:

Or install it directly:

Gemfile
ruby
gem 'lescopr'
Terminal
bash
bundle install
Terminal
bash
gem install lescopr

Configuration

Initialise the SDK before your application boots, then register the extension on your Sinatra class:

You can use environment variables to keep your keys out of version control:

Available options:

  • sdk_key — Your Lescopr SDK key (required)
  • api_key — Your Lescopr API key (required)
  • environment — The current environment (default: ENV['RACK_ENV'])
app.rb
ruby
require 'sinatra'
require 'lescopr'

Lescopr.init!(
  sdk_key: ENV['LESCOPR_SDK_KEY'],
  api_key: ENV['LESCOPR_API_KEY']
)

class MyApp < Sinatra::Base
  register Lescopr::Integrations::Sinatra::Extension

  get '/' do
    lescopr_log(:info, 'Home page visited')
    'Hello!'
  end
end
.env
bash
LESCOPR_SDK_KEY=lsk_xxxx
LESCOPR_API_KEY=lak_xxxx
LESCOPR_ENVIRONMENT=production

Usage

After registering the extension, all unhandled exceptions are captured automatically.

You can also log events manually from any route using the lescopr_log helper:

Or call the SDK directly from anywhere in your code:

app.rb
ruby
get '/orders' do
  lescopr_log(:info, 'Orders listed', { user_id: session[:user_id] })
  # ...
end
app.rb
ruby
begin
  risky_operation
rescue => e
  Lescopr.report(e)
  raise
end

Advanced

Rack middleware

You can also use the lower-level Rack middleware directly in config.ru, which works with any Rack-compatible framework:

Environment variables

  • LESCOPR_SDK_KEY — SDK key (overrides .lescopr.json)
  • LESCOPR_API_KEY — API key
  • LESCOPR_ENVIRONMENT — development or production
  • LESCOPR_DEBUG=true — Enables verbose output to .lescopr.log
config.ru
ruby
require 'lescopr'

Lescopr.init!(sdk_key: ENV['LESCOPR_SDK_KEY'], api_key: ENV['LESCOPR_API_KEY'])

use Lescopr::Integrations::Rack::Middleware

run MyApp