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
49
50
51
|
# File 'lib/new_relic/agent/database.rb', line 49
def close_connections
ConnectionManager.instance.close_connections
end
|
#explain_sql(sql, connection_config) ⇒ 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
62
63
64
65
66
67
|
# File 'lib/new_relic/agent/database.rb', line 62
def explain_sql(sql, connection_config)
return nil unless sql && connection_config
statement = sql.split(";\n")[0] explain_plan = explain_statement(statement, connection_config)
return explain_plan || []
end
|
#explain_statement(statement, config) ⇒ Object
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
# File 'lib/new_relic/agent/database.rb', line 69
def explain_statement(statement, config)
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
connection = get_connection(config)
plan = nil
if connection
plan = process_resultset(connection.execute("EXPLAIN #{statement}"))
end
return plan
end
end
|
#get_connection(config) ⇒ Object
45
46
47
|
# File 'lib/new_relic/agent/database.rb', line 45
def get_connection(config)
ConnectionManager.instance.get_connection(config)
end
|
#handle_exception_in_explain ⇒ Object
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/new_relic/agent/database.rb', line 121
def handle_exception_in_explain
yield
rescue => e
begin
::NewRelic::Agent.logger.error("Error getting query plan:", e)
rescue
end
end
|
#is_select?(statement) ⇒ Boolean
132
133
134
135
136
137
|
# File 'lib/new_relic/agent/database.rb', line 132
def is_select?(statement)
first_word, rest_of_statement = statement.split($;, 2)
(first_word.upcase == 'SELECT')
end
|
#obfuscate_sql(sql) ⇒ Object
22
23
24
|
# File 'lib/new_relic/agent/database.rb', line 22
def obfuscate_sql(sql)
Obfuscator.instance.obfuscator.call(sql)
end
|
#parameterized?(statement) ⇒ Boolean
139
140
141
|
# File 'lib/new_relic/agent/database.rb', line 139
def parameterized?(statement)
Obfuscator.instance.obfuscate_single_quote_literals(statement) =~ /\$\d+/
end
|
#process_resultset(items) ⇒ Object
92
93
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
|
# File 'lib/new_relic/agent/database.rb', line 92
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/new_relic/agent/database.rb', line 30
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
26
27
28
|
# File 'lib/new_relic/agent/database.rb', line 26
def set_sql_obfuscator(type, &block)
Obfuscator.instance.set_sql_obfuscator(type, &block)
end
|