Skip to main content

Track errors in Ruby and Rails

Add the postdeploy gem. In Rails it captures unhandled exceptions with no wiring. In Rack apps and scripts, add the middleware or call capture yourself.

Samples run on , facts checked on

Before you begin

  • Ruby 3.0 or later.
  • An Observe key that starts with obs_. Read Get an ingest key.
  • Version 0.2.0 of the gem. It has no runtime dependencies.

Install

Gemfile ruby
# Gemfile
gem "postdeploy", "~> 0.2"

Run bundle install, then set POSTDEPLOY_API_KEY in the environment of every process that reports errors.

Rails

The gem's Railtie adds PostDeploy::RackMiddleware inside ActionDispatch::DebugExceptions and reports errors from Rails.error. It also hooks into ActiveJob and Sidekiq when they are present. Do not add the middleware again.

app/controllers/application_controller.rb ruby
class ApplicationController < ActionController::Base
  before_action do
    PostDeploy.set_user(id: current_user.id, email: current_user.email) if current_user
  end
end

Rack

Add the middleware first. It reports the error with the request method, URL without query string, parameters and allow-listed headers, then raises the error again so your error page still renders.

config.ru ruby
require "postdeploy"

use PostDeploy::RackMiddleware

run lambda { |env|
  PostDeploy.set_user(id: "user_rack_1")
  raise "Checkout failed" if env["PATH_INFO"] == "/boom"
  [200, { "content-type" => "text/plain" }, ["ok"]]
}

Scripts and tasks

Call PostDeploy.flush before a short process exits. Version 0.2.0 does not flush at exit, so a script that ends without it loses its events.

report.rb ruby
require "postdeploy"

PostDeploy.configure do |config|
  config.environment = "production"
  config.release = ENV["GIT_SHA"]
end

PostDeploy.set_user(id: "user_456", email: "[email protected]")
PostDeploy.set_tag("plan", "team")
PostDeploy.set_context("order", { order_id: "ord_789" })

begin
  raise ArgumentError, "Invoice total is negative"
rescue => e
  PostDeploy.capture_exception(e, context: { feature: "billing" })
end

PostDeploy.capture_message("Low disk space", level: "warning")
PostDeploy.flush

Verify

  1. Step 1. Run the script.

    Terminal shell
    POSTDEPLOY_API_KEY=obs_your_key ruby report.rb
  2. Step 2. Open Observe. The table shows "ArgumentError: Invoice total is negative" as an error and "Low disk space" as a warning.

Troubleshoot

A rake task or script reports nothing
Call PostDeploy.flush before the process exits.
Errors in development never arrive
Keep the Railtie's position. A second middleware at the top of the stack runs after DebugExceptions has handled the error.
Events go to the wrong host
Leave config.endpoint and POSTDEPLOY_INGEST_URL unset in production. The default is the PostDeploy ingest host.
The error page still renders
The middleware raises the error again after it reports it. This is expected.

Limits

  • Version 0.2.0 does not flush at process exit.
  • The gem README in 0.2.0 describes an older key format. Keys start with obs_.

Send your first signal today.

14 days free, no card required. Then $29 a month.

Start your 14-day free trial