class Traces::Context
A generic representation of the current tracing context.
def self.local(flags = 0, **options)
Create a local trace context which is likley to be globally unique.
def self.local(flags = 0, **options) self.new(SecureRandom.hex(16), SecureRandom.hex(8), flags, **options) end
def self.nested(parent, flags = 0)
Nest a local trace context in an optional parent context.
def self.nested(parent, flags = 0) if parent parent.nested(flags) else self.local(flags) end end
def self.parse(parent, state = nil, **options)
@parameter parent [String] The parent trace context.
Parse a string representation of a distributed trace.
def self.parse(parent, state = nil, **options) version, trace_id, parent_id, flags = parent.split('-') if version == '00' flags = Integer(flags, 16) if state.is_a?(String) state = state.split(',') end if state state = state.map{|item| item.split('=')}.to_h end self.new(trace_id, parent_id, flags, state, **options) end end
def as_json
def as_json { trace_id: @trace_id, parent_id: @parent_id, flags: @flags, state: @state, remote: @remote } end
def initialize(trace_id, parent_id, flags, state = nil, remote: false)
@parameter state [Hash] Additional vendor-specific trace identification information.
@parameter flags [Integer] An 8-bit field that controls tracing flags such as sampling, trace level, etc.
@parameter parent_id [String] The ID of this operation as known by the caller (sometimes referred to as the span ID).
@parameter trace_id [String] The ID of the whole trace forest.
Initialize the trace context.
def initialize(trace_id, parent_id, flags, state = nil, remote: false) @trace_id = trace_id @parent_id = parent_id @flags = flags @state = state @remote = remote end
def nested(flags = @flags)
def nested(flags = @flags) Context.new(@trace_id, SecureRandom.hex(8), flags, @state, remote: @remote) end
def remote?
def remote? @remote end
def sampled?
def sampled? (@flags & SAMPLED) != 0 end
def to_json(...)
def to_json(...) as_json.to_json(...) end
def to_s
def to_s "00-#{@trace_id}-#{@parent_id}-#{@flags.to_s(16)}" end