Module: NewRelic::Agent::Database

Extended by:
Database
Included in:
Database
Defined in:
lib/new_relic/agent/database.rb,
lib/new_relic/agent/database/obfuscator.rb,
lib/new_relic/agent/database/obfuscation_helpers.rb,
lib/new_relic/agent/database/explain_plan_helpers.rb,
lib/new_relic/agent/database/postgres_explain_obfuscator.rb

Defined Under Namespace

Modules: ExplainPlanHelpers, ObfuscationHelpers, PostgresExplainObfuscator Classes: ConnectionManager, Obfuscator, Statement

Constant Summary collapse

MAX_QUERY_LENGTH =
16384
ELLIPSIS =
'...'.freeze
RECORD_FOR =
[:raw, :obfuscated].freeze
KNOWN_OPERATIONS =
%w[
  alter
  select
  update
  delete
  insert
  create
  show
  set
  exec
  execute
  call
]
OTHER_OPERATION =
'other'.freeze
SQL_COMMENT_REGEX =
Regexp.new('/\*.*?\*/', Regexp::MULTILINE).freeze

Instance Method Summary collapse

Instance Method Details

#capture_query(query) ⇒ Object

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.



20
21
22
23
24
25
26
27
28
29
30
# 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_connectionsObject



129
130
131
# File 'lib/new_relic/agent/database.rb', line 129

def close_connections
  ConnectionManager.instance.close_connections
end

#explain_sql(statement) ⇒ Object

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



141
142
143
144
145
146
# 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

#explain_this(statement, use_execute = false) ⇒ Object



93
94
95
96
97
98
99
100
101
# 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

#explain_this_using_adapter_connection(statement, use_execute) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
# 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

#explain_this_using_with_connection(statement) ⇒ Object



103
104
105
106
107
# 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

#get_connection(config, &connector) ⇒ Object



89
90
91
# File 'lib/new_relic/agent/database.rb', line 89

def get_connection(config, &connector)
  ConnectionManager.instance.get_connection(config, &connector)
end

#obfuscate_sql(sql) ⇒ Object



42
43
44
# File 'lib/new_relic/agent/database.rb', line 42

def obfuscate_sql(sql)
  Obfuscator.instance.obfuscator.call(sql)
end

#parse_operation_from_query(sql) ⇒ Object



164
165
166
167
168
169
170
# 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

#record_sql_method(config_section = :transaction_tracer) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# 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) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# 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) ⇒ Object



46
47
48
# 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) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
# 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) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# 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

#supports_with_connection?Boolean

ActiveRecord v7.2.0 introduced with_connection

Returns:

  • (Boolean)


122
123
124
125
126
127
# 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

#truncate_query(query) ⇒ Object



32
33
34
35
36
37
38
39
40
# 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