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



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

def current
  stack.last
end

.stackObject



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

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.



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

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