Skip to content

Ruby

Instrument your Ruby applications to send traces, logs, and metrics to Kopai.

Terminal window
gem install opentelemetry-sdk opentelemetry-exporter-otlp
require 'opentelemetry/sdk'
require 'opentelemetry/exporter/otlp'
endpoint = ENV['OTEL_EXPORTER_OTLP_ENDPOINT'] || 'http://localhost:4318'
service_name = ENV['OTEL_SERVICE_NAME'] || 'my-service'
# Create OTLP exporter with explicit endpoint
exporter = OpenTelemetry::Exporter::OTLP::Exporter.new(
endpoint: "#{endpoint}/v1/traces"
)
OpenTelemetry::SDK.configure do |c|
c.service_name = service_name
c.add_span_processor(
OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(exporter)
)
end
# Get tracer and create spans
tracer = OpenTelemetry.tracer_provider.tracer('my-app')
tracer.in_span('my-operation') do |span|
span.set_attribute('key', 'value')
# ... your code
end

Set the required environment variables and run:

Terminal window
export OTEL_EXPORTER_OTLP_ENDPOINT="https://otlp.kopai.app"
export OTEL_SERVICE_NAME="my-service"
ruby app.rb
VariableDescription
OTEL_EXPORTER_OTLP_ENDPOINTKopai OTLP endpoint
OTEL_SERVICE_NAMEName shown in Kopai UI

For Sinatra, Rails, or other frameworks:

Terminal window
gem install opentelemetry-instrumentation-all
require 'opentelemetry/instrumentation/all'
OpenTelemetry::SDK.configure do |c|
c.use_all
end

For a complete working example:

Ruby Example