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.



34
35
36
37
38
# File 'lib/ddtrace/context_provider.rb', line 34

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

  self.local = Datadog::Context.new
end

Instance Method Details

#localObject

Return the thread-local context.



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

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

#local=(ctx) ⇒ Object

Override the thread-local context with a new context.



41
42
43
# File 'lib/ddtrace/context_provider.rb', line 41

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