Class: Datadog::DefaultContextProvider

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

Overview

DefaultContextProvider is a default context provider that retrieves all contexts from the current thread-local storage. It is suitable for synchronous programming.

Instance Method Summary collapse

Constructor Details

#initializeDefaultContextProvider

Initializes the default context provider with a thread-bound context.



8
9
10
# File 'lib/ddtrace/context_provider.rb', line 8

def initialize
  @context = ThreadLocalContext.new
end

Instance Method Details

#context(key = nil) ⇒ Object

Return the local context.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ddtrace/context_provider.rb', line 18

def context(key = nil)
  current_context = key.nil? ? @context.local : @context.local(key)

  # Rebuild/reset context after a fork
  #
  # We don't want forked processes to copy and retransmit spans
  # that were generated from the parent process. Reset it such
  # that it acts like a distributed trace.
  current_context.after_fork! do
    current_context = self.context = current_context.fork_clone
  end

  current_context
end

#context=(ctx) ⇒ Object

Sets the current context.



13
14
15
# File 'lib/ddtrace/context_provider.rb', line 13

def context=(ctx)
  @context.local = ctx
end