class NewRelic::Agent::DistributedTracePayload

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

@api public

Constants

DATA_KEY
ID_KEY
PARENT_ACCOUNT_ID_KEY
PARENT_APP_KEY
PARENT_TYPE
PARENT_TYPE_KEY
PRIORITY_KEY
SAMPLED_KEY
TIMESTAMP_KEY
TRACE_ID_KEY
TRUSTED_ACCOUNT_KEY
TX_KEY
VERSION
VERSION_KEY

Key names for serialization

Attributes

id[RW]
parent_account_id[RW]
parent_app_id[RW]
parent_type[RW]
priority[RW]
sampled[RW]
sampled?[RW]
timestamp[RW]
trace_id[RW]
transaction_id[RW]
trusted_account_key[RW]
version[RW]

Public Class Methods

for_transaction(transaction) click to toggle source
# 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.parent_account_id = Agent.config[:account_id]
  payload.parent_app_id = Agent.config[:primary_application_id]

  assign_trusted_account_key(payload, payload.parent_account_id)

  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) click to toggle source
# 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) click to toggle source
# 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.parent_account_id = payload_data[PARENT_ACCOUNT_ID_KEY]
  payload.parent_app_id = payload_data[PARENT_APP_KEY]
  payload.trusted_account_key = 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) click to toggle source
# File lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb, line 85
def major_version_matches?(payload)
  payload.version[0] == VERSION[0]
end

Private Class Methods

assign_trusted_account_key(payload, account_id) click to toggle source
# File lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb, line 91
def assign_trusted_account_key(payload, account_id)
  trusted_account_key = Agent.config[:trusted_account_key]

  if account_id != trusted_account_key
    payload.trusted_account_key = trusted_account_key
  end
end
current_segment_id(transaction) click to toggle source
# File lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb, line 99
def current_segment_id(transaction)
  if Agent.config[:'span_events.enabled'] && transaction.current_segment
    transaction.current_segment.guid
  end
end

Public Instance Methods

http_safe() click to toggle source

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

@return [String] Payload translated to JSON and encoded for

inclusion in headers

@api public

# File lib/new_relic/agent/distributed_tracing/distributed_trace_payload.rb, line 154
def http_safe
  NewRelic::Base64.strict_encode64(text)
end
text() click to toggle source

Represent this payload as a raw JSON string.

@return [String] Payload translated to JSON

@api public

# 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_account_id,
    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] = trusted_account_key if trusted_account_key

  JSON.dump(result)
end