Class: TingYun::Agent::Collector::SqlTrace

Inherits:
Metrics::Stats show all
Includes:
Support::Coerce
Defined in:
lib/ting_yun/agent/collector/sql_sampler/sql_trace.rb

Instance Attribute Summary collapse

Attributes inherited from Metrics::Stats

#call_count, #max_call_time, #min_call_time, #sum_of_squares, #total_call_time, #total_exclusive_time

Instance Method Summary collapse

Methods included from Support::Coerce

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

Methods inherited from Metrics::Stats

#==, create_from_hash, #increment_count, #is_reset?, #merge, #merge!, #metrics, #record, #record_apdex, #record_data_point, #reset, #to_json, #to_s

Constructor Details

#initialize(normalized_query, slow_sql, action_name, uri) ⇒ SqlTrace

Returns a new instance of SqlTrace.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ting_yun/agent/collector/sql_sampler/sql_trace.rb', line 19

def initialize(normalized_query, slow_sql, action_name, uri)
  super()
  @params = {}

  @action_metric_name = action_name
  @slow_sql = slow_sql
  @sql = normalized_query
  @uri = uri
  @params[:stacktrace] = slow_sql.backtrace
  record_data_point(float(slow_sql.duration))
end

Instance Attribute Details

#action_metric_nameObject (readonly)

Returns the value of attribute action_metric_name.



13
14
15
# File 'lib/ting_yun/agent/collector/sql_sampler/sql_trace.rb', line 13

def action_metric_name
  @action_metric_name
end

#paramsObject (readonly)

Returns the value of attribute params.



17
18
19
# File 'lib/ting_yun/agent/collector/sql_sampler/sql_trace.rb', line 17

def params
  @params
end

#slow_sqlObject (readonly)

Returns the value of attribute slow_sql.



16
17
18
# File 'lib/ting_yun/agent/collector/sql_sampler/sql_trace.rb', line 16

def slow_sql
  @slow_sql
end

#sqlObject (readonly)

Returns the value of attribute sql.



15
16
17
# File 'lib/ting_yun/agent/collector/sql_sampler/sql_trace.rb', line 15

def sql
  @sql
end

#uriObject (readonly)

Returns the value of attribute uri.



14
15
16
# File 'lib/ting_yun/agent/collector/sql_sampler/sql_trace.rb', line 14

def uri
  @uri
end

Instance Method Details

#aggregate(slow_sql, action_name, uri) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/ting_yun/agent/collector/sql_sampler/sql_trace.rb', line 31

def aggregate(slow_sql, action_name, uri)
  duration = slow_sql.duration
  if duration > max_call_time
    @action_metric_name = action_name
    @slow_sql = slow_sql
    @uri = uri
    @params[:stacktrace] = slow_sql.backtrace
  end
  record_data_point(float(duration))
end

#need_to_explain?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/ting_yun/agent/collector/sql_sampler/sql_trace.rb', line 49

def need_to_explain?
  Agent.config[:'nbs.action_tracer.explain_enabled'] &&  @slow_sql.duration * 1000 > TingYun::Agent.config[:'nbs.action_tracer.explain_threshold']
end

#prepare_to_sendObject



43
44
45
46
# File 'lib/ting_yun/agent/collector/sql_sampler/sql_trace.rb', line 43

def prepare_to_send
  @sql = @slow_sql.sql unless Agent.config[:'nbs.action_tracer.record_sql'].to_s == 'obfuscated'
  @params[:explainPlan] = @slow_sql.explain if need_to_explain?
end

#to_collector_array(encoder) ⇒ Object



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

def to_collector_array(encoder)
  [
      @slow_sql.start_time,
      string(@action_metric_name),
      string(@slow_sql.metric_name),
      string(@uri||@action_metric_name),
      string(@sql),
      int(@call_count),
      TingYun::Helper.time_to_millis(@total_call_time),
      TingYun::Helper.time_to_millis(@max_call_time),
      TingYun::Helper.time_to_millis(@min_call_time),
      encoder.encode(@params)
  ]
end