class XRay::DefaultContext

The default context storage management used by the X-Ray recorder. It uses thread local to store segments and subsegments.

Constants

CONTEXT_MISSING_KEY
DEFAULT_STRATEGY
LOCAL_KEY
SUPPORTED_STRATEGY

Attributes

context_missing[R]

Public Class Methods

new() click to toggle source
# File lib/aws-xray-sdk/context/default_context.rb, line 20
def initialize
  strategy = ENV[CONTEXT_MISSING_KEY] || DEFAULT_STRATEGY
  @context_missing = sanitize_strategy(strategy)
end

Public Instance Methods

clear!() click to toggle source

Clear the current thread local storage on X-Ray related entities.

# File lib/aws-xray-sdk/context/default_context.rb, line 40
def clear!
  Thread.current[LOCAL_KEY] = nil
end
context_missing=(v) click to toggle source
# File lib/aws-xray-sdk/context/default_context.rb, line 65
def context_missing=(v)
  strategy = ENV[CONTEXT_MISSING_KEY] || v
  @context_missing = sanitize_strategy(strategy)
end
current_entity() click to toggle source

@return [Entity] The current active entity(could be segment or subsegment).

# File lib/aws-xray-sdk/context/default_context.rb, line 31
def current_entity
  if entity = Thread.current[LOCAL_KEY]
    entity
  else
    handle_context_missing
  end
end
handle_context_missing() click to toggle source

When the current entity needs to be accessed but there is none, it handles the missing context based on the configuration. On `RUNTIME_ERROR` it raises `ContextMissingError`. On 'LOG_ERROR' it logs an error message and return `nil`.

# File lib/aws-xray-sdk/context/default_context.rb, line 55
def handle_context_missing
  case context_missing
  when 'RUNTIME_ERROR'
    raise ContextMissingError
  when 'LOG_ERROR'
    logger.error %(cannot find the current context.)
  end
  nil
end
inject_context(entity, target_ctx: nil) click to toggle source

@param [Entity] entity The entity to inject. @param [Thread] target_ctx Put the provided entity to the new thread.

# File lib/aws-xray-sdk/context/default_context.rb, line 46
def inject_context(entity, target_ctx: nil)
  target_ctx ||= Thread.current
  target_ctx[LOCAL_KEY] = entity if entity
end
store_entity(entity:) click to toggle source

@param [Entity] entity The entity to be stored in the context.

# File lib/aws-xray-sdk/context/default_context.rb, line 26
def store_entity(entity:)
  Thread.current[LOCAL_KEY] = entity
end

Private Instance Methods

sanitize_strategy(v) click to toggle source
# File lib/aws-xray-sdk/context/default_context.rb, line 72
def sanitize_strategy(v)
  if SUPPORTED_STRATEGY.include?(v)
    v
  else
    logger.warn %(context missing #{v} is not supported, switch to default #{DEFAULT_STRATEGY}.)
    DEFAULT_STRATEGY
  end
end