Class: OpenCensus::Trace::Formatters::TraceContext

Inherits:
Object
  • Object
show all
Defined in:
lib/opencensus/trace/formatters/trace_context.rb

Overview

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).

Constant Summary collapse

VERSION_PATTERN =

Internal regex used to identify the TraceContext version

/^([0-9a-fA-F]{2})-(.+)$/
HEADER_V0_PATTERN =

Internal regex used to parse fields in version 0

/^([0-9a-fA-F]{32})-([0-9a-fA-F]{16})(-([0-9a-fA-F]{2}))?$/
HEADER_NAME =

The outgoing header used for the TraceContext header specification.

"Trace-Context".freeze
RACK_HEADER_NAME =

The rack environment header used for the TraceContext header specification

"HTTP_TRACE_CONTEXT".freeze

Instance Method Summary collapse

Instance Method Details

#deserialize(header) ⇒ TraceContextData?

Deserialize a trace context header into a TraceContext object.

Parameters:

  • header (String)

Returns:



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/opencensus/trace/formatters/trace_context.rb', line 79

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_nameString

Returns the name of the header used for context propagation.

Returns:

  • (String)


59
60
61
# File 'lib/opencensus/trace/formatters/trace_context.rb', line 59

def header_name
  HEADER_NAME
end

#rack_header_nameString

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

Returns:

  • (String)


69
70
71
# File 'lib/opencensus/trace/formatters/trace_context.rb', line 69

def rack_header_name
  RACK_HEADER_NAME
end

#serialize(trace_context) ⇒ String

Serialize a TraceContextData object.

Parameters:

Returns:

  • (String)


101
102
103
104
105
106
107
108
109
# File 'lib/opencensus/trace/formatters/trace_context.rb', line 101

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