OTel for Ruby
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
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
- Start by installing rails.
2. Create an api-only application called dice-ruby.
3. Change into the newly created folder dice-ruby and create a controller for rolling a dice
4. a new file called app/controllers/dice_controller.rb will be created. Open this file and update it with the following code:
5. Open the file named config/routes.rb and add the following code to it:
6. Run the application with the following command and open http://localhost:8080/rolldice in your web browser to ensure it is working.
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.
- Install the opentelemetry-sdk and opentelemetry-instrumentation-all packages:
2. Create a file named config/initializers/opentelemetry.rb and add the following code to it:
Step 5: Run the Instrumented App
1. Run the instrumented application to generate traces
2. Open http://localhost:8080/rolldice in your web browser and reload the page a few times.