Rails 8.1.1 application with OpenTelemetry auto-instrumentation for traces, metrics, and logs.
- HTTP requests and responses
- Database queries (ActiveRecord)
- Background jobs and cache operations
- Action Cable connections
- Distributed trace propagation (W3C)
- Docker Desktop or Docker Engine with Compose
- Base14 Scout OIDC credentials (setup guide)
- Ruby 3.3+ (only for local development without Docker)
# Remove demo credentials
rm config/master.key config/credentials.yml.enc
# Rails will auto-generate master key when you edit credentials
bin/rails credentials:edit
# Never commit config/master.key (already in .gitignore)
# In production, use: RAILS_MASTER_KEY=<your-key> environment variable# Configure Scout credentials in docker-compose.yml
# Update SCOUT_CLIENT_ID, SCOUT_CLIENT_SECRET, SCOUT_TOKEN_URL, SCOUT_ENDPOINT
# Start application
docker-compose up --build
# Verify it's running
curl http://localhost:3000The app runs on port 3000.
| Variable | Required | Description |
|---|---|---|
OTEL_SERVICE_NAME |
Yes | Service name for OpenTelemetry |
SCOUT_CLIENT_ID |
Yes | Base14 Scout OAuth client ID |
SCOUT_CLIENT_SECRET |
Yes | Base14 Scout OAuth client secret |
SCOUT_TOKEN_URL |
Yes | Base14 Scout OAuth token endpoint |
SCOUT_ENDPOINT |
Yes | Base14 Scout OTLP endpoint |
gem 'opentelemetry-sdk'
gem 'opentelemetry-exporter-otlp'
gem 'opentelemetry-instrumentation-all'require 'opentelemetry/sdk'
require 'opentelemetry/exporter/otlp'
OpenTelemetry::SDK.configure do |c|
c.service_name = ENV.fetch('OTEL_SERVICE_NAME', 'rails-app')
# Fetch OIDC token for authentication
token = fetch_oidc_token
headers = { 'Authorization' => "Bearer #{token}" } if token
# Configure OTLP exporter
otlp_exporter = OpenTelemetry::Exporter::OTLP::Exporter.new(
endpoint: ENV.fetch('SCOUT_ENDPOINT'),
headers: headers
)
c.add_span_processor(
OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(otlp_exporter)
)
# Enable all automatic instrumentation
c.use_all
endSee the full implementation in config/initializers/opentelemetry.rb for OIDC token fetching and log correlation.
Automatically included in telemetry:
service.name=ruby-rails8-sqlite-otel
telemetry.sdk.name=opentelemetry
telemetry.sdk.language=rubybundle install # Install dependencies
bin/rails db:setup # Setup database
bin/rails server # Run applicationSet required environment variables before running locally.
docker-compose up --build # Build and start
docker-compose down # Stop all
docker-compose down -v # Stop and remove volumes
docker-compose logs -f web # View logs- HTTP requests (method, URL, status, controller/action)
- Database queries (SQL statements, duration)
- View rendering and background jobs
- Exceptions with stack traces
All Rails logs include trace_id and span_id for correlation. The
initializer extends the Rails logger to automatically add trace context to
every log entry.
docker-compose logs web | grep -i "oidc\|token"Verify Scout credentials are correct and token URL is accessible.
docker-compose logs web | grep -i opentelemetryCheck that Scout endpoint is reachable and OIDC token is being fetched successfully.
In docker-compose.yml:
environment:
- RAILS_LOG_LEVEL=debug| Component | Version |
|---|---|
| Rails | 8.1.1 |
| Ruby | 3.3+ |
| OpenTelemetry SDK | Latest |
| OpenTelemetry Instrumentation | Latest |
| SQLite | 2.1+ |
- Rails Auto-Instrumentation Guide - Base14 documentation
- OpenTelemetry Ruby - OTel Ruby docs
- Base14 Scout - Observability platform