Class: Datadog::Tracing::DefaultContextProvider

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

Overview

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

Instance Method Summary collapse

Constructor Details

#initializeDefaultContextProvider

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



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

def initialize
  @context = FiberLocalContext.new
end

Instance Method Details

#context(key = nil) ⇒ Object

Return the local context.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/datadog/tracing/context_provider.rb', line 23

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
    # TODO: Only assign to `self.context` when working on the current thread (`key == nil`)
    current_context = self.context = current_context.fork_clone
  end

  current_context
end

#context=(ctx) ⇒ Object

Sets the current context.



18
19
20
# File 'lib/datadog/tracing/context_provider.rb', line 18

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