Class: TingYun::Agent::Transaction::TraceNode

Inherits:
Object
  • Object
show all
Defined in:
lib/ting_yun/agent/transaction/trace_node.rb

Constant Summary collapse

UNKNOWN_NODE_NAME =
'<unknown>'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(timestamp, metric_name) ⇒ TraceNode

Returns a new instance of TraceNode.



20
21
22
23
24
25
26
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 20

def initialize(timestamp, metric_name)
  @entry_timestamp = timestamp
  @metric_name     = metric_name || UNKNOWN_NODE_NAME
  @called_nodes    = nil
  @count           = 1
  self["exception"] ||= []
end

Instance Attribute Details

#called_nodesObject (readonly)

Returns the value of attribute called_nodes.



12
13
14
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 12

def called_nodes
  @called_nodes
end

#countObject

Returns the value of attribute count.



13
14
15
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 13

def count
  @count
end

#entry_timestampObject (readonly)

Returns the value of attribute entry_timestamp.



12
13
14
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 12

def 
  @entry_timestamp
end

#exit_timestampObject

Returns the value of attribute exit_timestamp.



13
14
15
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 13

def exit_timestamp
  @exit_timestamp
end

#klassObject

Returns the value of attribute klass.



13
14
15
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 13

def klass
  @klass
end

#methodObject

Returns the value of attribute method.



13
14
15
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 13

def method
  @method
end

#metric_nameObject

Returns the value of attribute metric_name.



13
14
15
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 13

def metric_name
  @metric_name
end

#nameObject

Returns the value of attribute name.



13
14
15
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 13

def name
  @name
end

#parent_nodeObject

Returns the value of attribute parent_node.



12
13
14
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 12

def parent_node
  @parent_node
end

#uriObject

Returns the value of attribute uri.



13
14
15
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 13

def uri
  @uri
end

Instance Method Details

#[](key) ⇒ Object



78
79
80
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 78

def [](key)
  params[key]
end

#[]=(key, value) ⇒ Object



72
73
74
75
76
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 72

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) ⇒ Object



28
29
30
31
32
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 28

def add_called_node(s)
  @called_nodes ||= []
  @called_nodes << s
  s.parent_node = self
end

#add_error(error) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 114

def add_error(error)
  if error.respond_to?(:tingyun_external)
    self["exception"] << {"message" => error.message,
                          "class" => "External #{error.tingyun_code}"
                          }
  else
    if ::TingYun::Agent.config[:'nbs.exception.stack_enabled']
      self["exception"] << {"message" => error.message,
                            "class" => error.class.name ,
                            "stacktrace"=> error.backtrace.reject! { |t| t.include?('tingyun_rpm') }
      }
    else
      self["exception"] << {"message" => error.message,
                            "class" => error.class.name
      }
    end
  end
end

#custom_paramsObject



64
65
66
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 64

def custom_params
  {}
end

#durationObject

return the total duration of this node



39
40
41
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 39

def duration
  TingYun::Helper.time_to_millis(@exit_timestamp - @entry_timestamp)
end

#each_call(&blk) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 94

def each_call(&blk)
  blk.call self

  if @called_nodes
    @called_nodes.each do |node|
      node.each_call(&blk)
    end
  end
end

#end_trace(timestamp) ⇒ Object



34
35
36
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 34

def end_trace(timestamp)
  @exit_timestamp = timestamp
end

#explain_sqlObject



104
105
106
107
108
109
110
111
112
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 104

def explain_sql
  return params[:explainPlan] if params.key?(:explainPlan)

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

  TingYun::Agent::Database.explain_sql(statement)
end

#merge(hash) ⇒ Object



90
91
92
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 90

def merge(hash)
  params.merge! hash
end

#paramsObject



82
83
84
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 82

def params
  @params ||= {}
end

#params=(p) ⇒ Object



86
87
88
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 86

def params=(p)
  @params = p
end

#pre_metric_name(metric_name) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 44

def pre_metric_name(metric_name)
  @name ||= if metric_name.start_with?('Database ')
              "#{metric_name.split('/')[0]}%2F#{metric_name.split('%2F')[-1]}"
            else
              metric_name
            end
end

#request_paramsObject



68
69
70
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 68

def request_params
  {}
end

#to_arrayObject



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ting_yun/agent/transaction/trace_node.rb', line 52

def to_array
  [TingYun::Helper.time_to_millis(),
   TingYun::Helper.time_to_millis(exit_timestamp),
   TingYun::Support::Coerce.string(metric_name),
   TingYun::Support::Coerce.string(uri)||'',
   TingYun::Support::Coerce.int(count),
   TingYun::Support::Coerce.string(klass)||TingYun::Support::Coerce.string(pre_metric_name(metric_name)),
   TingYun::Support::Coerce.string(method)||'',
   params] +
      [(@called_nodes ? @called_nodes.map{|s| s.to_array} : [])]
end