Class: Datadog::ThreadLocalContext

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

Overview

ThreadLocalContext can be used as a tracer global reference to create a different \Context for each thread. In synchronous tracer, this is required to prevent multiple threads sharing the same \Context in different executions.

Instance Method Summary collapse

Constructor Details

#initializeThreadLocalContext

ThreadLocalContext can be used as a tracer global reference to create a different \Context for each thread. In synchronous tracer, this is required to prevent multiple threads sharing the same \Context in different executions.

To support multiple tracers simultaneously, each \ThreadLocalContext instance has its own thread-local variable.



46
47
48
49
50
# File 'lib/ddtrace/context_provider.rb', line 46

def initialize
  @key = "datadog_context_#{object_id}".to_sym

  self.local = Datadog::Context.new
end

Instance Method Details

#local(thread = Thread.current) ⇒ Object

Return the thread-local context.



58
59
60
# File 'lib/ddtrace/context_provider.rb', line 58

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

#local=(ctx) ⇒ Object

Override the thread-local context with a new context.



53
54
55
# File 'lib/ddtrace/context_provider.rb', line 53

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