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
34
35
36
|
# File 'lib/new_relic/agent/database.rb', line 34
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
47
48
49
50
51
52
|
# File 'lib/new_relic/agent/database.rb', line 47
def explain_sql(sql, connection_config)
return nil unless sql && connection_config
statement = sql.split(";\n")[0] explain_sql = explain_statement(statement, connection_config)
return explain_sql || []
end
|
#explain_statement(statement, config) ⇒ Object
54
55
56
57
58
59
60
61
62
63
64
65
|
# File 'lib/new_relic/agent/database.rb', line 54
def explain_statement(statement, config)
if is_select?(statement)
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
end
|
#get_connection(config) ⇒ Object
30
31
32
|
# File 'lib/new_relic/agent/database.rb', line 30
def get_connection(config)
ConnectionManager.instance.get_connection(config)
end
|
#handle_exception_in_explain ⇒ Object
96
97
98
99
100
101
102
103
104
105
106
|
# File 'lib/new_relic/agent/database.rb', line 96
def handle_exception_in_explain
yield
rescue => e
begin
NewRelic::Control.instance.log.error("Error getting query plan: #{e.message}")
NewRelic::Control.instance.log.debug(e.backtrace.join("\n"))
rescue
end
end
|
#is_select?(statement) ⇒ Boolean
108
109
110
111
112
113
|
# File 'lib/new_relic/agent/database.rb', line 108
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
|
#process_resultset(items) ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
# File 'lib/new_relic/agent/database.rb', line 67
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
|
#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
|