Class: TingYun::Agent::Transaction::Trace

Inherits:
Object
  • Object
show all
Includes:
Support::Coerce
Defined in:
lib/ting_yun/agent/transaction/trace.rb

Constant Summary collapse

EMPTY_STRING =
''.freeze
HEX_DIGITS =
(0..15).map{|i| i.to_s(16)}
GUID_LENGTH =
16

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support::Coerce

event_params, float, int, int_or_nil, log_failure, string, url_encode

Constructor Details

#initialize(start_time) ⇒ Trace

Returns a new instance of Trace.



15
16
17
18
19
20
21
22
# File 'lib/ting_yun/agent/transaction/trace.rb', line 15

def initialize(start_time)
  @start_time = start_time
  @node_count = 0
  @prepared = false
  @guid = generate_guid
  @root_node = TingYun::Agent::Transaction::TraceNode.new(0.0, "ROOT",-1)
  @e_set = Set.new
end

Instance Attribute Details

#array_sizeObject

Returns the value of attribute array_size.



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

def array_size
  @array_size
end

#attributesObject

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#e_setObject

Returns the value of attribute e_set.



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

def e_set
  @e_set
end

#finishedObject

Returns the value of attribute finished.



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

def finished
  @finished
end

#guidObject

Returns the value of attribute guid.



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

def guid
  @guid
end

#node_countObject

Returns the value of attribute node_count.



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

def node_count
  @node_count
end

#root_nodeObject

Returns the value of attribute root_node.



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

def root_node
  @root_node
end

#start_timeObject

Returns the value of attribute start_time.



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

def start_time
  @start_time
end

#thresholdObject

Returns the value of attribute threshold.



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

def threshold
  @threshold
end

Instance Method Details

#add_errors(errors) ⇒ Object



162
163
164
165
166
167
168
169
# File 'lib/ting_yun/agent/transaction/trace.rb', line 162

def add_errors(errors)
  errors.each do |error|
    unless @e_set.member? error.object_id
      @e_set.add error.object_id
      root_node.add_error(error)
    end
  end
end

#add_errors_to_current_node(state, error) ⇒ Object



171
172
173
174
175
176
# File 'lib/ting_yun/agent/transaction/trace.rb', line 171

def add_errors_to_current_node(state, error)
  unless @e_set.member? error.object_id
    @e_set.add error.object_id
    state.transaction_sample_builder.current_node.add_error(error)
  end
end

#collect_explain_plans!Object



120
121
122
123
124
125
126
127
128
# File 'lib/ting_yun/agent/transaction/trace.rb', line 120

def collect_explain_plans!
  return unless TingYun::Agent::Database.should_action_collect_explain_plans?
  threshold = TingYun::Agent.config[:'action_tracer.action_threshold']
  root_node.each_call do |node|
    if node[:sql] && node.duration > threshold
      node[:explainPlan] = node.explain_sql
    end
  end
end

#create_node(time_since_start, metric_name = nil) ⇒ Object



24
25
26
27
# File 'lib/ting_yun/agent/transaction/trace.rb', line 24

def create_node(time_since_start, metric_name = nil)
  @node_count += 1
  TingYun::Agent::Transaction::TraceNode.new(time_since_start, metric_name,@node_count)
end

#custom_paramsObject



146
147
148
149
150
151
152
153
154
155
# File 'lib/ting_yun/agent/transaction/trace.rb', line 146

def custom_params
  custom_param = {
      :threadName => string(attributes.agent_attributes[:threadName]),
      :referer    => string(attributes.agent_attributes[:referer]) || EMPTY_STRING
  }
  custom_param[:httpStatus] = int(attributes.agent_attributes[:httpStatus]) if attributes.agent_attributes[:httpStatus]
  custom_param[:entryTrace] = attributes.agent_attributes[:entryTrace] if attributes.agent_attributes[:entryTrace]
  custom_param.merge! attributes.custom_params
  custom_param
end

#durationObject



29
30
31
# File 'lib/ting_yun/agent/transaction/trace.rb', line 29

def duration
  root_node.duration
end

#prepare_sql_for_transmission!(&block) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/ting_yun/agent/transaction/trace.rb', line 130

def prepare_sql_for_transmission!(&block)
  strategy = TingYun::Agent::Database.record_sql_method('nbs.action_tracer.record_sql')
  root_node.each_call do |node|
    next unless node[:sql]

    case strategy
      when :obfuscated
        node[:sql] = TingYun::Agent::Database.obfuscate_sql(node[:sql])
      when :raw
        node[:sql] = node[:sql].sql.to_s
      else
        node[:sql] = nil
    end
  end
end

#prepare_to_send!Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/ting_yun/agent/transaction/trace.rb', line 105

def prepare_to_send!
  return self if @prepared

  if TingYun::Agent::Database.should_record_sql?('nbs.action_tracer.record_sql')
    collect_explain_plans!
    prepare_sql_for_transmission!
  else
    root_node.each_call do |node|
      node.params.delete(:sql)
    end
  end
  @prepared = true
  self
end

#request_paramsObject



157
158
159
160
# File 'lib/ting_yun/agent/transaction/trace.rb', line 157

def request_params
  return {} unless TingYun::Agent.config['action_tracer.capture_params']
  attributes.request_params
end

#to_collector_array(encoder) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ting_yun/agent/transaction/trace.rb', line 80

def to_collector_array(encoder)
  hash = {
    "time" => @start_time.round,
    "tid" => attributes.agent_attributes[:tx_id],
    "rid" => guid,
    "duration" => duration,
    "ip"=> attributes.agent_attributes[:ip]||"",
    "method" => attributes.agent_attributes[:method]||"UNKNOWN",
    "status" => attributes.agent_attributes[:httpStatus]||0,
    "custom" => attributes.custom_params,
    "detail" => trace_tree
  }
  hash["user"] = attributes. unless attributes..empty?
  hash["cross"] = attributes.agent_attributes[:cross]  unless attributes.agent_attributes[:cross].nil?
  hash["refid"] = attributes.agent_attributes[:refid]  unless attributes.agent_attributes[:refid].nil?
  actionName = TingYun::Helper.correctly_encoded(attributes.agent_attributes[:metric_name])
  unless actionName.nil?
   hash["tmd5"] = attributes.agent_attributes[:tmd5] || Digest::MD5.hexdigest(actionName)
   hash["action"]= actionName
  end
  url = TingYun::Helper.correctly_encoded(attributes.agent_attributes[:url])
  hash["url"]=url unless url.nil?
  hash
end

#to_collector_arrayV2(encoder) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/ting_yun/agent/transaction/trace.rb', line 68

def to_collector_arrayV2(encoder)
  [
      @start_time.round,
      duration,
      TingYun::Helper.correctly_encoded(attributes.agent_attributes[:metric_name]|| EMPTY_STRING),
      TingYun::Helper.correctly_encoded(attributes.agent_attributes[:request_path]||attributes.agent_attributes[:metric_name]|| EMPTY_STRING),
      encoder.encode(trace_tree),
      attributes.agent_attributes[:tx_id],
      guid
  ] + array_size
end

#trace_treeObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/ting_yun/agent/transaction/trace.rb', line 47

def trace_tree
 hash =  {
    "params" =>attributes.agent_attributes[:params],
    "queryStringParameters" => attributes.agent_attributes[:queryString],
    "custom" => {
        :threadName => attributes.agent_attributes[:threadName]
    },
    "responseHeader" =>attributes.response_header,
    "requestHeader" =>attributes.request_header,
    "session" => attributes.agent_attributes[:cookie],
    "tracers" => root_node.to_hash.flatten!
  }
  if attributes.agent_attributes[:method] == "GET"
    hash["params"] = attributes.agent_attributes[:params]
  else
    hash["posts"] = attributes.agent_attributes[:params]
  end

  hash
end

#trace_treeV2Object



38
39
40
41
42
43
44
45
# File 'lib/ting_yun/agent/transaction/trace.rb', line 38

def trace_treeV2
  [
      @start_time.round,
      request_params,
      custom_params,
      root_node.to_array
  ]
end