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.



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

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")
  @e_set = Set.new
end

Instance Attribute Details

#array_sizeObject

Returns the value of attribute array_size.



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

def array_size
  @array_size
end

#attributesObject

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#e_setObject

Returns the value of attribute e_set.



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

def e_set
  @e_set
end

#finishedObject

Returns the value of attribute finished.



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

def finished
  @finished
end

#guidObject

Returns the value of attribute guid.



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

def guid
  @guid
end

#node_countObject

Returns the value of attribute node_count.



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

def node_count
  @node_count
end

#root_nodeObject

Returns the value of attribute root_node.



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

def root_node
  @root_node
end

#start_timeObject

Returns the value of attribute start_time.



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

def start_time
  @start_time
end

#thresholdObject

Returns the value of attribute threshold.



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

def threshold
  @threshold
end

Instance Method Details

#add_errors(errors) ⇒ Object



115
116
117
118
119
120
121
122
# File 'lib/ting_yun/agent/transaction/trace.rb', line 115

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



124
125
126
127
128
129
# File 'lib/ting_yun/agent/transaction/trace.rb', line 124

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



73
74
75
76
77
78
79
80
81
# File 'lib/ting_yun/agent/transaction/trace.rb', line 73

def collect_explain_plans!
  return unless TingYun::Agent::Database.should_action_collect_explain_plans?
  threshold = TingYun::Agent.config[:'nbs.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



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

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

#custom_paramsObject



99
100
101
102
103
104
105
106
107
108
# File 'lib/ting_yun/agent/transaction/trace.rb', line 99

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



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

def duration
  root_node.duration
end

#prepare_sql_for_transmission!(&block) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/ting_yun/agent/transaction/trace.rb', line 83

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



58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ting_yun/agent/transaction/trace.rb', line 58

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



110
111
112
113
# File 'lib/ting_yun/agent/transaction/trace.rb', line 110

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

#to_collector_array(encoder) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ting_yun/agent/transaction/trace.rb', line 46

def to_collector_array(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



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

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