class Datadog::DefaultContextProvider

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

Public Class Methods

new() click to toggle source

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

# File lib/ddtrace/context_provider.rb, line 8
def initialize
  @context = ThreadLocalContext.new
end

Public Instance Methods

context(key = nil) click to toggle source

Return the local context.

# 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) click to toggle source

Sets the current context.

# File lib/ddtrace/context_provider.rb, line 13
def context=(ctx)
  @context.local = ctx
end