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 start by installing rails gem install rails 2\ create an api only application called dice ruby rails new api dice ruby 3\ change into the newly created folder dice ruby and create a controller for rolling a dice cd dice ruby rails generate controller dice 4\ a new file called app/controllers/dice controller rb will be created open this file and update it with the following code class dicecontroller < applicationcontroller def roll render json (rand(6) + 1) to s end end 5\ open the file named config/routes rb and add the following code to it rails application routes draw do get 'rolldice', to 'dice#roll' end 6\ run the application with the following command and open http //localhost 8080/rolldice in your web browser to ensure it is working rails server p 8080 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 bundle add opentelemetry sdk opentelemetry instrumentation all 2\ create a file named config/initializers/opentelemetry rb and add the following code to it \# config/initializers/opentelemetry rb require 'opentelemetry/sdk' require 'opentelemetry/instrumentation/all' opentelemetry sdk configure do |c| c service name = 'dice ruby' c use all() # enables all instrumentation! end step 5 run the instrumented app 1\ run the instrumented application to generate traces env otel exporter otlp endpoint=https //otel kloudmate com 4318 otel exporter otlp headers=”authorization=\<private key>” otel exporter otlp protocol=http/json rails server p 8080 2\ open http //localhost 8080/rolldice in your web browser and reload the page a few times source url for the example application https //opentelemetry io/docs/instrumentation/net/getting started/