Class: NewRelic::Agent::DistributedTracePayload

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

Overview

This class contains properties related to distributed traces. To obtain an instance, call DistributedTracing#create_distributed_trace_payload

Constant Summary collapse

VERSION =
[0, 1].freeze
PARENT_TYPE =
'App'
VERSION_KEY =

Key names for serialization

'v'
DATA_KEY =
'd'
PARENT_TYPE_KEY =
'ty'
PARENT_ACCOUNT_ID_KEY =
'ac'
PARENT_APP_KEY =
'ap'
TRUSTED_ACCOUNT_KEY =
'tk'
ID_KEY =
'id'
TX_KEY =
'tx'
TRACE_ID_KEY =
'tr'
SAMPLED_KEY =
'sa'
TIMESTAMP_KEY =
'ti'
PRIORITY_KEY =
'pr'

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

Instance Attribute Details

#idObject



106
107
108
# File 'lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb', line 106

def id
  @id
end

#parent_account_idObject



106
107
108
# File 'lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb', line 106

def 
  @parent_account_id
end

#parent_app_idObject



106
107
108
# File 'lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb', line 106

def parent_app_id
  @parent_app_id
end

#parent_typeObject



106
107
108
# File 'lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb', line 106

def parent_type
  @parent_type
end

#priorityObject



106
107
108
# File 'lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb', line 106

def priority
  @priority
end

#sampledObject Also known as: sampled?



106
107
108
# File 'lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb', line 106

def sampled
  @sampled
end

#timestampObject



106
107
108
# File 'lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb', line 106

def timestamp
  @timestamp
end

#trace_idObject



106
107
108
# File 'lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb', line 106

def trace_id
  @trace_id
end

#transaction_idObject



106
107
108
# File 'lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb', line 106

def transaction_id
  @transaction_id
end

#trusted_account_keyObject



106
107
108
# File 'lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb', line 106

def 
  @trusted_account_key
end

#versionObject



106
107
108
# File 'lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb', line 106

def version
  @version
end

Class Method Details

.for_transaction(transaction) ⇒ 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/distributed_trace_payload.rb', line 37

def for_transaction(transaction)
  return nil unless Agent.instance.connected?

  payload = new
  payload.version = VERSION
  payload.parent_type = PARENT_TYPE
  payload. = Agent.config[:account_id]
  payload.parent_app_id = Agent.config[:primary_application_id]

  (payload, payload.)

  payload.id = current_segment_id(transaction)
  payload.transaction_id = transaction.guid
  payload.timestamp = Process.clock_gettime(Process::CLOCK_REALTIME, :millisecond)
  payload.trace_id = transaction.trace_id
  payload.sampled = transaction.sampled?
  payload.priority = transaction.priority

  payload
end

.from_http_safe(http_safe_payload) ⇒ Object



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

def from_http_safe(http_safe_payload)
  decoded_payload = NewRelic::Base64.strict_decode64(http_safe_payload)
  from_json(decoded_payload)
end

.from_json(serialized_payload) ⇒ Object



58
59
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/distributed_trace_payload.rb', line 58

def from_json(serialized_payload)
  raw_payload = JSON.parse(serialized_payload)
  return raw_payload if raw_payload.nil?

  payload_data = raw_payload[DATA_KEY]

  payload = new
  payload.version = raw_payload[VERSION_KEY]
  payload.parent_type = payload_data[PARENT_TYPE_KEY]
  payload. = payload_data[PARENT_ACCOUNT_ID_KEY]
  payload.parent_app_id = payload_data[PARENT_APP_KEY]
  payload. = payload_data[TRUSTED_ACCOUNT_KEY]
  payload.timestamp = payload_data[TIMESTAMP_KEY]
  payload.id = payload_data[ID_KEY]
  payload.transaction_id = payload_data[TX_KEY]
  payload.trace_id = payload_data[TRACE_ID_KEY]
  payload.sampled = payload_data[SAMPLED_KEY]
  payload.priority = payload_data[PRIORITY_KEY]

  payload
end

.major_version_matches?(payload) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
# File 'lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb', line 85

def major_version_matches?(payload)
  payload.version[0] == VERSION[0]
end

Instance Method Details

#http_safeString

Encode this payload as a string suitable for passing via an HTTP header.

Returns:

  • (String)

    Payload translated to JSON and encoded for inclusion in headers



154
155
156
# File 'lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb', line 154

def http_safe
  NewRelic::Base64.strict_encode64(text)
end

#textString

Represent this payload as a raw JSON string.

Returns:

  • (String)

    Payload translated to JSON



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb', line 125

def text
  result = {
    VERSION_KEY => version
  }

  result[DATA_KEY] = {
    PARENT_TYPE_KEY => parent_type,
    PARENT_ACCOUNT_ID_KEY => ,
    PARENT_APP_KEY => parent_app_id,
    TX_KEY => transaction_id,
    TRACE_ID_KEY => trace_id,
    SAMPLED_KEY => sampled,
    PRIORITY_KEY => priority,
    TIMESTAMP_KEY => timestamp
  }

  result[DATA_KEY][ID_KEY] = id if id
  result[DATA_KEY][TRUSTED_ACCOUNT_KEY] =  if 

  JSON.dump(result)
end