Ruby
Integration
Section titled “Integration”Instrument your Ruby applications to send traces, logs, and metrics to Kopai.
Installation
Section titled “Installation”gem install opentelemetry-sdk opentelemetry-exporter-otlpBasic Setup
Section titled “Basic Setup”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 endpointexporter = 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 spanstracer = OpenTelemetry.tracer_provider.tracer('my-app')tracer.in_span('my-operation') do |span| span.set_attribute('key', 'value') # ... your codeendRunning Your Application
Section titled “Running Your Application”Set the required environment variables and run:
export OTEL_EXPORTER_OTLP_ENDPOINT="https://otlp.kopai.app"export OTEL_SERVICE_NAME="my-service"ruby app.rb| Variable | Description |
|---|---|
OTEL_EXPORTER_OTLP_ENDPOINT | Kopai OTLP endpoint |
OTEL_SERVICE_NAME | Name shown in Kopai UI |
Auto-Instrumentation
Section titled “Auto-Instrumentation”For Sinatra, Rails, or other frameworks:
gem install opentelemetry-instrumentation-allrequire 'opentelemetry/instrumentation/all'
OpenTelemetry::SDK.configure do |c| c.use_allendWorking Example
Section titled “Working Example”For a complete working example: