Sha256: df37279429ce03b30e474819b85dea83cd5aeff9754bf5353841188ed7f57f4a

Contents?: true

Size: 1.73 KB

Versions: 32

Compression:

Stored size: 1.73 KB

Contents

# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

module NewRelic
  module Agent
    module Database
      module PostgresExplainObfuscator
        extend self

        # 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.
        QUOTED_STRINGS_REGEX = /'(?:[^']|'')*'|"(?:[^"]|"")*"/
        LABEL_LINE_REGEX = /^([^:\n]*:\s+).*$/.freeze

        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
      end
    end
  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
newrelic_rpm-9.16.0 lib/new_relic/agent/database/postgres_explain_obfuscator.rb
newrelic_rpm-9.15.0 lib/new_relic/agent/database/postgres_explain_obfuscator.rb
newrelic_rpm-9.14.0 lib/new_relic/agent/database/postgres_explain_obfuscator.rb
newrelic_rpm-9.13.0 lib/new_relic/agent/database/postgres_explain_obfuscator.rb
newrelic_rpm-9.12.0 lib/new_relic/agent/database/postgres_explain_obfuscator.rb
newrelic_rpm-9.11.0 lib/new_relic/agent/database/postgres_explain_obfuscator.rb
newrelic_rpm-9.10.2 lib/new_relic/agent/database/postgres_explain_obfuscator.rb
newrelic_rpm-9.10.1 lib/new_relic/agent/database/postgres_explain_obfuscator.rb
newrelic_rpm-9.10.0 lib/new_relic/agent/database/postgres_explain_obfuscator.rb
newrelic_rpm-9.9.0 lib/new_relic/agent/database/postgres_explain_obfuscator.rb
newrelic_rpm-9.8.0 lib/new_relic/agent/database/postgres_explain_obfuscator.rb
newrelic_rpm-9.7.1 lib/new_relic/agent/database/postgres_explain_obfuscator.rb
newrelic_rpm-9.7.0 lib/new_relic/agent/database/postgres_explain_obfuscator.rb
newrelic_rpm-9.6.0 lib/new_relic/agent/database/postgres_explain_obfuscator.rb
newrelic_rpm-9.5.0 lib/new_relic/agent/database/postgres_explain_obfuscator.rb
newrelic_rpm-9.4.2 lib/new_relic/agent/database/postgres_explain_obfuscator.rb
newrelic_rpm-9.4.1 lib/new_relic/agent/database/postgres_explain_obfuscator.rb
newrelic_rpm-9.4.0 lib/new_relic/agent/database/postgres_explain_obfuscator.rb
newrelic_rpm-9.3.1 lib/new_relic/agent/database/postgres_explain_obfuscator.rb
newrelic_rpm-9.3.0 lib/new_relic/agent/database/postgres_explain_obfuscator.rb