Ruby to Logit.io Example

Ship logs and Traces from Ruby to Logit.io

Follow the steps below to send your observability data to Logit.io

Logs

Get started quickly with Ruby logging using our configuration guide on how to easily ship Ruby logs to your hosted ELK Logstash.

Install Integration

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

Simple Ruby Logging

gem install logstash-logger
irb
require 'logstash-logger'
 
tcp_logger = LogStashLogger.new(
  type: :tcp,
  host: '@logstash.host',
  port: "@logstash.sslPort:strip_quotes",
  ssl_enable: true
)
 
tcp_logger.error "This is logged as an error"
tcp_logger.info "This is logged as info"
tcp_logger.debug "This is logged as debug"
tcp_logger.warn "This is logged as a warning"

Check Logit.io for your logs

Data should now have been sent to your Stack.

View My Data

If you don't see take a look at How to diagnose no data in Stack below for how to diagnose common issues.

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.

APM

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.

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.

If you need any more assistance in analysing your Ruby logs we're here to help. Feel free to get in touch by reaching out to our support team via Intercom and we will be happy to assist.