Module: NewRelic::Agent::Database

Extended by:
Database
Included in:
Database
Defined in:
lib/new_relic/agent/database.rb,
lib/new_relic/agent/database/obfuscator.rb,
lib/new_relic/agent/database/obfuscation_helpers.rb,
lib/new_relic/agent/database/explain_plan_helpers.rb,
lib/new_relic/agent/database/postgres_explain_obfuscator.rb

Defined Under Namespace

Modules: ExplainPlanHelpers, ObfuscationHelpers, PostgresExplainObfuscator Classes: ConnectionManager, Obfuscator, Statement

Constant Summary collapse

MAX_QUERY_LENGTH =
16384
ELLIPSIS =
'...'.freeze
RECORD_FOR =
[:raw, :obfuscated].freeze
KNOWN_OPERATIONS =
%w[
  alter
  select
  update
  delete
  insert
  create
  show
  set
  exec
  execute
  call
]
OTHER_OPERATION =
'other'.freeze
SQL_COMMENT_REGEX =
Regexp.new('/\*.*?\*/', Regexp::MULTILINE).freeze

Instance Method Summary collapse

Instance Method Details

#capture_query(query) ⇒ Object

Properly encode, truncate, and dup the incoming query. Take care not to the dup the query more than once as correctly encoded may also dup the query.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/new_relic/agent/database.rb', line 20

def capture_query(query)
  return unless query

  id = query.object_id
  query = Helper.correctly_encoded(truncate_query(query))
  if query.object_id == id
    query.dup
  else
    query
  end
end

#close_connectionsObject



93
94
95
# File 'lib/new_relic/agent/database.rb', line 93

def close_connections
  ConnectionManager.instance.close_connections
end

#explain_sql(statement) ⇒ Object

Perform this in the runtime environment of a managed application, to explain the sql statement executed within a node of a transaction sample. Returns an array of two arrays. The first array contains the headers, while the second consists of arrays of strings for each column returned by 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



105
106
107
108
109
110
# File 'lib/new_relic/agent/database.rb', line 105

def explain_sql(statement)
  return nil unless statement.sql && statement.explainer && statement.config

  statement.sql = statement.sql.split(";\n")[0] # only explain the first
  return statement.explain || []
end

#get_connection(config, &connector) ⇒ Object



89
90
91
# File 'lib/new_relic/agent/database.rb', line 89

def get_connection(config, &connector)
  ConnectionManager.instance.get_connection(config, &connector)
end

#obfuscate_sql(sql) ⇒ Object



42
43
44
# File 'lib/new_relic/agent/database.rb', line 42

def obfuscate_sql(sql)
  Obfuscator.instance.obfuscator.call(sql)
end

#parse_operation_from_query(sql) ⇒ Object



128
129
130
131
132
133
134
# File 'lib/new_relic/agent/database.rb', line 128

def parse_operation_from_query(sql)
  sql = Helper.correctly_encoded(sql).gsub(SQL_COMMENT_REGEX, NewRelic::EMPTY_STR)
  return unless sql =~ /(\w+)/

  op = Regexp.last_match(1).downcase
  KNOWN_OPERATIONS.include?(op) ? op : OTHER_OPERATION
end

#record_sql_method(config_section = :transaction_tracer) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/new_relic/agent/database.rb', line 50

def record_sql_method(config_section = :transaction_tracer)
  key = record_sql_method_key(config_section)

  case Agent.config[key].to_s
  when 'off'
    :off
  when 'none'
    :off
  when 'false'
    :off
  when 'raw'
    :raw
  else
    :obfuscated
  end
end

#record_sql_method_key(config_section) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/new_relic/agent/database.rb', line 67

def record_sql_method_key(config_section)
  case config_section
  when :transaction_tracer
    :'transaction_tracer.record_sql'
  when :slow_sql
    :'slow_sql.record_sql'
  else
    "#{config_section}.record_sql".to_sym
  end
end

#set_sql_obfuscator(type, &block) ⇒ Object



46
47
48
# File 'lib/new_relic/agent/database.rb', line 46

def set_sql_obfuscator(type, &block)
  Obfuscator.instance.set_sql_obfuscator(type, &block)
end

#should_collect_explain_plans?(config_section = :transaction_tracer) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
# File 'lib/new_relic/agent/database.rb', line 84

def should_collect_explain_plans?(config_section = :transaction_tracer)
  should_record_sql?(config_section) &&
    Agent.config["#{config_section}.explain_enabled".to_sym]
end

#should_record_sql?(config_section = :transaction_tracer) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/new_relic/agent/database.rb', line 80

def should_record_sql?(config_section = :transaction_tracer)
  RECORD_FOR.include?(record_sql_method(config_section))
end

#truncate_query(query) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/new_relic/agent/database.rb', line 32

def truncate_query(query)
  return unless query

  if query.length > (MAX_QUERY_LENGTH - 4)
    query[0..MAX_QUERY_LENGTH - 4] << ELLIPSIS
  else
    query
  end
end