Home/Documentation/Ruby/Ruby on Rails

Ruby / Rails

Instrument Rails APIs.

Capture matched Action Dispatch routes, controller actions and customer context through an automatically registered Railtie.

Rails 7.1–8.xSDK 0.1Production ready
01

Gem

Install one adapter.

Ruby Core, Rack middleware and compatible Rails dependencies are resolved automatically.

Terminalbash
bundle add apirelio-rails
02

Railtie

Configure one initializer.

The Railtie inserts Apirelio into the Rails middleware stack automatically.

config/initializers/apirelio.rbruby
# config/initializers/apirelio.rb
Apirelio::Rails.configure do |config|
  config.api_key = ENV.fetch("APIRELIO_API_KEY", "")
  config.endpoint = ENV.fetch("APIRELIO_ENDPOINT", "https://apirelio.com")
  config.service = "billing-api"
  config.environment = Rails.env
  config.release = ENV["APIRELIO_RELEASE"]
  config.include_routes = ["/api/**"]
  config.exclude_routes = ["/api/health", "/api/internal/**"]
  config.metadata_keys = ["region"]
end
03

B2B context

Resolve stable identities.

Run authentication before Apirelio completes the request, then expose the current account and API client through the Rack environment or another request-local source.

Identity resolversruby
Apirelio::Rails.configure do |config|
  # ...connection options...
  config.customer_resolver = ->(env) do
    account = env["current_account"]
    account && {
      id: account.id.to_s,
      name: account.name,
      plan: account.plan
    }
  end

  config.application_resolver = ->(env) do
    client = env["current_api_client"]
    client && { id: client.id.to_s, name: client.name }
  end
end
04

Safe enrichment

Add bounded operational context.

Only scalar keys included in metadata_keys are retained. Prefer stable error codes over exception messages.

Controller contextruby
class Api::InvoicesController < ApplicationController
  def create
    Apirelio::Rails.context(request.env)
      &.add_metadata(region: "eu-central")
      &.set_error_code("PAYMENT_REQUIRED")

    head :accepted
  end
end
05

Lifecycle

Resolved, asynchronous and fail-safe.

The integration records the final status, duration, matched Rails route and controller action. Events enter a bounded background queue; resolver and delivery failures never replace the original response or exception.

Privacy boundary

Request and response bodies, query strings, credentials, cookies and client IP addresses are never included.