OpenTelemetry Ruby Configuration

Ship traces from Ruby to OpenSearch with OpenTelemetry

Use OpenTelemetry to easily send Ruby traces to your Logit.io Stack.

This sample app was created and tested with ruby 3.3.4 (2024-07-09 revision be1089c8ec) [x64-mingw-ucrt]

Install Integration

Please click on the Install Integration button to configure your stack for this source.

Install

Create a new directory for your project and call the new directory opentelemetry_example.

Open a Terminal window or Command Prompt and navigate into the new opentelemetry_example directory. Then enter the following command.

bundle init

You will now see a Gem file has been created inside the folder.

Create two new files in the folder and call them app.rb and config.rb. There should now be a total of three files in the opentelemetry_example folder.

Configuring the App

Open the Gem file and paste in the following code, overwriting the existing content.

# frozen_string_literal: true
 
source 'http://www.rubygems.org'
 
# gem "rails"
 
gem 'opentelemetry-sdk'
gem 'opentelemetry-exporter-otlp'
gem 'opentelemetry-instrumentation-all'

Install the Gems with the following command in Terminal or the Command prompt.

bundle install

Using OpenTelemetry

Copy and Paste the code below into config.rb.

⚠️

You will need to update any placeholder variables with your OpenTelemetry details.

config.rb
# config.rb
 
# Define variables
OTLP_ENDPOINT_URL = "https://@opentelemetry.endpointAddress"
PORT = "@opentelemetry.httpsPort"
OTLP_USERNAME = "@opentelemetry.username"
OTLP_PASSWORD = "@opentelemetry.password"

Finally copy and paste the code below into app.rb

app.rb
# app.rb
 
require_relative 'config'
 
require "sinatra"
require 'opentelemetry/sdk'
require 'opentelemetry/exporter/otlp'
require 'base64'
 
# Configuration Parameters
ENV['OTEL_EXPORTER_OTLP_ENDPOINT'] = "#{OTLP_ENDPOINT_URL}:#{PORT}"
ENV['OTEL_EXPORTER_OTLP_HEADERS'] = "Authorization = Basic " + Base64.strict_encode64("#{OTLP_USERNAME}:#{OTLP_PASSWORD}")
ENV['OTEL_SERVICE_NAME'] = "LogitTestApp"
 
# Configure the OpenTelemetry SDK with OTLP Exporter
OpenTelemetry::SDK.configure do |c|
  c.use_all() # Enable all available instrumentation
end
 
# Endpoint to be called to add traces
get "/" do
tracer = OpenTelemetry.tracer_provider.tracer('LogitTestService')
 
# Start the first span
tracer.in_span('Logit Span Test') do |span|
  span.set_attribute('First Message', 'This is a test!')
  span.set_attribute('Second Message', 'Message sent at ' + Time.now.utc.strftime('%Y-%m-%dT%H:%M:%S'))
 
  # Start the second span within the context of the first span
  tracer.in_span('Logit Span Test 2') do |child_span|
    child_span.set_attribute('First Message', 'This is another test!')
    child_span.set_attribute('Second Message', 'Message sent at ' + Time.now.utc.strftime('%Y-%m-%dT%H:%M:%S'))
  end
end
  
 
end
 
# Message when starting app
puts "Starting Logit Test App"

Run the Ruby App

Run the Ruby app with the following command in Terminal or the Command Prompt window.

ruby app.rb

You will see feedback from the app so that you know that is running, make a note of the endpoint that it is listening on.

Ruby Endpoint

Open a new Terminal or Command Prompt window and make a curl request to the listening endpoint

curl http://127.0.0.1:4567

Traces will now have been sent to your Stack.

Launch Logit.io to view your traces

Launch APM

How to diagnose no data in Stack

If you don't see data appearing in your stack after following this integration, take a look at the troubleshooting guide for steps to diagnose and resolve the problem or contact our support team and we'll be happy to assist.