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



118
119
120
# File 'lib/bitfab/span_context.rb', line 118

def current
  stack.last
end

.stackObject



114
115
116
# File 'lib/bitfab/span_context.rb', line 114

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.



124
125
126
127
128
129
130
# File 'lib/bitfab/span_context.rb', line 124

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