Class: NewRelic::Agent::SqlTrace

Inherits:
Stats
  • Object
show all
Includes:
Coerce
Defined in:
lib/new_relic/agent/sql_sampler.rb

Constant Summary

Constants inherited from Stats

NewRelic::Agent::Stats::SKIP_MARSHALLING

Instance Attribute Summary collapse

Attributes inherited from 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 Coerce

boolean_int!, float, float!, int, int!, int_or_nil, log_failure, scalar, string, value_or_nil

Methods inherited from Stats

#==, #hash_merge, #increment_count, #inspect_full, #is_reset?, #marshal_dump, #marshal_load, #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.



298
299
300
301
302
303
304
# File 'lib/new_relic/agent/sql_sampler.rb', line 298

def initialize(normalized_query, slow_sql, path, uri)
  super()
  @params = slow_sql.base_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)



294
295
296
# File 'lib/new_relic/agent/sql_sampler.rb', line 294

def database_metric_name
  @database_metric_name
end

#paramsObject (readonly)



295
296
297
# File 'lib/new_relic/agent/sql_sampler.rb', line 295

def params
  @params
end

#pathObject (readonly)



290
291
292
# File 'lib/new_relic/agent/sql_sampler.rb', line 290

def path
  @path
end

#slow_sqlObject (readonly)



296
297
298
# File 'lib/new_relic/agent/sql_sampler.rb', line 296

def slow_sql
  @slow_sql
end

#sqlObject (readonly)



293
294
295
# File 'lib/new_relic/agent/sql_sampler.rb', line 293

def sql
  @sql
end

#sql_idObject (readonly)



292
293
294
# File 'lib/new_relic/agent/sql_sampler.rb', line 292

def sql_id
  @sql_id
end

#urlObject (readonly)



291
292
293
# File 'lib/new_relic/agent/sql_sampler.rb', line 291

def url
  @url
end

Instance Method Details

#aggregate(slow_sql, path, uri) ⇒ Object



315
316
317
318
319
320
321
# File 'lib/new_relic/agent/sql_sampler.rb', line 315

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



323
324
325
# File 'lib/new_relic/agent/sql_sampler.rb', line 323

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

#need_to_explain?Boolean

Returns:

  • (Boolean)


337
338
339
# File 'lib/new_relic/agent/sql_sampler.rb', line 337

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

#need_to_obfuscate?Boolean

Returns:

  • (Boolean)


333
334
335
# File 'lib/new_relic/agent/sql_sampler.rb', line 333

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

#prepare_to_sendObject



327
328
329
330
331
# File 'lib/new_relic/agent/sql_sampler.rb', line 327

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



306
307
308
309
310
311
312
313
# File 'lib/new_relic/agent/sql_sampler.rb', line 306

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



343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/new_relic/agent/sql_sampler.rb', line 343

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