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
Regexp.new('/\*.*?\*/', Regexp::MULTILINE).freeze
- QUERY_NAME_REGEX =
Regexp.new('/\*\s*NewRelicQueryName:\s*(.*?)\*/', 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_connections ⇒ Object
133
134
135
|
# File 'lib/new_relic/agent/database.rb', line 133
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
145
146
147
148
149
150
|
# File 'lib/new_relic/agent/database.rb', line 145
def explain_sql(statement)
return nil unless statement.sql && statement.explainer && statement.config
statement.sql = statement.sql.split(";\n")[0] return statement.explain || []
end
|
#explain_this(statement, use_execute = false) ⇒ Object
93
94
95
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/new_relic/agent/database.rb', line 93
def explain_this(statement, use_execute = false)
connection = get_connection(statement.config) { new_explain_connection(statement.config) }
return unless connection
run_explain(statement.config, connection) do
if use_execute connection.execute("EXPLAIN #{statement.sql}")
else
connection.exec_query("EXPLAIN #{statement.sql}", "Explain #{statement.name}", statement.binds)
end
end
rescue => e
NewRelic::Agent.logger.error("Couldn't fetch the explain plan for statement: #{e}")
end
|
Extracts an explicit query name from a SQL comment.
Looks for comments in the format: /* NewRelicQueryName: CustomName */
Returns the custom name with forward slashes replaced by pipes to avoid
metric parsing issues, or nil if no query name comment is found.
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
# File 'lib/new_relic/agent/database.rb', line 184
def (sql)
return unless sql
sql = Helper.correctly_encoded(sql)
match = sql.match(QUERY_NAME_REGEX)
return unless match
query_name = match[1].strip
return if query_name.empty?
query_name.tr('/', '|')
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
|
#new_explain_connection(config) ⇒ Object
108
109
110
111
112
113
114
115
116
|
# File 'lib/new_relic/agent/database.rb', line 108
def new_explain_connection(config)
if supports_connection_adapters_resolve?
::ActiveRecord::ConnectionAdapters.resolve(config[:adapter]).new(config)
else
::ActiveRecord::Base.send(:"#{config[:adapter]}_connection", config)
end
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
#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
|
#run_explain(config, connection) ⇒ Object
Resets the connection on error so it isn't reused while in a bad state
119
120
121
122
123
124
|
# File 'lib/new_relic/agent/database.rb', line 119
def run_explain(config, connection)
yield
rescue
ConnectionManager.instance.reset_or_discard_connection(config, connection)
raise
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
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
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_connection_adapters_resolve? ⇒ Boolean
126
127
128
129
130
131
|
# File 'lib/new_relic/agent/database.rb', line 126
def supports_connection_adapters_resolve?
return @supports_connection_adapters_resolve if defined?(@supports_connection_adapters_resolve)
@supports_connection_adapters_resolve = defined?(::ActiveRecord::VERSION::STRING) &&
NewRelic::Helper.version_satisfied?(ActiveRecord::VERSION::STRING, '>=', '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
|