class ElasticAPM::TraceContext::Traceparent

@api private

Constants

ID_LENGTH
NON_HEX_REGEX
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, parent_id: nil, id: nil, recorded: true ) click to toggle source
# File lib/elastic_apm/trace_context/traceparent.rb, line 30
def initialize(
  version: VERSION,
  trace_id: nil,
  parent_id: nil,
  id: nil,
  recorded: true
)
  @version = version
  @trace_id = trace_id || hex(TRACE_ID_LENGTH)
  @parent_id = parent_id
  @id = id || hex(ID_LENGTH)
  @recorded = recorded
end
parse(header) click to toggle source
# File lib/elastic_apm/trace_context/traceparent.rb, line 48
def self.parse(header)
  raise_invalid(header) unless header.length == 55
  raise_invalid(header) 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_invalid(header) if NON_HEX_REGEX.match?(t.trace_id)
    raise_invalid(header) if NON_HEX_REGEX.match?(t.parent_id)
  end
end

Private Class Methods

raise_invalid(header) click to toggle source
# File lib/elastic_apm/trace_context/traceparent.rb, line 66
def raise_invalid(header)
  raise InvalidTraceparentHeader,
    "Couldn't parse invalid traceparent header: #{header.inspect}"
end

Public Instance Methods

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

  self.recorded = flags[7] == '1'
end
hex_flags() click to toggle source
# File lib/elastic_apm/trace_context/traceparent.rb, line 82
def hex_flags
  format('%02x', flags.to_i(2))
end
to_header() click to toggle source
# File lib/elastic_apm/trace_context/traceparent.rb, line 97
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/traceparent.rb, line 103
def hex(len)
  SecureRandom.hex(len)
end