Class: OneApm::Collector::SqlTrace

Inherits:
Metrics::Stats show all
Includes:
OneApm::Coerce
Defined in:
lib/one_apm/collector/containers/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 OneApm::Coerce

#event_params, #float, #int, #int_or_nil, #log_failure, #string

Methods inherited from Metrics::Stats

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

Constructor Details

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

Returns a new instance of SqlTrace.



261
262
263
264
265
266
267
# File 'lib/one_apm/collector/containers/sql_sampler.rb', line 261

def initialize(normalized_query, slow_sql, path, uri)
  super()
  @params = {}
  @sql_id = consistent_hash(normalized_query)
  set_primary slow_sql, path, uri
  record_data_point(float(slow_sql.duration))
end

Instance Attribute Details

#database_metric_nameObject (readonly)

Returns the value of attribute database_metric_name.



257
258
259
# File 'lib/one_apm/collector/containers/sql_sampler.rb', line 257

def database_metric_name
  @database_metric_name
end

#paramsObject (readonly)

Returns the value of attribute params.



258
259
260
# File 'lib/one_apm/collector/containers/sql_sampler.rb', line 258

def params
  @params
end

#pathObject (readonly)

Returns the value of attribute path.



253
254
255
# File 'lib/one_apm/collector/containers/sql_sampler.rb', line 253

def path
  @path
end

#slow_sqlObject (readonly)

Returns the value of attribute slow_sql.



259
260
261
# File 'lib/one_apm/collector/containers/sql_sampler.rb', line 259

def slow_sql
  @slow_sql
end

#sqlObject (readonly)

Returns the value of attribute sql.



256
257
258
# File 'lib/one_apm/collector/containers/sql_sampler.rb', line 256

def sql
  @sql
end

#sql_idObject (readonly)

Returns the value of attribute sql_id.



255
256
257
# File 'lib/one_apm/collector/containers/sql_sampler.rb', line 255

def sql_id
  @sql_id
end

#urlObject (readonly)

Returns the value of attribute url.



254
255
256
# File 'lib/one_apm/collector/containers/sql_sampler.rb', line 254

def url
  @url
end

Instance Method Details

#aggregate(slow_sql, path, uri) ⇒ Object



278
279
280
281
282
283
284
# File 'lib/one_apm/collector/containers/sql_sampler.rb', line 278

def aggregate(slow_sql, path, uri)
  if slow_sql.duration > max_call_time
    set_primary slow_sql, path, uri
  end

  record_data_point(float(slow_sql.duration))
end

#aggregate_trace(trace) ⇒ Object



286
287
288
# File 'lib/one_apm/collector/containers/sql_sampler.rb', line 286

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

#need_to_explain?Boolean

Returns:

  • (Boolean)


300
301
302
# File 'lib/one_apm/collector/containers/sql_sampler.rb', line 300

def need_to_explain?
  OneApm::Manager.config[:'slow_sql.explain_enabled']
end

#need_to_obfuscate?Boolean

Returns:

  • (Boolean)


296
297
298
# File 'lib/one_apm/collector/containers/sql_sampler.rb', line 296

def need_to_obfuscate?
  OneApm::Manager.config[:'slow_sql.record_sql'].to_s == 'obfuscated'
end

#prepare_to_sendObject



290
291
292
293
294
# File 'lib/one_apm/collector/containers/sql_sampler.rb', line 290

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

#set_primary(slow_sql, path, uri) ⇒ Object



269
270
271
272
273
274
275
276
# File 'lib/one_apm/collector/containers/sql_sampler.rb', line 269

def set_primary(slow_sql, path, uri)
  @slow_sql = slow_sql
  @sql = slow_sql.sql
  @database_metric_name = slow_sql.metric_name
  @path = path
  @url = uri
  @params[:backtrace] = slow_sql.backtrace if slow_sql.backtrace
end

#to_collector_array(encoder) ⇒ Object



306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/one_apm/collector/containers/sql_sampler.rb', line 306

def to_collector_array(encoder)
  [ string(@path),
    string(@url),
    int(@sql_id),
    string(@sql),
    string(@database_metric_name),
    int(@call_count),
    Helper.time_to_millis(@total_call_time),
    Helper.time_to_millis(@min_call_time),
    Helper.time_to_millis(@max_call_time),
    encoder.encode(@params) ]
end