Skip to main content

Ruby on Rails OpenTelemetry auto-instrumentation

Opentelemetry-Ruby gives the possibility to obtain telemetry data from Ruby on Rails (RoR) applications by adding a few lines of code to your project.

There are a few simple steps to instrument the application and export the telemetry data by OpenTelemetry Protocol exporter.

Step 1. OpenTelemetry dependencies installation

Installation of the packages listed below is required to apply the instrumentation and export telemetry data. All listed gems are located on and they can be installed in two different ways:

  • gem command:

    gem install opentelemetry-sdk -v 1.4.0
    gem install opentelemetry-exporter-otlp -v 0.26.3
  • bundler, the packages have to be inserted into your gemfile and the bundle install command has to be run:

    gem 'opentelemetry-sdk', '1.4.0'
    gem 'opentelemetry-exporter-otlp', '0.26.3'

Installation of the gems above is mandatory. The next step is to install instrumentation packages corresponding to the libraries used in the application. A complete list of available plugins can be found here.

There are two solutions:

  • Installation of the specific package - for example, if the application is a Rails Web server that is also performing some database queries using MySQL package. To get traces from libraries used in the project, corresponding instrumented packages have to be installed:

    gem install opentelemetry-instrumentation-rails -v 0.30.0
    gem install opentelemetry-instrumentation-action_pack -v 0.9.0
    gem install opentelemetry-instrumentation-action_view -v 0.7.0
    gem install opentelemetry-instrumentation-active_job -v 0.7.1
    gem install opentelemetry-instrumentation-active_model_serializers -v 0.20.1
    gem install opentelemetry-instrumentation-active_record -v 0.7.0
    gem install opentelemetry-instrumentation-mysql2 -v 0.27.0
  • Installation of the "all in one" package - installing this package will install all available instrumentation packages:

    gem install opentelemetry-instrumentation-all -v 0.60.0

Step 2. Code changes

To enable instrumentation in the Ruby on Rails application and export the telemetry data it is enough to add the code below to the project. Two examples below present a specific package and an "all in one" instrumentation. The code has to be added in the config/environment.rb file before application initialization as in the examples below:

  • Specific package instrumentation - in this example only Rails library will be instrumented:

    require 'opentelemetry/sdk'
    require_relative 'application'

    OpenTelemetry::SDK.configure do |c|
    c.use 'OpenTelemetry::Instrumentation::Rails'
    c.use 'OpenTelemetry::Instrumentation::ActionPack'
    c.use 'OpenTelemetry::Instrumentation::ActionView'
    c.use 'OpenTelemetry::Instrumentation::ActiveJob'
    c.use 'OpenTelemetry::Instrumentation::ActiveModelSerializers'
    c.use 'OpenTelemetry::Instrumentation::ActiveRecord'

  • "All in one" instrumentation - this configuration will instrument Rails and other supported and used in project libraries:

    require 'opentelemetry/sdk'
    require_relative 'application'

    OpenTelemetry::SDK.configure do |c|


Step 3. Telemetry data exporter configuration

The final step is to configure the exporter host, service and application name. This can be done directly in the code or by environment variables. In this example, the exporter will be configured by environment variables.

  • OTEL_EXPORTER=otlp - environment variable sets the exporter to OTLP.

  • OTEL_EXPORTER_OTLP_ENDPOINT=http://OTLP_ENDPOINT:4318 - environment variable configures the endpoint where telemetry data will be sent.

This should be OpenTelemetry Collector/Agent endpoint address or OTLP/HTTP source. For Kubernetes environments, see the available endpoints for a direct connection. For other environments see endpoints and protocols.

  • OTEL_SERVICE_NAME=SERVICE_NAME - configure the service name. Ensure the string value represents its business logic, such as "FinanceServiceCall". This will appear as a tracing service name in Sumo Logic.

  • OTEL_RESOURCE_ATTRIBUTES=application=APPLICATION_NAME - configure the application name. This will appear as a tracing application name in Sumo Logic. Additional attributes can be added here as comma-separated key=value pairs. Add the deployment.environment=[environment-name] tag as needed to allow for filtering by environment on dashboard panels. For more information, see Services Dashboard Panels.

TraceID, SpanID and operation data injection into logs

Additional information like TraceID, SpanID or operation data in the application logs could be very helpful to correlate traces, spans and operations with specific logs. To add this data to the logs it is required to obtain it from OpenTelemetry::Trace and modify logger.

Please see example code:

logger =
logger.formatter = proc do |severity, time, progname, msg|
span_id = OpenTelemetry::Trace.current_span.context.hex_span_id
trace_id = OpenTelemetry::Trace.current_span.context.hex_trace_id
if defined?
operation =
operation = 'undefined'

"#{time}, #{severity}: #{msg} - trace_id=#{trace_id} - span_id=#{span_id} - operation=#{operation}\n"
set :logger, logger

Example output:

"2022-02-28 13:01:25 +0000, INFO: {:remote_ip=>\"\", :request_path=>\"/get_beans\", :query_string=>\"\", :request_method=>\"POST\", :execution_time_sec=>0.00033453479409217834, :response_status_code=>200} - trace_id=cdd460d538917f82560cbb91373a05a6 - span_id=12a09921c89fd6e9 - operation=POST /get_beans
Privacy Statement
Terms of Use

Copyright © 2024 by Sumo Logic, Inc.