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
- Finish the core Ruby setup in the Ruby OpenTelemetry instrumentation guide so exporters, tracer provider, and SDK are configured.
- The
opentelemetry-sdkgem installed in your application. - Tested with Ruby 3.3.0 and OpenTelemetry Ruby SDK v1.10.0.
Step 1. Create manual spans
Use the SDK tracer to wrap important work inside custom spans:
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
endTips:
- 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_spanblock 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:
# 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.finishA new thread starts with an empty context. Capture the current context before you spawn and activate it inside the thread:
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)
endAcross 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.
# 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
endFor a queue, put the carrier on the message and read it back in the consumer:
# 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) }
endStep 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.
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.
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
endrecord_exceptionattaches stack trace and message details.- Setting status to
errorsurfaces the span in SigNoz error views and alerts. - Continue propagating the error upstream so calling code can respond.
Validate
- Trigger the code paths that emit manual spans.
- In SigNoz Traces, filter by
service.nameor your span name. - Open a trace and verify attributes, events, and error status.
- 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_spanblocks 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.injectand that the outgoing request carries atraceparentheader. - Make sure that the receiver wraps the entry span in
OpenTelemetry::Context.with_current(context)using the context thatextractreturned. - Check the header case. The default getter is case-sensitive, so a carrier holding
Traceparentrather thantraceparentextracts 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_attributeoradd_eventbefore the span ends. Post-end mutations are ignored. - Avoid reusing finished spans—always create a new span for each invocation.
Next steps
- Set up alerts for your Ruby application
- Explore the OpenTelemetry Ruby instrumentation registry for additional library instrumentations
- Read Context Propagation for the wire format and the failure modes shared across languages