Module: Honeycomb::W3CPropagation::UnmarshalTraceContext

Defined in:
lib/honeycomb/propagation/w3c.rb

Overview

Parse trace headers

Constant Summary collapse

INVALID_TRACE_ID =
"00000000000000000000000000000000"
INVALID_SPAN_ID =
"0000000000000000"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(serialized_trace) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/honeycomb/propagation/w3c.rb', line 15

def parse(serialized_trace)
  unless serialized_trace.nil?
    version, payload = serialized_trace.split("-", 2)
    # version should be 2 hex characters
    if /^[A-Fa-f0-9]{2}$/.match?(version)
      trace_id, parent_span_id = parse_v1(payload)

      if !trace_id.nil? && !parent_span_id.nil?
        # return nil for dataset
        return [trace_id, parent_span_id, nil, nil]
      end
    end
  end
  [nil, nil, nil, nil]
end

.parse_rack_env(env) ⇒ Object



11
12
13
# File 'lib/honeycomb/propagation/w3c.rb', line 11

def parse_rack_env(env)
  parse env["HTTP_TRACEPARENT"]
end

.parse_v1(payload) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/honeycomb/propagation/w3c.rb', line 31

def parse_v1(payload)
  trace_id, parent_span_id, trace_flags = payload.split("-", 3)

  # if trace_flags is nil, it means a field is missing
  if trace_flags.nil? || trace_id == INVALID_TRACE_ID || parent_span_id == INVALID_SPAN_ID
    return [nil, nil]
  end

  [trace_id, parent_span_id]
end

Instance Method Details

#parse(serialized_trace) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/honeycomb/propagation/w3c.rb', line 15

def parse(serialized_trace)
  unless serialized_trace.nil?
    version, payload = serialized_trace.split("-", 2)
    # version should be 2 hex characters
    if /^[A-Fa-f0-9]{2}$/.match?(version)
      trace_id, parent_span_id = parse_v1(payload)

      if !trace_id.nil? && !parent_span_id.nil?
        # return nil for dataset
        return [trace_id, parent_span_id, nil, nil]
      end
    end
  end
  [nil, nil, nil, nil]
end