module NewRelic::Agent::Database
Constants
- ELLIPSIS
- KNOWN_OPERATIONS
- MAX_QUERY_LENGTH
- OTHER_OPERATION
- RECORD_FOR
- SQL_COMMENT_REGEX
Public Instance Methods
Source
# 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
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.
Source
# File lib/new_relic/agent/database.rb, line 129 def close_connections ConnectionManager.instance.close_connections end
Source
# File lib/new_relic/agent/database.rb, line 141 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
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
Source
# File lib/new_relic/agent/database.rb, line 93 def explain_this(statement, use_execute = false) if supports_with_connection? explain_this_using_with_connection(statement) else explain_this_using_adapter_connection(statement, use_execute) end rescue => e NewRelic::Agent.logger.error("Couldn't fetch the explain plan for statement: #{e}") end
Source
# File lib/new_relic/agent/database.rb, line 109 def explain_this_using_adapter_connection(statement, use_execute) connection = get_connection(statement.config) do ::ActiveRecord::Base.send(:"#{statement.config[:adapter]}_connection", statement.config) end if use_execute connection.execute("EXPLAIN #{statement.sql}") else connection.exec_query("EXPLAIN #{statement.sql}", "Explain #{statement.name}", statement.binds) end end
Source
# File lib/new_relic/agent/database.rb, line 103 def explain_this_using_with_connection(statement) ::ActiveRecord::Base.with_connection do |conn| conn.exec_query("EXPLAIN #{statement.sql}", "Explain #{statement.name}", statement.binds) end end
Source
# File lib/new_relic/agent/database.rb, line 89 def get_connection(config, &connector) ConnectionManager.instance.get_connection(config, &connector) end
Source
# File lib/new_relic/agent/database.rb, line 42 def obfuscate_sql(sql) Obfuscator.instance.obfuscator.call(sql) end
Source
# File lib/new_relic/agent/database.rb, line 164 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
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
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
Source
# File lib/new_relic/agent/database.rb, line 46 def set_sql_obfuscator(type, &block) Obfuscator.instance.set_sql_obfuscator(type, &block) end
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
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
Source
# File lib/new_relic/agent/database.rb, line 122 def supports_with_connection? return @supports_with_connection if defined?(@supports_with_connection) @supports_with_connection = defined?(::ActiveRecord::VERSION::STRING) && Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('7.2.0') end
ActiveRecord v7.2.0 introduced with_connection
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