class NewRelic::Agent::Transaction::TraceNode

Constants

UNKNOWN_NODE_NAME

Attributes

entry_timestamp[R]
exit_timestamp[R]

The exit timestamp will be relative except for the outermost sample which will have a timestamp.

metric_name[RW]
parent_node[R]

Public Class Methods

new(timestamp, metric_name) click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 18
def initialize(timestamp, metric_name)
  @entry_timestamp = timestamp
  @metric_name     = metric_name || UNKNOWN_NODE_NAME
  @called_nodes    = nil
end

Public Instance Methods

[](key) click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 114
def [](key)
  params[key]
end
[]=(key, value) click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 108
def []=(key, value)
  # only create a parameters field if a parameter is set; this will save
  # bandwidth etc as most nodes have no parameters
  params[key] = value
end
add_called_node(s) click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 30
def add_called_node(s)
  @called_nodes ||= []
  @called_nodes << s
  s.parent_node = self
end
called_nodes() click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 82
def called_nodes
  @called_nodes || []
end
called_nodes=(nodes) click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 179
def called_nodes=(nodes)
  @called_nodes = nodes
end
count_nodes() click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 102
def count_nodes
  count = 1
  called_nodes.each { | node | count  += node.count_nodes }
  count
end
duration() click to toggle source

return the total duration of this node

# File lib/new_relic/agent/transaction/trace_node.rb, line 87
def duration
  (@exit_timestamp - @entry_timestamp).to_f
end
each_node(&block) click to toggle source

call the provided block for this node and each of the called nodes

# File lib/new_relic/agent/transaction/trace_node.rb, line 128
def each_node(&block)
  block.call self

  if @called_nodes
    @called_nodes.each do |node|
      node.each_node(&block)
    end
  end
end
each_node_with_nest_tracking(&block) click to toggle source

call the provided block for this node and each of the called nodes while keeping track of nested nodes

# File lib/new_relic/agent/transaction/trace_node.rb, line 140
def each_node_with_nest_tracking(&block)
  summary = block.call self
  summary.current_nest_count += 1 if summary

  if @called_nodes
    @called_nodes.each do |node|
      node.each_node_with_nest_tracking(&block)
    end
  end

  summary.current_nest_count -= 1 if summary
end
end_trace(timestamp) click to toggle source

sets the final timestamp on a node to indicate the exit point of the node

# File lib/new_relic/agent/transaction/trace_node.rb, line 26
def end_trace(timestamp)
  @exit_timestamp = timestamp
end
exclusive_duration() click to toggle source

return the duration of this node without including the time in the called nodes

# File lib/new_relic/agent/transaction/trace_node.rb, line 93
def exclusive_duration
  d = duration

  called_nodes.each do |node|
    d -= node.duration
  end
  d
end
explain_sql() click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 163
def explain_sql
  return params[:explain_plan] if params.key?(:explain_plan)

  statement = params[:sql]
  return nil unless statement.respond_to?(:config) &&
    statement.respond_to?(:explainer)

  NewRelic::Agent::Database.explain_sql(statement.sql,
                                        statement.config,
                                        statement.explainer)
end
find_node(id) click to toggle source

This is only for use by developer mode

# File lib/new_relic/agent/transaction/trace_node.rb, line 154
def find_node(id)
  return self if object_id == id
  called_nodes.each do |node|
    found = node.find_node(id)
    return found if found
  end
  nil
end
obfuscated_sql() click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 175
def obfuscated_sql
  NewRelic::Agent::Database.obfuscate_sql(params[:sql].sql)
end
params() click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 118
def params
  @params ||= {}
end
params=(p) click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 122
def params=(p)
  @params = p
end
path_string() click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 48
def path_string
  "#{metric_name}[#{called_nodes.collect {|node| node.path_string }.join('')}]"
end
to_array() click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 40
def to_array
  [ NewRelic::Helper.time_to_millis(@entry_timestamp),
    NewRelic::Helper.time_to_millis(@exit_timestamp),
    NewRelic::Coerce.string(@metric_name),
    (@params || {}) ] +
    [ (@called_nodes ? @called_nodes.map{|s| s.to_array} : []) ]
end
to_debug_str(depth) click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 61
def to_debug_str(depth)
  tab = "  " * depth
  s = tab.clone
  s << ">> #{'%3i ms' % (@entry_timestamp*1000)} [#{self.class.name.split("::").last}] #{metric_name} \n"
  unless params.empty?
    params.each do |k,v|
      s << "#{tab}    -#{'%-16s' % k}: #{v.to_s[0..80]}\n"
    end
  end
  called_nodes.each do |cs|
    s << cs.to_debug_str(depth + 1)
  end
  s << tab + "<< "
  s << case @exit_timestamp
  when nil then ' n/a'
  when Numeric then '%3i ms' % (@exit_timestamp*1000)
  else @exit_timestamp.to_s
  end
  s << " #{metric_name}\n"
end
to_s() click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 36
def to_s
  to_debug_str(0)
end
to_s_compact() click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 52
def to_s_compact
  str = ""
  str << metric_name
  if called_nodes.any?
    str << "{#{called_nodes.map { | cs | cs.to_s_compact }.join(",")}}"
  end
  str
end

Protected Instance Methods

parent_node=(s) click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 184
def parent_node=(s)
  @parent_node = s
end