Module: EventHub::CorrelationId

Defined in:
lib/eventhub/correlation_id.rb

Overview

Manages correlation_id for distributed tracing Storage mechanism can be swapped if needed (e.g., Thread.current -> Fiber storage)

Class Method Summary collapse

Class Method Details

.clearObject



15
16
17
# File 'lib/eventhub/correlation_id.rb', line 15

def clear
  Thread.current[:eventhub_correlation_id] = nil
end

.currentObject



7
8
9
# File 'lib/eventhub/correlation_id.rb', line 7

def current
  Thread.current[:eventhub_correlation_id]
end

.current=(value) ⇒ Object



11
12
13
# File 'lib/eventhub/correlation_id.rb', line 11

def current=(value)
  Thread.current[:eventhub_correlation_id] = value
end

.with(correlation_id) ⇒ Object

Execute block with correlation_id set, ensures cleanup.

Always saves the prior value and restores it on exit, even when called with nil/empty - otherwise any value written to ‘current` inside the block (e.g. handle_payload’s fallback to the message body’s execution_id) would leak onto the consumer thread and be picked up by the next message’s processing.



26
27
28
29
30
31
32
33
34
# File 'lib/eventhub/correlation_id.rb', line 26

def with(correlation_id)
  old_value = current
  begin
    self.current = correlation_id unless correlation_id.nil? || correlation_id.to_s.empty?
    yield
  ensure
    self.current = old_value
  end
end