class NewRelic::Agent::Transaction::TraceNode

Constants

UNKNOWN_NODE_NAME

Attributes

children[W]
entry_timestamp[R]
exit_timestamp[RW]

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

metric_name[RW]
params[W]
parent_node[R]

Public Class Methods

new(metric_name, relative_start, relative_end = nil, params = nil, parent = nil) click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 20
def initialize(metric_name, relative_start, relative_end = nil, params = nil, parent = nil)
  @entry_timestamp = relative_start
  @metric_name = metric_name || UNKNOWN_NODE_NAME
  @exit_timestamp = relative_end
  @children = nil
  @params = select_allowed_params(params)
  @parent_node = parent
end

Public Instance Methods

[](key) click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 130
def [](key)
  params[key]
end
[]=(key, value) click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 124
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
children() click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 90
def children
  @children ||= []
end
count_nodes() click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 112
def count_nodes
  count = 1
  children.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 97
def duration
  (@exit_timestamp - @entry_timestamp).to_f
end
each_node() { |self| ... } 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 136
def each_node(&block)
  yield(self)

  @children&.each do |node|
    node.each_node(&block)
  end
end
each_node_with_nest_tracking() { |self| ... } 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 146
def each_node_with_nest_tracking(&block)
  summary = yield(self)
  summary.current_nest_count += 1 if summary

  # no then branch coverage
  # rubocop:disable Style/SafeNavigation
  if @children
    @children.each do |node|
      node.each_node_with_nest_tracking(&block)
    end
  end
  # rubocop:enable Style/SafeNavigation

  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 39
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 103
def exclusive_duration
  d = duration

  children.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 162
def explain_sql
  return params[:explain_plan] if params.key?(:explain_plan)

  statement = params[:sql]
  return nil unless statement.is_a?(Database::Statement)

  NewRelic::Agent::Database.explain_sql(statement)
end
obfuscated_sql() click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 171
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
parent_node=(s) click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 175
def parent_node=(s)
  @parent_node = s
end
path_string() click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 56
def path_string
  "#{metric_name}[#{children.collect { |node| node.path_string }.join('')}]"
end
select_allowed_params(params) click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 29
def select_allowed_params(params)
  return unless params

  params.select do |p|
    NewRelic::Agent.instance.attribute_filter.allows_key?(p, AttributeFilter::DST_TRANSACTION_SEGMENTS)
  end
end
to_array() click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 47
def to_array
  params = @params || NewRelic::EMPTY_HASH
  [NewRelic::Helper.time_to_millis(@entry_timestamp),
    NewRelic::Helper.time_to_millis(@exit_timestamp),
    NewRelic::Coerce.string(@metric_name),
    params] +
    [(@children ? @children.map { |s| s.to_array } : NewRelic::EMPTY_ARRAY)]
end
to_debug_str(depth) click to toggle source
# File lib/new_relic/agent/transaction/trace_node.rb, line 69
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
  children.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 43
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 60
def to_s_compact
  str = +''
  str << metric_name
  if children.any?
    str << "{#{children.map { |cs| cs.to_s_compact }.join(',')}}"
  end
  str
end