class OpenTelemetry::Trace::Propagation::TraceContext::TraceParent

A TraceParent is an implementation of the W3C trace context specification www.w3.org/TR/trace-context/ {Trace::SpanContext}

Constants

INVALID_SPAN_ID
INVALID_TRACE_ID
InvalidFormatError
InvalidSpanIDError
InvalidTraceIDError
InvalidVersionError
MAX_VERSION
REGEXP
SUPPORTED_VERSION
TRACE_PARENT_HEADER

Attributes

flags[R]
span_id[R]
trace_id[R]
version[R]

Public Class Methods

from_span_context(ctx) click to toggle source

Creates a new {TraceParent} from a supplied {Trace::SpanContext} @param [SpanContext] ctx The span context @return [TraceParent] a trace parent

# File lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb, line 36
def from_span_context(ctx)
  new(trace_id: ctx.trace_id, span_id: ctx.span_id, flags: ctx.trace_flags)
end
from_string(string) click to toggle source

Deserializes the {TraceParent} from the string representation @param [String] string The serialized trace parent @return [TraceParent] a trace_parent @raise [InvalidFormatError] on an invalid format @raise [InvalidVerionError] on an invalid version @raise [InvalidTraceIDError] on an invalid trace_id @raise [InvalidSpanIDError] on an invalid span_id

# File lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb, line 47
def from_string(string)
  matches = match_input(string)

  version = parse_version(matches[:version])
  raise InvalidFormatError if version > SUPPORTED_VERSION && string.length < 55

  trace_id = parse_trace_id(matches[:trace_id])
  span_id = parse_span_id(matches[:span_id])
  flags = parse_flags(matches[:flags])

  new(trace_id: trace_id, span_id: span_id, flags: flags)
end
new(trace_id: nil, span_id: nil, version: SUPPORTED_VERSION, flags: Trace::TraceFlags::DEFAULT) click to toggle source
# File lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb, line 120
def initialize(trace_id: nil, span_id: nil, version: SUPPORTED_VERSION, flags: Trace::TraceFlags::DEFAULT)
  @trace_id = trace_id
  @span_id = span_id
  @version = version
  @flags = flags
end

Private Class Methods

match_input(string) click to toggle source
# File lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb, line 62
def match_input(string)
  matches = REGEXP.match(string)
  raise InvalidFormatError, 'regexp match failed' if !matches || matches.length < 6

  matches
end
parse_flags(string) click to toggle source
# File lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb, line 91
def parse_flags(string)
  OpenTelemetry::Trace::TraceFlags.from_byte(string.to_i(16))
end
parse_span_id(string) click to toggle source
# File lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb, line 84
def parse_span_id(string)
  raise InvalidSpanIDError, string if string == INVALID_SPAN_ID

  string.downcase!
  Array(string).pack('H*')
end
parse_trace_id(string) click to toggle source
# File lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb, line 77
def parse_trace_id(string)
  raise InvalidTraceIDError, string if string == INVALID_TRACE_ID

  string.downcase!
  Array(string).pack('H*')
end
parse_version(string) click to toggle source
# File lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb, line 69
def parse_version(string)
  v = string.to_i(16)
  raise InvalidFormatError, string unless v
  raise InvalidVersionError, v if v > MAX_VERSION

  v
end

Public Instance Methods

sampled?() click to toggle source

Returns the sampling choice from the trace_flags @return [Boolean] the sampling choice

# File lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb, line 102
def sampled?
  flags.sampled?
end
to_s() click to toggle source

converts this object into a string according to the w3c spec @return [String] the serialized trace_parent

# File lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb, line 108
def to_s
  "00-#{trace_id.unpack1('H*')}-#{span_id.unpack1('H*')}-#{flag_string}"
end

Private Instance Methods

flag_string() click to toggle source
# File lib/opentelemetry/trace/propagation/trace_context/trace_parent.rb, line 114
def flag_string
  # the w3c standard only dictates the one flag for this version
  # therefore we can only output the one flag.
  flags.sampled? ? '01' : '00'
end