Hanami — Lescopr SDK

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

Introduction

The Lescopr gem integrates with Hanami through the standard Rack middleware stack.

Hanami is fully Rack-compatible, so Lescopr::Integrations::Rack::Middleware can be mounted directly and will capture all unhandled exceptions across your slices.

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

Requirements

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

  • Ruby >= 2.7
  • Hanami >= 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 in your Hanami app boot file (e.g. config/app.rb or a dedicated provider) and add the middleware to the stack:

Set your keys via environment variables or .lescopr.json:

Available options:

  • sdk_key — Your Lescopr SDK key (required)
  • api_key — Your Lescopr API key (required)
  • environment — The current environment (default: ENV['HANAMI_ENV'])
config/providers/lescopr.rb
ruby
Hanami.app.register_provider(:lescopr) do
  start do
    require 'lescopr'
    Lescopr.init!(
      sdk_key: ENV['LESCOPR_SDK_KEY'],
      api_key: ENV['LESCOPR_API_KEY'],
      environment: ENV['HANAMI_ENV']
    )
  end
end
config/app.rb
ruby
module MyApp
  class App < Hanami::App
    config.middleware.use Lescopr::Integrations::Rack::Middleware
  end
end
.env
bash
LESCOPR_SDK_KEY=lsk_xxxx
LESCOPR_API_KEY=lak_xxxx
HANAMI_ENV=production

Usage

Once the middleware is mounted, all unhandled exceptions are captured automatically across every slice.

You can also log custom events from any action or interactor:

The daemon flushes the queue to the Lescopr API every 5 seconds in a background thread — completely non-blocking.

app/actions/orders/index.rb
ruby
module MyApp
  module Actions
    module Orders
      class Index < MyApp::Action
        def handle(request, response)
          Lescopr.log(:info, 'Orders listed', { user_id: request.session[:user_id] })
          # ...
        end
      end
    end
  end
end

Advanced

Container registration (Hanami 2)

You can register the Lescopr client as a provider in config/providers/lescopr.rb so it is available via dependency injection throughout your slices.

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/providers/lescopr.rb
ruby
Hanami.app.register_provider(:lescopr) do
  start do
    require 'lescopr'
    client = Lescopr.init!(
      sdk_key: ENV['LESCOPR_SDK_KEY'],
      api_key: ENV['LESCOPR_API_KEY']
    )
    register('lescopr.client', client)
  end
end