Skip to main content
Sumo Logic

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.

How to instrument your RoR application?

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

  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 RubyGems.org and they can be installed in two different ways:

    • gem command:

      $ gem install opentelemetry-sdk -v 1.0.0.rc2
      $ gem install opentelemetry-exporter-otlp -v 0.20.1

    • bundler, the packages have to be inserted into your gemfile and the bundle install command has to be run:

      gem 'opentelemetry-sdk', '1.0.0.rc2'
      gem 'opentelemetry-exporter-otlp', '0.20.1'

    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.18.1
      $ gem install opentelemetry-instrumentation-mysql2 -v 0.18.1

    • Installation of the "all in one" package - installing this package will install all available instrumentation packages
      $ gem install opentelemetry-instrumentation-all -v 0.19.0

  1. 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'
      end

      Rails.application.initialize!

    • "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|
        c.use_all
      end

      Rails.application.initialize!

  2. Telemetry data exporter configuration

    The final step is to configure the exporter host and service 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_TRACES_EXPORTER=otlp - environment variable sets the exporter to OTLP
    • OTEL_EXPORTER_OTLP_ENDPOINT=http://collection-sumologic-otelcol.sumologic:55681 - environment variable configures the endpoint where telemetry data will be sent.

      In this example, the value of the variable points to the default Sumologic Kubernetes Collector. For Kubernetes environments see the available endpoints for a direct connection. For other environments see endpoints and protocols.
    • OTEL_RESOURCE_ATTRIBUTES=service.name=my-service - configure the service name

  3. Kubernetes metadata configuration

    In scenarios when your application runs on Kubernetes, it is important to insert an additional attribute to the span generated by your microservice that allows the OpenTelemetry collector to properly assign Kubernetes infrastructure metadata.

    The k8s.pod.ip attribute contains the IP address of the pod and the simplest way to configure it in the Kubernetes deployment yaml file is to add the below code into the spec.template.spec.containers section:

    env:
     - name: MY_POD_IP
       valueFrom:
        fieldRef:
         fieldPath: status.podIP
     - name: OTEL_RESOURCE_ATTRIBUTES
       value: "service.name=my-service,k8s.pod.ip=$(MY_POD_IP)"
     - name: OTEL_EXPORTER_OTLP_ENDPOINT
       value: "http://collection-sumologic-otelcol.sumologic:55681"
     - name: OTEL_EXPORTER_OTLP_INSECURE
       value: "true"