OpenTelemetry Support
Using OpenTelemetry SDKs

OTel for Ruby

8min

In this guide, we will walk you through the process of setting up and using OpenTelemetry in Ruby. You will learn how to instrument a simple PHP application to produce traces, metrics, and logs and export them to KloudMate.

Step 1: Prerequisites

Before moving forward ensure that you have the following installed locally:

  • MRI Ruby >= 3.0, jruby >= 9.3.2.0, or truffleruby >= 22.1
  • Bundler

OpenTelemetry support for support for jruby and truffleruby are on a best-effort basis at this time.

Step 2: Example Application

If you already have a Ruby application and don't need to create one from scratch, you can directly jump to the Instrumentation part of this guide.

For this tutorial, we will be using a basic Rails application. However, OpenTelemetry Ruby is compatible with other web frameworks such as Sinatra and Rack as well. Feel free to adapt the instructions to your preferred framework.

Step 3: Installation

  1. Start by installing rails.
Text


2. Create an api-only application called dice-ruby.

Text


3. Change into the newly created folder dice-ruby and create a controller for rolling a dice

Text


4. a new file called app/controllers/dice_controller.rb will be created. Open this file and update it with the following code:

Ruby


5. Open the file named config/routes.rb and add the following code to it:

Ruby


6. Run the application with the following command and open http://localhost:8080/rolldice in your web browser to ensure it is working.

Text


Step 4: Instrumentation

We will be using opentelemetry-sdk and opentelemetry-instrumentation-all packages that provide instrumentations for Rails, Sinatra, several HTTP libraries, and more.

  1. Install the opentelemetry-sdk and opentelemetry-instrumentation-all packages:
Linux


2. Create a file named config/initializers/opentelemetry.rb and add the following code to it:

Text


Step 5: Run the Instrumented App

1. Run the instrumented application to generate traces

Text


2. Open http://localhost:8080/rolldice in your web browser and reload the page a few times.