class OpenCensus::Trace::Formatters::TraceContext

This formatter serializes and deserializes span context according to the TraceContext specification. See [documentation](github.com/TraceContext/tracecontext-spec/blob/master/trace_context/HTTP_HEADER_FORMAT.md).

Constants

HEADER_NAME

The outgoing header used for the TraceContext header specification.

@private

HEADER_V0_PATTERN

Internal regex used to parse fields in version 0

@private

RACK_HEADER_NAME

The rack environment header used for the TraceContext header specification

@private

VERSION_PATTERN

Internal regex used to identify the TraceContext version

@private

Public Instance Methods

deserialize(header) click to toggle source

Deserialize a trace context header into a TraceContext object.

@param [String] header @return [TraceContextData, nil]

# File lib/opencensus/trace/formatters/trace_context.rb, line 80
def deserialize header
  match = VERSION_PATTERN.match(header)
  if match
    version = match[1].to_i(16)
    version_format = match[2]
    case version
    when 0
      parse_trace_context_header_version_0 version_format
    else
      nil
    end
  else
    nil
  end
end
header_name() click to toggle source

Returns the name of the header used for context propagation.

@return [String]

# File lib/opencensus/trace/formatters/trace_context.rb, line 60
def header_name
  HEADER_NAME
end
rack_header_name() click to toggle source

Returns the name of the rack_environment header to use when parsing context from an incoming request.

@return [String]

# File lib/opencensus/trace/formatters/trace_context.rb, line 70
def rack_header_name
  RACK_HEADER_NAME
end
serialize(trace_context) click to toggle source

Serialize a TraceContextData object.

@param [TraceContextData] trace_context @return [String]

# File lib/opencensus/trace/formatters/trace_context.rb, line 102
def serialize trace_context
  format(
    "%02<version>d-%<trace_id>s-%<span_id>s-%02<trace_options>d",
    version: 0, # version 0,
    trace_id: trace_context.trace_id,
    span_id: trace_context.span_id,
    trace_options: trace_context.trace_options
  )
end

Private Instance Methods

parse_trace_context_header_version_0(str) click to toggle source
# File lib/opencensus/trace/formatters/trace_context.rb, line 114
def parse_trace_context_header_version_0 str
  match = HEADER_V0_PATTERN.match(str)
  if match
    TraceContextData.new match[1].downcase,
                         match[2].downcase,
                         match[4].to_i(16)
  end
end