Module: NewRelic::Agent::Database::PostgresExplainObfuscator

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

Constant Summary collapse

QUOTED_STRINGS_REGEX =

Note that this regex can’t be shared with the ones in the Database::Obfuscator class because here we don’t look for backslash-escaped strings.

/'(?:[^']|'')*'|"(?:[^"]|"")*"/
LABEL_LINE_REGEX =
/^([^:\n]*:\s+).*$/.freeze

Instance Method Summary collapse

Instance Method Details

#obfuscate(explain) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/new_relic/agent/database/postgres_explain_obfuscator.rb', line 17

def obfuscate(explain)
  # First, we replace all single-quoted strings.
  # This is necessary in order to deal with multi-line string constants
  # embedded in the explain output.
  #
  # Note that we look for both single or double quotes but do not
  # replace double quotes in order to avoid accidentally latching onto a
  # single quote character embedded within a quoted identifier (such as
  # a table name).
  #
  # Note also that we make no special provisions for backslash-escaped
  # single quotes (\') because these are canonicalized to two single
  # quotes ('') in the explain output.
  explain.gsub!(QUOTED_STRINGS_REGEX) do |match|
    match.start_with?('"') ? match : '?'
  end

  # Now, mask anything after the first colon (:).
  # All parts of the query that can appear in the explain output are
  # prefixed with "<label>: ", so we want to preserve the label, but
  # remove the rest of the line.
  explain.gsub!(LABEL_LINE_REGEX, '\1?')
  explain
end