Class: XRay::DefaultContext

Inherits:
Object
  • Object
show all
Includes:
Context, Logging
Defined in:
lib/aws-xray-sdk/context/default_context.rb

Overview

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

Direct Known Subclasses

LambdaContext

Constant Summary collapse

LOCAL_KEY =
'_aws_xray_entity'.freeze
CONTEXT_MISSING_KEY =
'AWS_XRAY_CONTEXT_MISSING'.freeze
SUPPORTED_STRATEGY =
%w[RUNTIME_ERROR LOG_ERROR IGNORE_ERROR].freeze
DEFAULT_STRATEGY =
SUPPORTED_STRATEGY[0]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Logging

#logger, logger, logger=

Constructor Details

#initializeDefaultContext

Returns a new instance of DefaultContext.



20
21
22
23
# 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

Instance Attribute Details

#context_missingObject

Returns the value of attribute context_missing.



18
19
20
# File 'lib/aws-xray-sdk/context/default_context.rb', line 18

def context_missing
  @context_missing
end

Instance Method Details

#clear!Object

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



40
41
42
# File 'lib/aws-xray-sdk/context/default_context.rb', line 40

def clear!
  Thread.current[LOCAL_KEY] = nil
end

#current_entityEntity

Returns The current active entity(could be segment or subsegment).

Returns:

  • (Entity)

    The current active entity(could be segment or subsegment).



31
32
33
34
35
36
37
# 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_missingObject

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`.



55
56
57
58
59
60
61
62
63
# 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 %(can not find the current context.)
  end
  nil
end

#inject_context(entity, target_ctx: nil) ⇒ Object

Parameters:

  • entity (Entity)

    The entity to inject.

  • target_ctx (Thread) (defaults to: nil)

    Put the provided entity to the new thread.



46
47
48
49
# 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:) ⇒ Object

Parameters:

  • entity (Entity)

    The entity to be stored in the context.



26
27
28
# File 'lib/aws-xray-sdk/context/default_context.rb', line 26

def store_entity(entity:)
  Thread.current[LOCAL_KEY] = entity
end