aws_xray_sdk.core.context

Module Contents

log
MISSING_SEGMENT_MSG = cannot find the current segment/subsegment, please make sure you have a segment open
SUPPORTED_CONTEXT_MISSING = ['RUNTIME_ERROR', 'LOG_ERROR']
CXT_MISSING_STRATEGY_KEY = AWS_XRAY_CONTEXT_MISSING
class Context(context_missing='RUNTIME_ERROR')

Bases:object

The context storage class to store trace entities(segments/subsegments). The default implementation uses threadlocal to store these entities. It also provides interfaces to manually inject trace entities which will replace the current stored entities and to clean up the storage.

For any data access or data mutation, if there is no active segment present if will use user-defined behavior to handle such case. By default it throws an runtime error.

This data structure is thread-safe.

context_missing
put_segment(self, segment)

Store the segment created by xray_recorder to the context. It overrides the current segment if there is already one.

end_segment(self, end_time=None)

End the current active segment.

Parameters:end_time (int) – epoch in seconds. If not specified the current system time will be used.
put_subsegment(self, subsegment)

Store the subsegment created by xray_recorder to the context. If you put a new subsegment while there is already an open subsegment, the new subsegment becomes the child of the existing subsegment.

end_subsegment(self, end_time=None)

End the current active segment. Return False if there is no subsegment to end.

Parameters:end_time (int) – epoch in seconds. If not specified the current system time will be used.
get_trace_entity(self)

Return the current trace entity(segment/subsegment). If there is none, it behaves based on pre-defined context_missing strategy.

set_trace_entity(self, trace_entity)

Store the input trace_entity to local context. It will overwrite all existing ones if there is any.

clear_trace_entities(self)

clear all trace_entities stored in the local context. In case of using threadlocal to store trace entites, it will clean up all trace entities created by the current thread.

handle_context_missing(self)

Called whenever there is no trace entity to access or mutate.

_is_subsegment(self, entity)