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

Inherits:
Metrics::Stats show all
Includes:
Support::Coerce
Defined in:
lib/ting_yun/agent/collector/sql_sampler.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

Methods inherited from Metrics::Stats

#==, create_from_hash, #increment_count, #is_reset?, #merge, #merge!, #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.



242
243
244
245
246
247
248
249
250
251
252
# File 'lib/ting_yun/agent/collector/sql_sampler.rb', line 242

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.



236
237
238
# File 'lib/ting_yun/agent/collector/sql_sampler.rb', line 236

def action_metric_name
  @action_metric_name
end

#paramsObject (readonly)

Returns the value of attribute params.



240
241
242
# File 'lib/ting_yun/agent/collector/sql_sampler.rb', line 240

def params
  @params
end

#slow_sqlObject (readonly)

Returns the value of attribute slow_sql.



239
240
241
# File 'lib/ting_yun/agent/collector/sql_sampler.rb', line 239

def slow_sql
  @slow_sql
end

#sqlObject (readonly)

Returns the value of attribute sql.



238
239
240
# File 'lib/ting_yun/agent/collector/sql_sampler.rb', line 238

def sql
  @sql
end

#uriObject (readonly)

Returns the value of attribute uri.



237
238
239
# File 'lib/ting_yun/agent/collector/sql_sampler.rb', line 237

def uri
  @uri
end

Instance Method Details

#aggregate(slow_sql, action_name, uri) ⇒ Object



254
255
256
257
258
259
260
261
262
# File 'lib/ting_yun/agent/collector/sql_sampler.rb', line 254

def aggregate(slow_sql, action_name, uri)
  if slow_sql.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(slow_sql.duration))
end

#aggregate_trace(trace) ⇒ Object



264
265
266
# File 'lib/ting_yun/agent/collector/sql_sampler.rb', line 264

def aggregate_trace(trace)
  aggregate(trace.slow_sql, trace.path, trace.url)
end

#need_to_explain?Boolean

Returns:

  • (Boolean)


277
278
279
# File 'lib/ting_yun/agent/collector/sql_sampler.rb', line 277

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

#need_to_obfuscate?Boolean

Returns:

  • (Boolean)


273
274
275
# File 'lib/ting_yun/agent/collector/sql_sampler.rb', line 273

def need_to_obfuscate?
  Agent.config[:'nbs.action_tracer.record_sql'].to_s == 'obfuscated'
end

#prepare_to_sendObject



268
269
270
271
# File 'lib/ting_yun/agent/collector/sql_sampler.rb', line 268

def prepare_to_send
  @sql = @slow_sql.sql unless need_to_obfuscate?
  @params[:explainPlan] = @slow_sql.explain if need_to_explain?
end

#to_collector_array(encoder) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/ting_yun/agent/collector/sql_sampler.rb', line 284

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