Connect Rack to Bugsink

The Ruby SDK’s Rack middleware reports unhandled request exceptions with their HTTP request and application stack.

This guide adds the middleware to an existing Rack application and triggers a deliberate route failure. To follow along, create a Bugsink project and copy its DSN from the project setup page.

Install the SDK

Add the open-source Sentry Ruby SDK to your application’s bundle. Bugsink accepts the Sentry event protocol, so the SDK can send events directly to your Bugsink project:

bundle add sentry-ruby

Configure your DSN

Initialize the SDK in config.ru before the application is loaded. Reading the DSN from the environment keeps it out of source control:

Sentry.init do |config|
  config.dsn = ENV.fetch("SENTRY_DSN")
end

Set SENTRY_DSN to the DSN shown by your Bugsink project when you start the application:

export SENTRY_DSN=https://key@bugsink.example/1

Add the exception middleware

Exception middleware reports a failed Rack request and then re-raises the exception for the rest of the stack. Put it outside the application whose errors it should capture:

use Sentry::Rack::CaptureExceptions

require_relative "app"

run InventoryApp.new

Send a test error

Temporarily raise an exception in a route you can request. This example fails when checkout receives a product that is not in the inventory:

sku = Rack::Utils.unescape_path(match[1])
raise ProductNotFound, "Product #{sku} is not in inventory" unless INVENTORY.key?(sku)

Start the Rack application with its DSN, then request that route once:

bundle exec puma --bind tcp://127.0.0.1:9292 config.ru
:::bash
curl http://127.0.0.1:9292/checkout/rack-missing

The middleware reports the exception and re-raises it. The request therefore keeps its normal 500 response instead of being turned into a success.

See the error in Bugsink

Open your Bugsink project. The new ProductNotFound issue contains the request and points to the application line which raised the exception.

Bugsink preview

That’s it: unhandled Rack request exceptions will now be reported to Bugsink. Remove the deliberate exception and keep the middleware around the application code it should observe.

For handled exceptions, messages, tags, structured context, and breadcrumbs, continue with the Ruby guide.