# Ruby Instrument Ruby applications with OpenTelemetry # Integration Instrument your Ruby applications to send traces, logs, and metrics to Kopai. ## Installation ```bash gem install opentelemetry-sdk opentelemetry-exporter-otlp ``` ## Basic Setup ```ruby 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 ``` ## Running Your Application Set the required environment variables and run: ```bash export OTEL_EXPORTER_OTLP_ENDPOINT="https://otlp-http.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 For Sinatra, Rails, or other frameworks: ```bash gem install opentelemetry-instrumentation-all ``` ```ruby require 'opentelemetry/instrumentation/all' OpenTelemetry::SDK.configure do |c| c.use_all end ``` ## Working Example For a complete working example: **[Ruby Example](https://github.com/kopai-app/kopai-integration-examples/tree/main/ruby)**