class ElasticAPM::TraceContext

@api private

Constants

HEX_REGEX
ID_LENGTH
TRACE_ID_LENGTH
VERSION

Attributes

id[RW]
parent_id[RW]
recorded[RW]
recorded?[RW]
trace_id[RW]
version[RW]

Public Class Methods

new( version: VERSION, trace_id: nil, span_id: nil, id: nil, recorded: true ) click to toggle source
# File lib/elastic_apm/trace_context.rb, line 15
def initialize(
  version: VERSION,
  trace_id: nil,
  span_id: nil,
  id: nil,
  recorded: true
)
  @version = version
  @trace_id = trace_id || hex(TRACE_ID_LENGTH)
  # TODO: rename to parent_id with next major version bump
  @parent_id = span_id
  @id = id || hex(ID_LENGTH)
  @recorded = recorded
end
parse(header) click to toggle source

rubocop:disable Metrics/AbcSize

# File lib/elastic_apm/trace_context.rb, line 35
def self.parse(header)
  raise InvalidTraceparentHeader unless header.length == 55
  raise InvalidTraceparentHeader unless header[0..1] == VERSION

  new.tap do |t|
    t.version, t.trace_id, t.parent_id, t.flags =
      header.split('-').tap do |values|
        values[-1] = Util.hex_to_bits(values[-1])
      end

    raise InvalidTraceparentHeader if HEX_REGEX =~ t.trace_id
    raise InvalidTraceparentHeader if HEX_REGEX =~ t.parent_id
  end
end

Public Instance Methods

child() click to toggle source
# File lib/elastic_apm/trace_context.rb, line 69
def child
  dup.tap do |tc|
    tc.parent_id = tc.id
    tc.id = hex(ID_LENGTH)
  end
end
ensure_parent_id() click to toggle source
# File lib/elastic_apm/trace_context.rb, line 65
def ensure_parent_id
  @parent_id ||= hex(ID_LENGTH)
end
flags() click to toggle source
# File lib/elastic_apm/trace_context.rb, line 57
def flags
  format('0000000%d', recorded? ? 1 : 0)
end
flags=(flags) click to toggle source

rubocop:enable Metrics/AbcSize

# File lib/elastic_apm/trace_context.rb, line 51
def flags=(flags)
  @flags = flags

  self.recorded = flags[7] == '1'
end
hex_flags() click to toggle source
# File lib/elastic_apm/trace_context.rb, line 61
def hex_flags
  format('%02x', flags.to_i(2))
end
span_id() click to toggle source

@deprecated Use parent_id instead

# File lib/elastic_apm/trace_context.rb, line 81
def span_id
  @parent_id
end
span_id=(span_id) click to toggle source

@deprecated Use parent_id instead

# File lib/elastic_apm/trace_context.rb, line 86
def span_id=(span_id)
  @parent_id = span_id
end
to_header() click to toggle source
# File lib/elastic_apm/trace_context.rb, line 76
def to_header
  format('%s-%s-%s-%s', version, trace_id, id, hex_flags)
end

Private Instance Methods

hex(len) click to toggle source
# File lib/elastic_apm/trace_context.rb, line 95
def hex(len)
  SecureRandom.hex(len)
end