Class: Datadog::Tracing::FiberLocalContext

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/tracing/context_provider.rb

Overview

FiberLocalContext can be used as a tracer global reference to create a different Context for each fiber. This allows for the tracer to create a serial execution graph regardless of any concurrent execution: each concurrent execution path creates a new trace graph.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(otel_thread_context: nil) ⇒ FiberLocalContext

To support multiple tracers simultaneously, each Datadog::Tracing::FiberLocalContext instance has its own fiber-local variable.



62
63
64
65
66
67
# File 'lib/datadog/tracing/context_provider.rb', line 62

def initialize(otel_thread_context: nil)
  @otel_thread_context = otel_thread_context
  @key = :"datadog_context_#{FiberLocalContext.next_instance_id}"

  self.local = Context.new(otel_thread_context: otel_thread_context)
end

Class Method Details

.next_instance_id ⇒ Object



89
90
91
# File 'lib/datadog/tracing/context_provider.rb', line 89

def self.next_instance_id
  UNIQUE_INSTANCE_MUTEX.synchronize { UNIQUE_INSTANCE_GENERATOR.next }
end

Instance Method Details

#local(storage = Thread.current) ⇒ Object

Return the fiber-local context.



75
76
77
# File 'lib/datadog/tracing/context_provider.rb', line 75

def local(storage = Thread.current)
  storage[@key] ||= Context.new(otel_thread_context: @otel_thread_context)
end

#local=(ctx) ⇒ Object

Override the fiber-local context with a new context.



70
71
72
# File 'lib/datadog/tracing/context_provider.rb', line 70

def local=(ctx)
  Thread.current[@key] = ctx
end