Class: OpenCensus::Trace::Formatters::CloudTrace

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

Overview

This formatter serializes and deserializes span context according to the Google X-Cloud-Trace header specification.

Constant Summary collapse

HEADER_FORMAT =

Internal regex used to parse fields

%r{([0-9a-fA-F]{32})(?:\/(\d+))?(?:;o=(\d+))?}
HEADER_NAME =

The outgoing header used for the Google Cloud Trace header specification.

"X-Cloud-Trace".freeze
RACK_HEADER_NAME =

The rack environment header used the the Google Cloud Trace header specification.

"HTTP_X_CLOUD_TRACE".freeze

Instance Method Summary collapse

Instance Method Details

#deserialize(header) ⇒ TraceContextData?

Deserialize a trace context header into a TraceContext object.

Parameters:

  • header (String)

Returns:



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/opencensus/trace/formatters/cloud_trace.rb', line 71

def deserialize header
  match = HEADER_FORMAT.match(header)
  if match
    trace_id = match[1].downcase
    span_id = format("%016x", match[2].to_i)
    trace_options = match[3].to_i
    TraceContextData.new trace_id, span_id, trace_options
  else
    nil
  end
end

#header_nameString

Returns the name of the header used for context propagation.

Returns:

  • (String)


51
52
53
# File 'lib/opencensus/trace/formatters/cloud_trace.rb', line 51

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)


61
62
63
# File 'lib/opencensus/trace/formatters/cloud_trace.rb', line 61

def rack_header_name
  RACK_HEADER_NAME
end

#serialize(trace_context) ⇒ String

Serialize a TraceContextData object.

Parameters:

Returns:

  • (String)


89
90
91
92
93
94
95
96
97
98
# File 'lib/opencensus/trace/formatters/cloud_trace.rb', line 89

def serialize trace_context
  trace_context.trace_id.dup.tap do |ret|
    if trace_context.span_id
      ret << "/" << trace_context.span_id.to_i(16).to_s
    end
    if trace_context.trace_options
      ret << ";o=" << trace_context.trace_options.to_s
    end
  end
end