Class: NewRelic::Agent::SqlSampler

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

Overview

This class contains the logic of recording slow SQL traces, which may represent multiple aggregated SQL queries.

A slow SQL trace consists of a collection of SQL instrumented SQL queries that all normalize to the same text. For example, the following two queries would be aggregated together into a single slow SQL trace:

SELECT * FROM table WHERE id=42
SELECT * FROM table WHERE id=1234

Each slow SQL trace keeps track of the number of times the same normalized query was seen, the min, max, and total time spent executing those queries, and an example backtrace from one of the aggregated queries.

Instance Method Summary collapse

Instance Method Details

#notice_sql(sql, metric_name, config, duration, state = nil, explainer = nil, binds = nil, name = nil) ⇒ Object

Deprecated.

Use Datastores.notice_sql instead.

Records an SQL query, potentially creating a new slow SQL trace, or aggregating the query into an existing slow SQL trace.

This method should be used only by gem authors wishing to extend the Ruby agent to instrument new database interfaces - it should generally not be called directly from application code.

Parameters:

  • sql (String)

    the SQL query being recorded

  • metric_name (String)

    is the metric name under which this query will be recorded

  • config (Object)

    is the driver configuration for the connection

  • duration (Float)

    number of seconds the query took to execute

  • explainer (Proc) (defaults to: nil)

    for internal use only - 3rd-party clients must not pass this parameter.



142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/new_relic/agent/sql_sampler.rb', line 142

def notice_sql(sql, metric_name, config, duration, state = nil, explainer = nil, binds = nil, name = nil) # THREAD_LOCAL_ACCESS sometimes
  state ||= Tracer.state
  data = state.sql_sampler_transaction_data
  return unless data

  if state.is_sql_recorded?
    if duration > Agent.config[:'slow_sql.explain_threshold']
      backtrace = caller.join("\n")
      statement = Database::Statement.new(sql, config, explainer, binds, name)
      data.sql_data << SlowSql.new(statement, metric_name, duration, backtrace)
    end
  end
end