Module: Profiler::Instrumentation::ThreadContextPropagation

Defined in:
lib/profiler/instrumentation/thread_context_propagation.rb

Constant Summary collapse

PROPAGATED_KEYS =
%i[
  profiler_http_collector
  profiler_flamegraph_collector
].freeze

Instance Method Summary collapse

Instance Method Details

#initialize(*args, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/profiler/instrumentation/thread_context_propagation.rb', line 11

def initialize(*args, &block)
  parent_context = PROPAGATED_KEYS.filter_map do |key|
    val = Thread.current[key]
    [key, val] unless val.nil?
  end.to_h

  if parent_context.empty?
    super
  else
    super(*args) do
      parent_context.each { |k, v| Thread.current[k] = v }
      begin
        block&.call
      ensure
        PROPAGATED_KEYS.each { |k| Thread.current[k] = nil }
      end
    end
  end
end