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



108
109
110
# File 'lib/bitfab/span_context.rb', line 108

def current
  stack.last
end

.stackObject



104
105
106
# File 'lib/bitfab/span_context.rb', line 104

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.



114
115
116
117
118
119
120
# File 'lib/bitfab/span_context.rb', line 114

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