Module: NewRelic::Agent::Database
- Extended by:
- Database
- Included in:
- Database
- Defined in:
- lib/new_relic/agent/database.rb
Defined Under Namespace
Classes: ConnectionManager, Obfuscator, Statement
Instance Method Summary
collapse
Instance Method Details
#close_connections ⇒ Object
53
54
55
|
# File 'lib/new_relic/agent/database.rb', line 53
def close_connections
ConnectionManager.instance.close_connections
end
|
#explain_sql(sql, connection_config, &explainer) ⇒ Object
Perform this in the runtime environment of a managed application, to explain the sql statement executed within a segment of a transaction sample. Returns an array of explanations (which is an array rows consisting of an array of strings for each column returned by the 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
66
67
68
69
70
71
|
# File 'lib/new_relic/agent/database.rb', line 66
def explain_sql(sql, connection_config, &explainer)
return nil unless sql && connection_config
statement = sql.split(";\n")[0] explain_plan = explain_statement(statement, connection_config, &explainer)
return explain_plan || []
end
|
#explain_statement(statement, config, &explainer) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/new_relic/agent/database.rb', line 73
def explain_statement(statement, config, &explainer)
return unless is_select?(statement)
if statement[-3,3] == '...'
NewRelic::Agent.logger.debug('Unable to collect explain plan for truncated query.')
return
end
if parameterized?(statement)
NewRelic::Agent.logger.debug('Unable to collect explain plan for parameterized query.')
return
end
handle_exception_in_explain do
start = Time.now
plan = explainer.call(config, statement)
::NewRelic::Agent.record_metric("Supportability/Database/execute_explain_plan", Time.now - start)
return process_resultset(plan) if plan
end
end
|
#get_connection(config, &connector) ⇒ Object
49
50
51
|
# File 'lib/new_relic/agent/database.rb', line 49
def get_connection(config, &connector)
ConnectionManager.instance.get_connection(config, &connector)
end
|
#handle_exception_in_explain ⇒ Object
123
124
125
126
127
128
129
130
131
132
133
134
|
# File 'lib/new_relic/agent/database.rb', line 123
def handle_exception_in_explain
yield
rescue => e
begin
::NewRelic::Agent.logger.error("Error getting query plan:", e)
nil
rescue
nil
end
end
|
#is_select?(statement) ⇒ Boolean
136
137
138
139
140
141
|
# File 'lib/new_relic/agent/database.rb', line 136
def is_select?(statement)
first_word, rest_of_statement = statement.split($;, 2)
(first_word.upcase == 'SELECT')
end
|
#obfuscate_sql(sql) ⇒ Object
26
27
28
|
# File 'lib/new_relic/agent/database.rb', line 26
def obfuscate_sql(sql)
Obfuscator.instance.obfuscator.call(sql)
end
|
#parameterized?(statement) ⇒ Boolean
143
144
145
|
# File 'lib/new_relic/agent/database.rb', line 143
def parameterized?(statement)
Obfuscator.instance.obfuscate_single_quote_literals(statement) =~ /\$\d+/
end
|
#process_resultset(items) ⇒ Object
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# File 'lib/new_relic/agent/database.rb', line 94
def process_resultset(items)
= []
values = []
if items.respond_to?(:each_hash)
items.each_hash do |row|
= row.keys
values << .map{|h| row[h] }
end
elsif items.respond_to?(:each)
items.each do |row|
if row.kind_of?(Hash)
= row.keys
values << .map{|h| row[h] }
else
values << row
end
end
else
values = [items]
end
= nil if .empty?
[, values]
end
|
#record_sql_method ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'lib/new_relic/agent/database.rb', line 34
def record_sql_method
case Agent.config[:'transaction_tracer.record_sql'].to_s
when 'off'
:off
when 'none'
:off
when 'false'
:off
when 'raw'
:raw
else
:obfuscated
end
end
|
#set_sql_obfuscator(type, &block) ⇒ Object
30
31
32
|
# File 'lib/new_relic/agent/database.rb', line 30
def set_sql_obfuscator(type, &block)
Obfuscator.instance.set_sql_obfuscator(type, &block)
end
|