Class: NewRelic::Agent::TraceContextPayload

Inherits:
Object
  • Object
show all
Extended by:
Coerce
Defined in:
lib/new_relic/agent/distributed_tracing/trace_context_payload.rb

Constant Summary collapse

VERSION =
0
PARENT_TYPE =
0
DELIMITER =
'-'.freeze
SUPPORTABILITY_PARSE_EXCEPTION =
'Supportability/TraceContext/Parse/Exception'.freeze
TRUE_CHAR =
'1'.freeze
FALSE_CHAR =
'0'.freeze
PARENT_TYPES =
%w[App Browser Mobile].map(&:freeze).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Coerce

boolean_int!, float, float!, int, int!, int_or_nil, log_failure, scalar, string, value_or_nil

Constructor Details

#initialize(version, parent_type_id, parent_account_id, parent_app_id, id, transaction_id, sampled, priority, timestamp) ⇒ TraceContextPayload

Returns a new instance of TraceContextPayload.



86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/new_relic/agent/distributed_tracing/trace_context_payload.rb', line 86

def initialize(version, parent_type_id, , parent_app_id,
  id, transaction_id, sampled, priority, timestamp)
  @version = version
  @parent_type_id = parent_type_id
  @parent_account_id = 
  @parent_app_id = parent_app_id
  @id = id
  @transaction_id = transaction_id
  @sampled = sampled
  @priority = priority
  @timestamp = timestamp
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



74
75
76
# File 'lib/new_relic/agent/distributed_tracing/trace_context_payload.rb', line 74

def id
  @id
end

#parent_account_idObject

Returns the value of attribute parent_account_id.



74
75
76
# File 'lib/new_relic/agent/distributed_tracing/trace_context_payload.rb', line 74

def 
  @parent_account_id
end

#parent_app_idObject

Returns the value of attribute parent_app_id.



74
75
76
# File 'lib/new_relic/agent/distributed_tracing/trace_context_payload.rb', line 74

def parent_app_id
  @parent_app_id
end

#parent_type_idObject

Returns the value of attribute parent_type_id.



74
75
76
# File 'lib/new_relic/agent/distributed_tracing/trace_context_payload.rb', line 74

def parent_type_id
  @parent_type_id
end

#priorityObject

Returns the value of attribute priority.



74
75
76
# File 'lib/new_relic/agent/distributed_tracing/trace_context_payload.rb', line 74

def priority
  @priority
end

#sampledObject Also known as: sampled?

Returns the value of attribute sampled.



74
75
76
# File 'lib/new_relic/agent/distributed_tracing/trace_context_payload.rb', line 74

def sampled
  @sampled
end

#timestampObject

Returns the value of attribute timestamp.



74
75
76
# File 'lib/new_relic/agent/distributed_tracing/trace_context_payload.rb', line 74

def timestamp
  @timestamp
end

#transaction_idObject

Returns the value of attribute transaction_id.



74
75
76
# File 'lib/new_relic/agent/distributed_tracing/trace_context_payload.rb', line 74

def transaction_id
  @transaction_id
end

#versionObject

Returns the value of attribute version.



74
75
76
# File 'lib/new_relic/agent/distributed_tracing/trace_context_payload.rb', line 74

def version
  @version
end

Class Method Details

.create(version: VERSION, parent_type: PARENT_TYPE, parent_account_id: nil, parent_app_id: nil, id: nil, transaction_id: nil, sampled: nil, priority: nil, timestamp: now_ms) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/new_relic/agent/distributed_tracing/trace_context_payload.rb', line 21

def create(version: VERSION,
  parent_type: PARENT_TYPE,
  parent_account_id: nil,
  parent_app_id: nil,
  id: nil,
  transaction_id: nil,
  sampled: nil,
  priority: nil,
  timestamp: now_ms)

  new(version, parent_type, , parent_app_id, id,
    transaction_id, sampled, priority, timestamp)
end

.from_s(payload_string) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/new_relic/agent/distributed_tracing/trace_context_payload.rb', line 37

def from_s(payload_string)
  attrs = payload_string.split(DELIMITER)

  payload = create( \
    version: int!(attrs[0]),
    parent_type: int!(attrs[1]),
    parent_account_id: attrs[2],
    parent_app_id: attrs[3],
    id: value_or_nil(attrs[4]),
    transaction_id: value_or_nil(attrs[5]),
    sampled: value_or_nil(attrs[6]) ? boolean_int!(attrs[6]) == 1 : nil,
    priority: float!(attrs[7]),
    timestamp: int!(attrs[8])
  )
  handle_invalid_payload(message: 'payload missing attributes') unless payload.valid?
  payload
rescue => e
  handle_invalid_payload(error: e)
  raise
end

Instance Method Details

#parent_typeObject



99
100
101
# File 'lib/new_relic/agent/distributed_tracing/trace_context_payload.rb', line 99

def parent_type
  @parent_type_string ||= PARENT_TYPES[@parent_type_id]
end

#to_sObject



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/new_relic/agent/distributed_tracing/trace_context_payload.rb', line 113

def to_s
  result = version.to_s # required
  result << DELIMITER << parent_type_id.to_s # required
  result << DELIMITER <<  # required
  result << DELIMITER << parent_app_id # required
  result << DELIMITER << (id || NewRelic::EMPTY_STR)
  result << DELIMITER << (transaction_id || NewRelic::EMPTY_STR)
  result << DELIMITER << (sampled ? TRUE_CHAR : FALSE_CHAR)
  result << DELIMITER << sprintf('%.6f', priority)
  result << DELIMITER << timestamp.to_s # required
  result
end

#valid?Boolean

Returns:

  • (Boolean)


103
104
105
106
107
108
109
110
111
# File 'lib/new_relic/agent/distributed_tracing/trace_context_payload.rb', line 103

def valid?
  version \
    && parent_type_id \
    && !.empty? \
    && !parent_app_id.empty? \
    && timestamp
rescue
  false
end