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

Constructor Details

#initialize(start_time) ⇒ Trace

Returns a new instance of Trace.



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

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")
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#finishedObject

Returns the value of attribute finished.



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

def finished
  @finished
end

#guidObject

Returns the value of attribute guid.



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

def guid
  @guid
end

#node_countObject

Returns the value of attribute node_count.



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

def node_count
  @node_count
end

#root_nodeObject

Returns the value of attribute root_node.



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

def root_node
  @root_node
end

#start_timeObject

Returns the value of attribute start_time.



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

def start_time
  @start_time
end

#thresholdObject

Returns the value of attribute threshold.



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

def threshold
  @threshold
end

Instance Method Details

#collect_explain_plans!Object



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

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



21
22
23
24
# File 'lib/ting_yun/agent/transaction/trace.rb', line 21

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



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

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



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

def duration
  root_node.duration
end

#prepare_sql_for_transmission!(&block) ⇒ Object



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

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



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

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



108
109
110
111
# File 'lib/ting_yun/agent/transaction/trace.rb', line 108

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

#to_collector_array(encoder) ⇒ Object



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

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
  ]
end

#trace_treeObject



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

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