Class: XRay::DefaultContext
- Inherits:
-
Object
- Object
- XRay::DefaultContext
- 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.
Constant Summary collapse
- LOCAL_KEY =
'_aws_xray_entity'.freeze
- CONTEXT_MISSING_KEY =
'AWS_XRAY_CONTEXT_MISSING'.freeze
- SUPPORTED_STRATEGY =
%w[RUNTIME_ERROR LOG_ERROR].freeze
- DEFAULT_STRATEGY =
SUPPORTED_STRATEGY[0]
Instance Attribute Summary collapse
-
#context_missing ⇒ Object
Returns the value of attribute context_missing.
Instance Method Summary collapse
-
#clear! ⇒ Object
Clear the current thread local storage on X-Ray related entities.
-
#current_entity ⇒ Entity
The current active entity(could be segment or subsegment).
-
#handle_context_missing ⇒ Object
When the current entity needs to be accessed but there is none, it handles the missing context based on the configuration.
-
#initialize ⇒ DefaultContext
constructor
A new instance of DefaultContext.
- #inject_context(entity, target_ctx: nil) ⇒ Object
- #store_entity(entity:) ⇒ Object
Methods included from Logging
Constructor Details
#initialize ⇒ DefaultContext
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_missing ⇒ Object
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_entity ⇒ Entity
Returns 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_missing ⇒ Object
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
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
26 27 28 |
# File 'lib/aws-xray-sdk/context/default_context.rb', line 26 def store_entity(entity:) Thread.current[LOCAL_KEY] = entity end |