Skip to content

OpenTelemetry guide

OpenTelemetry for Ruby on Rails

An initializer and three gems give a Rails app spans for every controller action, ActiveRecord query, outgoing HTTP call and Sidekiq job.

3 min read11 sectionsUpdated

On this page

Key points

  • opentelemetry-instrumentation-all brings Rails, ActiveRecord, Net::HTTP, Redis and Sidekiq instrumentation.
  • c.use_all in an initializer turns them on.
  • Sidekiq jobs join the request trace that enqueued them.
  • Puma clustered mode works as is: the batch processor restarts after fork.

Add the gems

Gemfileruby
# Gemfile
gem "opentelemetry-sdk"
gem "opentelemetry-exporter-otlp"
gem "opentelemetry-instrumentation-all"

Initializer

opentelemetry.rbruby
# config/initializers/opentelemetry.rb
require "opentelemetry/sdk"
require "opentelemetry/exporter/otlp"
require "opentelemetry/instrumentation/all"

OpenTelemetry::SDK.configure do |c|
  c.service_name = "storefront"
  c.service_version = ENV.fetch("GIT_SHA", "dev")
  c.use_all  # Rails, ActionPack, ActiveRecord, Net::HTTP, Redis, Sidekiq, ...
end
bash
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
OTEL_RESOURCE_ATTRIBUTES=deployment.environment.name=production

Puma and forking

The Ruby SDK's batch span processor notices when the process has forked and starts a fresh export thread in each worker, so Puma clustered mode with preload_app! works without extra setup. If you only ever see spans from one process, check that the initializer actually runs (for example that the gems are not in a group excluded in production) and that nothing calls OpenTelemetry::SDK.configure a second time with different settings.

Reading ActiveRecord spans

Each SQL statement appears as a span under the controller action. Long runs of similar SELECT spans are the classic N+1; fix with includes, preload or eager_load, and compare the action's p95 before and after. See how to find slow database queries.

Sidekiq

Sidekiq instrumentation propagates context through job payloads, so a job's spans join the trace of the request that enqueued it, and job execution time and failures are visible per job class.

Send it to VitalRix

Shortcut. The VitalRix package does all of the above for you: gem "vitalrix", then Vitalrix.init in an initializer: it turns on every installed instrumentation and sends traces, metrics and logs. It reads VITALRIX_URL, VITALRIX_API_KEY, OTEL_SERVICE_NAME and DEPLOYMENT_ENVIRONMENT.

Directly. VitalRix accepts OTLP over HTTP with protobuf or JSON, which is what the Ruby exporter sends. Instead of a Collector at localhost:4318, point the exporter at VitalRix with the per-signal endpoints and your API key. Create the key in VitalRix under Settings, API keys, with trace, metric and log ingestion.

bash
export OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
export OTEL_EXPORTER_OTLP_TRACES_ENDPOINT=https://api.vitalrix.com/api/v1/traces/otlp/v1/traces
export OTEL_EXPORTER_OTLP_METRICS_ENDPOINT=https://api.vitalrix.com/api/v1/metrics/otlp/v1/metrics
export OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=https://api.vitalrix.com/api/v1/logs/otlp/v1/logs
export OTEL_EXPORTER_OTLP_HEADERS=x-api-key=your_api_key

Through a Collector. Run an OpenTelemetry Collector next to your app, or as a shared service, when the SDK exports over gRPC or you want one place for batching, sampling and retries. It receives from the SDK and forwards to VitalRix with your key.

otel-collector.yamlyaml
receivers:
  otlp:
    protocols:
      grpc: { endpoint: 0.0.0.0:4317 }
      http: { endpoint: 0.0.0.0:4318 }
processors:
  memory_limiter: { check_interval: 1s, limit_mib: 512 }
  batch: {}
exporters:
  otlphttp/vitalrix:
    traces_endpoint: ${env:VITALRIX_API}/api/v1/traces/otlp/v1/traces
    metrics_endpoint: ${env:VITALRIX_API}/api/v1/metrics/otlp/v1/metrics
    headers:
      x-api-key: ${env:VITALRIX_KEY}
service:
  pipelines:
    traces:  { receivers: [otlp], processors: [memory_limiter, batch], exporters: [otlphttp/vitalrix] }
    metrics: { receivers: [otlp], processors: [memory_limiter, batch], exporters: [otlphttp/vitalrix] }
bash
docker run -d --name otel-collector -p 4317:4317 -p 4318:4318 \
  -e VITALRIX_API=https://api.vitalrix.com -e VITALRIX_KEY=your_api_key \
  -v "$PWD/otel-collector.yaml:/etc/otelcol-contrib/config.yaml" \
  otel/opentelemetry-collector-contrib:latest

Check that it works

  1. Send a few requests to the app.
  2. In VitalRix, open APM: storefront appears in the service catalog within a minute, with request rate, error rate and p95.
  3. Open an endpoint and then one request to see its trace waterfall, including database spans.
  4. If nothing arrives, check the exporter's (or the Collector's) logs for export errors: 401 means the key or header is wrong, 402 that a new host is over the plan's APM host allowance. The packages have a check command that sends one test log.

Add service.version so new releases appear as deploy markers, and put the trace ID in your logs so each trace shows its log lines. See APM and logs, metrics and traces.

Frequently asked questions

Do I need instrumentation-all?

It is the easiest start. Later you can depend on individual instrumentation gems and use c.use for each, to keep the bundle smaller.

Does it work with Rails API-only apps?

Yes. The ActionPack and Rack instrumentation trace requests regardless of view rendering.

  • Ruby and Rails performance monitoring: Monitor Ruby on Rails apps: request latency per action, Puma thread and worker saturation, ActiveRecord queries, Sidekiq queues, memory bloat and GC.
  • OpenTelemetry for FastAPI: Trace a FastAPI service with OpenTelemetry: instrument the app, async database drivers and httpx, run uvicorn workers correctly and export.
  • OpenTelemetry for Go: Instrument a Go service with OpenTelemetry: set up the tracer provider, wrap net/http with otelhttp, pass context everywhere and export the spans.
  • OpenTelemetry for PHP and Laravel: Trace a Laravel app with OpenTelemetry: install the PHP extension and auto-instrumentation packages, configure PHP-FPM and trace queries.
  • OpenTelemetry for NestJS: Trace a NestJS service with OpenTelemetry: load the SDK before Nest boots, instrument HTTP, Express and NestJS core, and export JSON to VitalRix.
  • OpenTelemetry for Flask: Instrument a Flask app with OpenTelemetry, in code or with zero code: trace routes, SQLAlchemy and outgoing calls, and export to VitalRix.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.