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_connectionsObject



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] # only explain the first
  explain_sql = explain_statement(statement, connection_config)
  return explain_sql || []
end

#explain_statement(statement, config) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/new_relic/agent/database.rb', line 69

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



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_explainObject



111
112
113
114
115
116
117
118
119
120
121
# File 'lib/new_relic/agent/database.rb', line 111

def handle_exception_in_explain
  yield
rescue => 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
    # double exception. throw up your hands
  end
end

#is_select?(statement) ⇒ Boolean

Returns:

  • (Boolean)


123
124
125
126
127
128
# File 'lib/new_relic/agent/database.rb', line 123

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/new_relic/agent/database.rb', line 82

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

#record_sql_methodObject



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