Class: NewRelic::Agent::Transaction::Trace

Inherits:
Object
  • Object
show all
Defined in:
lib/new_relic/agent/transaction/trace.rb

Defined Under Namespace

Classes: FinishedTraceError

Constant Summary collapse

ROOT =
'ROOT'.freeze
AGENT_ATTRIBUTES_KEY =
'agentAttributes'.freeze
USER_ATTRIBUTES_KEY =
'userAttributes'.freeze
INTRINSIC_ATTRIBUTES_KEY =
'intrinsics'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_time) ⇒ Trace

Returns a new instance of Trace.



19
20
21
22
23
24
# File 'lib/new_relic/agent/transaction/trace.rb', line 19

def initialize(start_time)
  @start_time = start_time
  @node_count = 0
  @root_node = NewRelic::Agent::Transaction::TraceNode.new(ROOT, 0.0, nil, nil, nil)
  @prepared = false
end

Instance Attribute Details

#attributesObject



14
15
16
# File 'lib/new_relic/agent/transaction/trace.rb', line 14

def attributes
  @attributes
end

#finishedObject



14
15
16
# File 'lib/new_relic/agent/transaction/trace.rb', line 14

def finished
  @finished
end

#guidObject



14
15
16
# File 'lib/new_relic/agent/transaction/trace.rb', line 14

def guid
  @guid
end

#node_countObject



14
15
16
# File 'lib/new_relic/agent/transaction/trace.rb', line 14

def node_count
  @node_count
end

#profileObject



14
15
16
# File 'lib/new_relic/agent/transaction/trace.rb', line 14

def profile
  @profile
end

#root_nodeObject (readonly)



13
14
15
# File 'lib/new_relic/agent/transaction/trace.rb', line 13

def root_node
  @root_node
end

#start_timeObject (readonly)



13
14
15
# File 'lib/new_relic/agent/transaction/trace.rb', line 13

def start_time
  @start_time
end

#thresholdObject



14
15
16
# File 'lib/new_relic/agent/transaction/trace.rb', line 14

def threshold
  @threshold
end

#transaction_nameObject



14
15
16
# File 'lib/new_relic/agent/transaction/trace.rb', line 14

def transaction_name
  @transaction_name
end

Instance Method Details

#attributes_for_tracer_destinationObject



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/new_relic/agent/transaction/trace.rb', line 114

def attributes_for_tracer_destination
  destination = NewRelic::Agent::AttributeFilter::DST_TRANSACTION_TRACER

  agent_attributes = self.attributes.agent_attributes_for(destination)
  custom_attributes = self.attributes.custom_attributes_for(destination)
  intrinsic_attributes = self.attributes.intrinsic_attributes_for(destination)

  {
    AGENT_ATTRIBUTES_KEY => agent_attributes,
    USER_ATTRIBUTES_KEY => custom_attributes,
    INTRINSIC_ATTRIBUTES_KEY => intrinsic_attributes
  }
end

#collect_explain_plans!Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/new_relic/agent/transaction/trace.rb', line 78

def collect_explain_plans!
  return unless NewRelic::Agent::Database.should_collect_explain_plans?

  threshold = NewRelic::Agent.config[:'transaction_tracer.explain_threshold']

  each_node do |node|
    if node[:sql] && node.duration > threshold
      node[:explain_plan] = node.explain_sql
    end
  end
end

#count_nodesObject



30
31
32
33
34
35
36
37
38
# File 'lib/new_relic/agent/transaction/trace.rb', line 30

def count_nodes
  node_count = 0
  each_node do |node|
    next if node == root_node

    node_count += 1
  end
  node_count
end

#create_node(time_since_start, metric_name = nil) ⇒ Object

Raises:



53
54
55
56
57
58
# File 'lib/new_relic/agent/transaction/trace.rb', line 53

def create_node(time_since_start, metric_name = nil)
  raise FinishedTraceError.new("Can't create additional node for finished trace.") if self.finished

  self.node_count += 1
  NewRelic::Agent::Transaction::TraceNode.new(metric_name, time_since_start)
end

#durationObject



40
41
42
# File 'lib/new_relic/agent/transaction/trace.rb', line 40

def duration
  self.root_node.duration
end

#each_node(&block) ⇒ Object



60
61
62
# File 'lib/new_relic/agent/transaction/trace.rb', line 60

def each_node(&block)
  self.root_node.each_node(&block)
end

#each_node_with_nest_tracking(&block) ⇒ Object

Iterates recursively over each node in the entire transaction sample tree while keeping track of nested nodes



106
107
108
# File 'lib/new_relic/agent/transaction/trace.rb', line 106

def each_node_with_nest_tracking(&block)
  @root_node.each_node_with_nest_tracking(&block)
end

#prepare_sql_for_transmission!Object



90
91
92
93
94
95
96
# File 'lib/new_relic/agent/transaction/trace.rb', line 90

def prepare_sql_for_transmission!
  each_node do |node|
    next unless node[:sql]

    node[:sql] = node[:sql].safe_sql
  end
end

#prepare_to_send!Object



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/new_relic/agent/transaction/trace.rb', line 64

def prepare_to_send!
  return self if @prepared

  if NewRelic::Agent::Database.should_record_sql?
    collect_explain_plans!
    prepare_sql_for_transmission!
  else
    strip_sql!
  end

  @prepared = true
  self
end

#sample_idObject



26
27
28
# File 'lib/new_relic/agent/transaction/trace.rb', line 26

def sample_id
  self.object_id
end

#strip_sql!Object



98
99
100
101
102
# File 'lib/new_relic/agent/transaction/trace.rb', line 98

def strip_sql!
  each_node do |node|
    node.params.delete(:sql)
  end
end

#synthetics_resource_idObject



44
45
46
47
# File 'lib/new_relic/agent/transaction/trace.rb', line 44

def synthetics_resource_id
  intrinsic_attributes = attributes.intrinsic_attributes_for(NewRelic::Agent::AttributeFilter::DST_TRANSACTION_TRACER)
  intrinsic_attributes[:synthetics_resource_id]
end

#to_collector_array(encoder) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/new_relic/agent/transaction/trace.rb', line 138

def to_collector_array(encoder)
  attributes_hash = attributes_for_tracer_destination
  [
    NewRelic::Helper.time_to_millis(self.start_time),
    NewRelic::Helper.time_to_millis(self.root_node.duration),
    NewRelic::Coerce.string(self.transaction_name),
    NewRelic::Coerce.string(attributes_hash[AGENT_ATTRIBUTES_KEY][:'request.uri']),
    encoder.encode(trace_tree(attributes_hash)),
    NewRelic::Coerce.string(self.guid),
    nil,
    false, # forced?,
    nil, # NewRelic::Coerce.int_or_nil(xray_session_id),
    NewRelic::Coerce.string(self.synthetics_resource_id)
  ]
end

#to_s_compactObject



49
50
51
# File 'lib/new_relic/agent/transaction/trace.rb', line 49

def to_s_compact
  @root_node.to_s_compact
end

#trace_tree(attributes_hash) ⇒ Object



128
129
130
131
132
133
134
135
136
# File 'lib/new_relic/agent/transaction/trace.rb', line 128

def trace_tree(attributes_hash)
  [
    NewRelic::Coerce.float(self.start_time),
    NewRelic::EMPTY_HASH,
    NewRelic::EMPTY_HASH,
    self.root_node.to_array,
    attributes_hash
  ]
end