For the complete documentation index, see llms.txt. Markdown versions are available by appending .md to documentation URLs.

How to add manual instrumentation in Ruby

SigNoz Cloud - This page applies to SigNoz Cloud editions.
Self-Host - This page applies to self-hosted SigNoz editions.

Manual instrumentation gives you fine-grained control when automatic instrumentation alone cannot express important business operations. Use it to capture steps that matter for debugging, to attach business-specific attributes, or to guarantee that failures surface with the right context in SigNoz.

Prerequisites

Step 1. Create manual spans

Use the SDK tracer to wrap important work inside custom spans:

app/services/order_service.rb
class OrderService
  def process_order(order_id)
    tracer = OpenTelemetry.tracer_provider.tracer('order-service')
 
    tracer.in_span('process-order') do |span|
      span.set_attribute('order.id', order_id)
      span.set_attribute('order.status', 'processing')
 
      # Your order processing logic here
      validate_inventory(order_id)
      charge_payment(order_id)
    end
  end
end

Tips:

  • Reuse tracer instances instead of creating a new one for each request.
  • Start spans with descriptive names that match business steps (checkout, fetch-user, etc.).
  • The in_span block automatically ends the span when the block completes.

Step 2. Propagate context

Ruby keeps the active span in a context store the runtime manages, so in_span links child spans for you inside a single call chain. Two things break that chain: a new thread, and another service. See Context Propagation for the concepts behind this step.

Inside your process

in_span activates the span for the duration of its block. start_span creates a span without activating it, so nothing nested inside becomes its child:

in_process.rb
# validate_inventory's spans become children of process-order
tracer.in_span('process-order') do |span|
  validate_inventory(order_id)
end
 
# validate_inventory's spans do NOT become children of process-order
span = tracer.start_span('process-order')
validate_inventory(order_id)
span.finish

A new thread starts with an empty context. Capture the current context before you spawn and activate it inside the thread:

threads.rb
tracer.in_span('process-batch') do
  context = OpenTelemetry::Context.current
 
  items.map do |item|
    Thread.new do
      OpenTelemetry::Context.with_current(context) do
        # Spans started here attach to process-batch
        handle_item(item)
      end
    end
  end.each(&:join)
end

Across a service boundary

The Rails, Sinatra, net/http, and Sidekiq instrumentation gems inject and extract for you. Write the calls yourself for any hop they do not cover.

propagate.rb
# Outgoing: inject writes traceparent into the carrier
def charge_card(payload)
  tracer.in_span('charge-card') do
    carrier = {}
    OpenTelemetry.propagation.inject(carrier)
 
    # carrier is now { 'traceparent' => '00-<trace-id>-<span-id>-01' }
    HTTP.headers(carrier).post('https://payments.internal/charge', json: payload)
  end
end
 
# Incoming: extract returns a context to activate before the entry span
def handle_charge(request)
  context = OpenTelemetry.propagation.extract(request.headers)
 
  OpenTelemetry::Context.with_current(context) do
    tracer.in_span('handle-charge', kind: :server) do |span|
      # handle-charge is now a child of charge-card in the caller's trace
      capture_payment(request)
    end
  end
end

For a queue, put the carrier on the message and read it back in the consumer:

queue.rb
# Publisher
carrier = {}
OpenTelemetry.propagation.inject(carrier)
queue.publish(payload, headers: carrier)
 
# Consumer
context = OpenTelemetry.propagation.extract(message.headers)
OpenTelemetry::Context.with_current(context) do
  tracer.in_span('process-message', kind: :consumer) { handle(message) }
end

Step 3. Add attributes and events

Attributes show up as key-value pairs in SigNoz so you can filter and aggregate spans. Events capture notable moments inside a span.

app/services/payment_service.rb
class PaymentService
  def handle_payment(amount, currency)
    span = OpenTelemetry::Trace.current_span
 
    span.set_attribute('payment.amount', amount)
    span.set_attribute('payment.currency', currency)
    span.set_attribute('payment.method', 'credit_card')
 
    # Process the payment
    result = process_charge(amount, currency)
 
    span.add_event('payment.processed', attributes: {
      'status' => 'success',
      'transaction.id' => result.transaction_id
    })
  end
end
  • Keep attribute keys consistent (use semantic conventions when possible).
  • Use events to mark retries, cache hits/misses, queue waits, and similar milestones.

Step 4. Record errors

Flag failures on the span so they are easy to query in SigNoz.

app/services/risky_service.rb
class RiskyService
  def risky_operation
    span = OpenTelemetry::Trace.current_span
 
    begin
      do_something_risky
      span.status = OpenTelemetry::Trace::Status.ok
    rescue StandardError => e
      span.record_exception(e)
      span.status = OpenTelemetry::Trace::Status.error(e.message)
      raise
    end
  end
end
  • record_exception attaches stack trace and message details.
  • Setting status to error surfaces the span in SigNoz error views and alerts.
  • Continue propagating the error upstream so calling code can respond.

Validate

  1. Trigger the code paths that emit manual spans.
  2. In SigNoz Traces, filter by service.name or your span name.
  3. Open a trace and verify attributes, events, and error status.
  4. Check the Errors view to confirm failures show up with recorded exceptions.

Troubleshooting

Still not seeing data in SigNoz? Work through Debug missing traces, logs, and metrics, which covers SDK diagnostics, Collector connectivity, and the common ingestion errors for all three signals.

Why don't I see my custom spans in SigNoz?

  • Make sure that the OpenTelemetry initializer runs before your application handlers.
  • Check sampler configuration. Using a low sampling rate in development may drop most manual spans.
  • Verify traffic hits the functions where you inserted spans.

Why are child spans missing even though I create them?

  • Ensure you use in_span blocks or properly manage span context for nested spans.
  • When calling external services or databases, the instrumentation must propagate context.

Why does the downstream service start its own trace?

  • Make sure that the caller runs OpenTelemetry.propagation.inject and that the outgoing request carries a traceparent header.
  • Make sure that the receiver wraps the entry span in OpenTelemetry::Context.with_current(context) using the context that extract returned.
  • Check the header case. The default getter is case-sensitive, so a carrier holding Traceparent rather than traceparent extracts nothing and reports no error.
  • Make sure that both services register the same propagator format. See Context Propagation.

Why don't attributes or events appear on the span?

  • Attribute values must be strings, integers, floats, booleans, or arrays of these types.
  • Call set_attribute or add_event before the span ends. Post-end mutations are ignored.
  • Avoid reusing finished spans—always create a new span for each invocation.

Next steps

Is this page helpful

Last updated—September 03, 2026