Module: NewRelic::Agent::Database

Extended by:
Database
Included in:
Database
Defined in:
lib/new_relic/agent/database.rb

Defined Under Namespace

Classes: ConnectionManager, Obfuscator

Instance Method Summary collapse

Instance Method Details

#close_connectionsObject



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] # only explain the first
  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_explainObject



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 Exception => e
  begin
    # guarantees no throw from explain_sql
    NewRelic::Control.instance.log.error("Error getting query plan: #{e.message}")
    NewRelic::Control.instance.log.debug(e.backtrace.join("\n"))
  rescue Exception
    # double exception. throw up your hands
  end
end

#is_select?(statement) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
112
113
# File 'lib/new_relic/agent/database.rb', line 108

def is_select?(statement)
  # split the string into at most two segments on the
  # system-defined field separator character
  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)
  # The resultset type varies for different drivers.  Only thing you can count on is
  # that it implements each.  Also: can't use select_rows because the native postgres
  # driver doesn't know that method.
  
  headers = []
  values = []
  if items.respond_to?(:each_hash)
    items.each_hash do |row|
      headers = row.keys
      values << headers.map{|h| row[h] }
    end
  elsif items.respond_to?(:each)
    items.each do |row|
      if row.kind_of?(Hash)
        headers = row.keys
        values << headers.map{|h| row[h] }
      else
        values << row
      end
    end
  else
    values = [items]
  end
  
  headers = nil if headers.empty?
  [headers, 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