Installation
Add the official opentelemetry-ruby gems, configure a Rails initializer, and set environment variables to send traces to Sophonz.
This wires up the upstream opentelemetry-ruby gems — not a Sophonz package. Everything about where telemetry goes and who it belongs to comes from environment variables; the initializer just turns instrumentation on.
Quick start
Enter the collector URL, project, app name, version and app key below and pick a tab (Rails · Plain Ruby) to generate the initialization code from those values. Advanced options are behind the "Advanced options" toggle.
# config/initializers/opentelemetry.rb
require "opentelemetry/sdk"
require "opentelemetry/exporter/otlp"
require "opentelemetry/instrumentation/all"
ENV["OTEL_SERVICE_NAME"] ||= "next-sample"
ENV["OTEL_EXPORTER_OTLP_ENDPOINT"] ||= "https://in.sophonz.ai"
ENV["OTEL_EXPORTER_OTLP_PROTOCOL"] ||= "http/protobuf"
ENV["OTEL_EXPORTER_OTLP_HEADERS"] ||= "authorization=sk_tYtJUa4WweXXedj6lxjSqoPW5O0DnbXz"
ENV["OTEL_RESOURCE_ATTRIBUTES"] ||= "service.key=sk_tYtJUa4WweXXedj6lxjSqoPW5O0DnbXz,service.namespace=sophonz,service.version=1.0.0"
OpenTelemetry::SDK.configure do |c|
c.service_name = ENV.fetch("OTEL_SERVICE_NAME", "my-rails-app")
c.use_all
endPrerequisites
- Ruby 3.0+ and Rails 6.1+ (also works in a plain, non-Rails Ruby app — see the note below).
- A service registered in the Sophonz console, and the
sk_...API key issued for it.
Install the gems
bundle add opentelemetry-sdk opentelemetry-exporter-otlp opentelemetry-instrumentation-allTIP — Get your API key
Log in to the Sophonz console, register the service, and copy the issued sk_... key. It carries your tenant identity — see service.key below.
Add the initializer
# config/initializers/opentelemetry.rb
require "opentelemetry/sdk"
require "opentelemetry/exporter/otlp"
require "opentelemetry/instrumentation/all"
OpenTelemetry::SDK.configure do |c|
c.service_name = ENV.fetch("OTEL_SERVICE_NAME", "my-rails-app")
c.use_all
endc.use_all enables every OpenTelemetry instrumentation gem that's installed and available — with opentelemetry-instrumentation-all in your Gemfile, that covers Rails, Rack, ActiveRecord, and whichever HTTP client, background job, and cache gems you're already using, without listing them individually.
NOTE — Explicit requires, on purpose
Rails auto-requires everything in your Gemfile via Bundler.require, so in a real Rails app the initializer would still work without the require lines above. They're included anyway (and matter in a plain Ruby script, or if you set require: false for these gems) so OpenTelemetry::SDK.configure can find the OTLP exporter and instrumentation classes no matter how the app is booted.
Configure environment variables
OTEL_SERVICE_NAME=my-rails-app
OTEL_EXPORTER_OTLP_ENDPOINT=https://in.sophonz.ai
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf
OTEL_EXPORTER_OTLP_HEADERS=authorization=sk_your_api_key
OTEL_RESOURCE_ATTRIBUTES=service.key=sk_your_api_key,service.namespace=my-project,deployment.environment.name=production| Variable | Value | Notes |
|---|---|---|
OTEL_SERVICE_NAME | your service name | Also read directly in the initializer above via ENV.fetch. Must match the service name registered in the console — the portal joins telemetry to the registered service on (project, service.name). |
OTEL_EXPORTER_OTLP_ENDPOINT | https://in.sophonz.ai | Base OTLP endpoint. The OTLP exporter derives /v1/traces from it. |
OTEL_EXPORTER_OTLP_PROTOCOL | http/protobuf | Read automatically once opentelemetry-exporter-otlp is loaded. |
OTEL_EXPORTER_OTLP_HEADERS | authorization=sk_your_api_key | Sent as an HTTP header on every export request. |
OTEL_RESOURCE_ATTRIBUTES | service.key=sk_your_api_key,service.namespace=...,deployment.environment.name=... | service.key is the tenant identity the collector checks — required. service.namespace groups services into a project; deployment.environment.name labels the environment. |
CAUTION — service.key is required
The collector runs in enforce mode: telemetry without a resolvable service.key resource attribute is dropped silently. Your app keeps running, no exception is raised, and nothing shows up in Sophonz. Always include service.key in OTEL_RESOURCE_ATTRIBUTES, and send the same key as the authorization header via OTEL_EXPORTER_OTLP_HEADERS.
Rails notes
- Controller and view spans come from Rails' own instrumentation, driven by the
ActiveSupport::Notificationsevents Rails already emits — no middleware to add manually. - ActiveRecord query spans work with
pg,mysql2, andsqlite3out of the box oncec.use_allruns. - Outside Rails: for a plain Ruby script or service (Sinatra, a worker process, a Rake task), call the same
OpenTelemetry::SDK.configureblock as early as possible in your boot sequence — there's no Rails-specific requirement,use_allworks the same way. - Background jobs: if you use Sidekiq or Resque, their instrumentation gems (bundled with
opentelemetry-instrumentation-all) are enabled by the samec.use_allcall — job spans appear without extra configuration.
Verify
Start your app with the environment variables set:
OTEL_SERVICE_NAME=my-rails-app \
OTEL_EXPORTER_OTLP_ENDPOINT=https://in.sophonz.ai \
OTEL_EXPORTER_OTLP_PROTOCOL=http/protobuf \
OTEL_EXPORTER_OTLP_HEADERS=authorization=sk_your_api_key \
OTEL_RESOURCE_ATTRIBUTES=service.key=sk_your_api_key,service.namespace=my-project,deployment.environment.name=production \
bin/rails serverIf the OTLP exporter gem isn't loaded, the SDK logs WARN -- : The otlp exporter cannot be configured - please add opentelemetry-exporter-otlp to your Gemfile at boot — a useful signal that something in your Gemfile/require setup is off. Beyond that warning, export failures happen silently: a clean boot with no warnings doesn't guarantee telemetry arrived, since an invalid or missing service.key fails exactly the same way as a working setup. Generate some requests, then check the Sophonz console for the service you registered.
TIP — This is just the beginning
Now ship your app. Sophonz surfaces the problems real users hit — so your app evolves on its own.