Skip to main content
⏳ Estimated read time: 1 min read

Ruby

Ruby Tracing Instrumentation

Ruby >= v2.5

OpenTelemetry is the recommended approach to instrument for tracing. Please follow the instructions below to achieve auto-instrumentation:

In the example above

Gemfile

gem 'opentelemetry-sdk'
gem 'opentelemetry-exporter-otlp'
gem 'opentelemetry-instrumentation-all'

config/initializers/opentelemetry.rb

require 'opentelemetry/sdk'
require 'opentelemetry/exporter/otlp'
require 'opentelemetry/instrumentation/all'
OpenTelemetry::SDK.configure do |c|
c.service_name = 'sample-app'
c.use_all() # enables all instrumentation!
end

Ruby < v2.5

OpenTelemetry libraries do not support Ruby version less than 2.5. To get auto-instrumentation in this situation, we recommend using OpenCensus (which was later merged into OpenTelemetry) with the Jaeger exporter until you are able to upgrade.

Here is a sample Rails app auto instrumented:

Gemfile

gem "opencensus"
gem "opencensus-jaeger"

config/application.rb file

require 'rails/all'
require "opencensus/trace/integrations/rails"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
@@ -14,5 +16,20 @@ module SampleApp

...

config.opencensus.trace.default_sampler = OpenCensus::Trace::Samplers::Probability.new(0.5)

config.opencensus.trace.exporter = OpenCensus::Trace::Exporters::JaegerExporter.new(
service_name: 'sample-app',
host: 'localhost', # because otel-collector is running
port: '6831', # because otel-collector is running
protocol_class: ::Thrift::CompactProtocol # currently supporting only compact protocol
)