Module: Bitfab::SpanContext

Defined in:
lib/bitfab/span_context.rb

Overview

Thread-local span stack for tracking nested spans. Each entry is a Hash with :trace_id and :span_id keys.

Constant Summary collapse

STACK_KEY =
:__bitfab_span_stack

Class Method Summary collapse

Class Method Details

.currentObject



124
125
126
# File 'lib/bitfab/span_context.rb', line 124

def current
  stack.last
end

.stackObject



120
121
122
# File 'lib/bitfab/span_context.rb', line 120

def stack
  Thread.current[STACK_KEY] ||= []
end

.with_span(trace_id:, span_id:) ⇒ Object

Execute a block with a new span pushed onto the stack. The span is automatically popped when the block completes.



130
131
132
133
134
135
136
# File 'lib/bitfab/span_context.rb', line 130

def with_span(trace_id:, span_id:)
  entry = {trace_id:, span_id:}
  stack.push(entry)
  yield
ensure
  stack.pop
end