Manual Instrumentation

Warning: Use the manual instrumentation method only if there is no framework support.
  1. Add the following packages to the existing Gemfile.
    gem 'opentelemetry-sdk'
    gem 'opentelemetry-exporter-otlp'
  2. Add the following lines to create spans manually.
    For example, you can create spans to instrument a Ruby script or an unsupported framework.
    require 'opentelemetry/sdk'
    require 'opentelemetry/exporter/otlp'
    
    OpenTelemetry::SDK.configure
    
    def hello_world
      tracer_provider = OpenTelemetry.tracer_provider
      tracer = tracer_provider.tracer('hello world')
      tracer.in_span("hello_world", kind: :server) do |span|
        puts "Hello all"
        # Do something... expensive/simple task that you want to instrument
      end
      tracer_provider.shutdown
    end
    
    hello_world
  3. Set up the OTEL_EXPORTER_OTLP_ENDPOINT environment variable on which the OpenTelemetry Collector listens to:
    export OTEL_EXPORTER_OTLP_ENDPOINT=http://0.0.0.0:4318
    Note: 0.0.0.0 is the OpenTelemetry collector HOST and 4318 is the OpenTelemetry Collector PORT. For more information about available exporters, see Ruby Exporter.
  4. Install the newly added Gems to Gemfile.
    bundle install
  5. Start the Ruby application.