Class: NewRelic::Agent::DistributedTracing::TraceContext

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/distributed_tracing/trace_context.rb

Defined Under Namespace

Classes: HeaderData

Constant Summary collapse

VERSION =
0x0
COMMA =
','
EQUALS =
'='
INVALID_TRACE_ID =
('0' * 32)
INVALID_PARENT_ID =
('0' * 16)
INVALID_VERSION =
'ff'
TRACE_ID_KEY =
'trace_id'
TRACE_FLAGS_KEY =
'trace_flags'
PARENT_ID_KEY =
'parent_id'
VERSION_KEY =
'version'
UNDEFINED_FIELDS_KEY =
'undefined_fields'
TP_VERSION =
"(?<#{VERSION_KEY}>[a-f\\d]{2})"
TP_TRACE_ID =
"(?<#{TRACE_ID_KEY}>[a-f\\d]{32})"
TP_PARENT_ID =
"(?<#{PARENT_ID_KEY}>[a-f\\d]{16})"
TP_TRACE_FLAGS =
"(?<#{TRACE_FLAGS_KEY}>\\d{2})"
TP_UNDEFINED_FIELDS =
"(?<#{UNDEFINED_FIELDS_KEY}>-[a-zA-Z\\d-]*)"
TRACE_PARENT_REGEX =
/\A#{TP_VERSION}-#{TP_TRACE_ID}-#{TP_PARENT_ID}-#{TP_TRACE_FLAGS}#{TP_UNDEFINED_FIELDS}?\z/
TRACE_PARENT_FORMAT_STRING =
'%02x-%s-%s-%02x'
MAX_TRACE_STATE_SIZE =

bytes

512
MAX_TRACE_STATE_ENTRY_SIZE =

bytes

128
SUPPORTABILITY_TRACE_PARENT_PARSE_EXCEPTION =
'Supportability/TraceContext/TraceParent/Parse/Exception'
SUPPORTABILITY_TRACE_STATE_PARSE_EXCEPTION =
'Supportability/TraceContext/TraceState/Parse/Exception'
SUPPORTABILITY_TRACE_STATE_INVALID_NR_ENTRY =
'Supportability/TraceContext/TraceState/InvalidNrEntry'

Class Method Summary collapse

Class Method Details

.create_trace_state_entry(entry_key, payload) ⇒ Object



80
81
82
# File 'lib/new_relic/agent/distributed_tracing/trace_context.rb', line 80

def create_trace_state_entry(entry_key, payload)
  +"#{entry_key}=#{payload}"
end

.insert(format: NewRelic::FORMAT_NON_RACK, carrier: nil, parent_id: nil, trace_id: nil, trace_flags: nil, trace_state: nil) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/new_relic/agent/distributed_tracing/trace_context.rb', line 42

def insert(format: NewRelic::FORMAT_NON_RACK,
  carrier: nil,
  parent_id: nil,
  trace_id: nil,
  trace_flags: nil,
  trace_state: nil)

  trace_parent_header = trace_parent_header_for_format(format)
  carrier[trace_parent_header] = format_trace_parent( \
    trace_id: trace_id,
    parent_id: parent_id,
    trace_flags: trace_flags
  )

  trace_state_header = trace_state_header_for_format(format)
  carrier[trace_state_header] = trace_state if trace_state && !trace_state.empty?
end

.parse(format: NewRelic::FORMAT_NON_RACK, carrier: nil, trace_state_entry_key: nil) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/new_relic/agent/distributed_tracing/trace_context.rb', line 60

def parse(format: NewRelic::FORMAT_NON_RACK,
  carrier: nil,
  trace_state_entry_key: nil)
  trace_parent = extract_traceparent(format, carrier)
  unless trace_parent_valid?(trace_parent)
    NewRelic::Agent.increment_metric(SUPPORTABILITY_TRACE_PARENT_PARSE_EXCEPTION)
    return nil
  end

  begin
    if header_data = extract_tracestate(format, carrier, trace_state_entry_key)
      header_data.trace_parent = trace_parent
      header_data
    end
  rescue Exception
    NewRelic::Agent.increment_metric(SUPPORTABILITY_TRACE_STATE_PARSE_EXCEPTION)
    return nil
  end
end