Ruby SDK
Full observability for Ruby services — HTTP, databases, queues, and logging captured automatically via OpenTelemetry
Installation
Or add to your Gemfile:
Requires Ruby 3.0+.
Minimum Setup
Two fields are required: api_key and service_name. Everything else has defaults.
Built on OpenTelemetry
The Ruby SDK uses opentelemetry-sdk with auto-detection of installed instrumentations. When you initialize the client, it scans for installed OTel instrumentation gems and enables them automatically. The Rails Railtie auto-configures the full OTel pipeline including resource attributes and the OTLP exporter pointed at your Obtrace ingest endpoint.
What's Captured Automatically
After creating an ObtraceSDK::Client, the SDK configures OpenTelemetry and enables all detected instrumentations. You don't need to write any extra code for this:
| What | How | Needs code? |
|---|---|---|
| All raised exceptions | TracePoint(:raise) intercepts every raise and sends it as an error log | No |
| Unhandled fatal exceptions | Fatal exceptions that reach the top of the stack are captured at process exit | No |
| Outbound HTTP | Net::HTTP and Faraday instrumented via OTel with spans and traceparent injection | No |
| Inbound HTTP | rack, rails, sinatra requests instrumented via OTel | No |
| Databases | pg, mysql2 queries instrumented with spans when OTel gems are installed | No |
| Redis | redis gem operations instrumented via OTel | No |
| Sidekiq | Job execution instrumented with spans via OTel | No |
| gRPC | Client and server calls instrumented via OTel | No |
| RabbitMQ | bunny gem operations instrumented via OTel | No |
| Logger output | Logger instances are intercepted and forwarded as structured logs with level mapping | No |
| Shutdown flush | at_exit flushes buffered telemetry on process exit | No |
Opting Out of Auto HTTP Instrumentation
Opting Out of Logger Capture
Optional: Custom Telemetry
The automatic exception, HTTP, and Logger capture covers basic observability. For business-specific events, use the SDK methods directly.
Logging
Use client.log for structured events. Logs are best for state transitions, errors, and audit trails.
Levels: trace, debug, info, warn, error, fatal.
Metrics
Use client.metric for numerical measurements you want to track over time.
The unit parameter follows OTLP conventions: "ms" for milliseconds, "By" for bytes, "1" for dimensionless values.
Tracing / Spans
Use client.span for operations with duration. Spans appear in the trace waterfall and are best for database queries, HTTP calls, and queue processing.
Framework Integration
Rails (Railtie Auto-Init)
The SDK includes a Rails Railtie that automatically initializes when Rails boots. Add your credentials to config/credentials.yml.enc or environment variables, then add the gem to your Gemfile:
Set the required environment variables:
The Railtie auto-initializes ObtraceSDK::Client, configures OpenTelemetry with the OTLP exporter, enables all detected OTel instrumentations (ActiveRecord, ActionPack, Rack, Net::HTTP, Faraday, Redis, Sidekiq, pg, mysql2, etc.), registers Rack middleware for request instrumentation, and wires up shutdown flushing. No initializer file needed.
To access the client instance:
Rails (Manual)
If you prefer manual setup over the Railtie:
Register in config/application.rb:
Rack
For Sinatra or plain Rack apps, use the same middleware pattern:
Sidekiq / Background Jobs
For background workers, instrument each job and flush periodically:
Add a Sidekiq middleware to flush after each job:
Configuration Reference
| Parameter | Type | Required | Default | Description |
|---|---|---|---|---|
api_key | String | Yes | Obtrace API key | |
service_name | String | Yes | Stable name for this service | |
tenant_id | String? | No | from key | Tenant identifier |
project_id | String? | No | from key | Project identifier |
app_id | String? | No | Application identifier within the project | |
env | String | No | "production" | Deployment environment |
service_version | String? | No | Version string for this deployment | |
auto_instrument_http | Boolean | No | true | Patch Net::HTTP with automatic spans and trace propagation |
auto_capture_logger | Boolean | No | true | Intercept Logger output and forward as structured logs |
validate_semantic_metrics | Boolean | No | false | Warn on non-standard metric names |
debug | Boolean | No | false | Enable verbose logging to stderr |
max_queue_size | Integer | No | 2048 | Maximum buffered items before dropping |
flush_interval_ms | Integer | No | 5000 | Auto-flush interval in milliseconds |
Validation Checklist
After deploying, verify:
service_name,env, andservice_versionare stable across restarts and match what you see in the Obtrace UI- At least one real request path emits both a log and a span
- No
401or403errors appear in stderr whendebugistrue - The
at_exithook is firing (check that telemetry arrives from short-lived scripts without explicitflushcalls) - Outbound Net::HTTP calls show auto-generated spans
- Logger output appears as structured logs in the Obtrace UI
- Sidekiq or other long-running workers flush after each job rather than relying solely on the exit hook
- Metrics use OTLP-standard units (
ms,By,1) not custom strings