module NewRelic::Agent::Database

Constants

ELLIPSIS
KNOWN_OPERATIONS
MAX_QUERY_LENGTH
OTHER_OPERATION
RECORD_FOR
SQL_COMMENT_REGEX

Public Instance Methods

capture_query(query) click to toggle source

Properly encode, truncate, and dup the incoming query. Take care not to the dup the query more than once as correctly encoded may also dup the query.

# File lib/new_relic/agent/database.rb, line 20
def capture_query(query)
  return unless query

  id = query.object_id
  query = Helper.correctly_encoded(truncate_query(query))
  if query.object_id == id
    query.dup
  else
    query
  end
end
close_connections() click to toggle source
# File lib/new_relic/agent/database.rb, line 93
def close_connections
  ConnectionManager.instance.close_connections
end
explain_sql(statement) click to toggle source

Perform this in the runtime environment of a managed application, to explain the sql statement executed within a node of a transaction sample. Returns an array of two arrays. The first array contains the headers, while the second consists of arrays of strings for each column returned by the explain query. Note this happens only for statements whose execution time exceeds a threshold (e.g. 500ms) and only within the slowest transaction in a report period, selected for shipment to New Relic

# File lib/new_relic/agent/database.rb, line 105
def explain_sql(statement)
  return nil unless statement.sql && statement.explainer && statement.config

  statement.sql = statement.sql.split(";\n")[0] # only explain the first
  return statement.explain || []
end
get_connection(config, &connector) click to toggle source
# File lib/new_relic/agent/database.rb, line 89
def get_connection(config, &connector)
  ConnectionManager.instance.get_connection(config, &connector)
end
obfuscate_sql(sql) click to toggle source
# File lib/new_relic/agent/database.rb, line 42
def obfuscate_sql(sql)
  Obfuscator.instance.obfuscator.call(sql)
end
parse_operation_from_query(sql) click to toggle source
# File lib/new_relic/agent/database.rb, line 128
def parse_operation_from_query(sql)
  sql = Helper.correctly_encoded(sql).gsub(SQL_COMMENT_REGEX, NewRelic::EMPTY_STR)
  return unless sql =~ /(\w+)/

  op = Regexp.last_match(1).downcase
  KNOWN_OPERATIONS.include?(op) ? op : OTHER_OPERATION
end
record_sql_method(config_section = :transaction_tracer) click to toggle source
# File lib/new_relic/agent/database.rb, line 50
def record_sql_method(config_section = :transaction_tracer)
  key = record_sql_method_key(config_section)

  case Agent.config[key].to_s
  when 'off'
    :off
  when 'none'
    :off
  when 'false'
    :off
  when 'raw'
    :raw
  else
    :obfuscated
  end
end
record_sql_method_key(config_section) click to toggle source
# File lib/new_relic/agent/database.rb, line 67
def record_sql_method_key(config_section)
  case config_section
  when :transaction_tracer
    :'transaction_tracer.record_sql'
  when :slow_sql
    :'slow_sql.record_sql'
  else
    "#{config_section}.record_sql".to_sym
  end
end
set_sql_obfuscator(type, &block) click to toggle source
# File lib/new_relic/agent/database.rb, line 46
def set_sql_obfuscator(type, &block)
  Obfuscator.instance.set_sql_obfuscator(type, &block)
end
should_collect_explain_plans?(config_section = :transaction_tracer) click to toggle source
# File lib/new_relic/agent/database.rb, line 84
def should_collect_explain_plans?(config_section = :transaction_tracer)
  should_record_sql?(config_section) &&
    Agent.config["#{config_section}.explain_enabled".to_sym]
end
should_record_sql?(config_section = :transaction_tracer) click to toggle source
# File lib/new_relic/agent/database.rb, line 80
def should_record_sql?(config_section = :transaction_tracer)
  RECORD_FOR.include?(record_sql_method(config_section))
end
truncate_query(query) click to toggle source
# File lib/new_relic/agent/database.rb, line 32
def truncate_query(query)
  return unless query

  if query.length > (MAX_QUERY_LENGTH - 4)
    query[0..MAX_QUERY_LENGTH - 4] << ELLIPSIS
  else
    query
  end
end