Sha256: a87b25f0dc89acf36355d7f7761a2b92855f56e3bbad9dec9a0d89e5ade5908a

Contents?: true

Size: 1.76 KB

Versions: 18

Compression:

Stored size: 1.76 KB

Contents

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

require 'new_relic/agent/database/obfuscation_helpers'

module NewRelic
  module Agent
    module Database
      class Obfuscator
        include Singleton
        include ObfuscationHelpers

        attr_reader :obfuscator

        QUERY_TOO_LARGE_MESSAGE = "Query too large (over 16k characters) to safely obfuscate".freeze
        ELLIPSIS = "...".freeze

        def initialize
          reset
        end

        def reset
          @obfuscator = method(:default_sql_obfuscator)
        end

        # Sets the sql obfuscator used to clean up sql when sending it
        # to the server. Possible types are:
        #
        # :before => sets the block to run before the existing
        # obfuscators
        #
        # :after => sets the block to run after the existing
        # obfuscator(s)
        #
        # :replace => removes the current obfuscator and replaces it
        # with the provided block
        def set_sql_obfuscator(type, &block)
          if type == :before
            @obfuscator = NewRelic::ChainedCall.new(block, @obfuscator)
          elsif type == :after
            @obfuscator = NewRelic::ChainedCall.new(@obfuscator, block)
          elsif type == :replace
            @obfuscator = block
          else
            fail "unknown sql_obfuscator type #{type}"
          end
        end

        def default_sql_obfuscator(sql)
          stmt = sql.kind_of?(Statement) ? sql : Statement.new(sql)

          if stmt.sql.end_with? ELLIPSIS
            return QUERY_TOO_LARGE_MESSAGE
          end

          obfuscate(stmt.sql, stmt.adapter).to_s
        end
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
newrelic_rpm-8.9.0 lib/new_relic/agent/database/obfuscator.rb
newrelic_rpm-8.8.0 lib/new_relic/agent/database/obfuscator.rb
newrelic_rpm-8.7.0 lib/new_relic/agent/database/obfuscator.rb
newrelic_rpm-8.6.0 lib/new_relic/agent/database/obfuscator.rb
newrelic_rpm-8.5.0 lib/new_relic/agent/database/obfuscator.rb
newrelic_rpm-8.4.0 lib/new_relic/agent/database/obfuscator.rb
newrelic_rpm-8.3.0 lib/new_relic/agent/database/obfuscator.rb
newrelic_rpm-8.2.0 lib/new_relic/agent/database/obfuscator.rb
newrelic_rpm-8.1.0 lib/new_relic/agent/database/obfuscator.rb
newrelic_rpm-8.0.0 lib/new_relic/agent/database/obfuscator.rb
newrelic_rpm-7.2.0 lib/new_relic/agent/database/obfuscator.rb
newrelic_rpm-7.1.0 lib/new_relic/agent/database/obfuscator.rb
newrelic_rpm-7.0.0 lib/new_relic/agent/database/obfuscator.rb
newrelic_rpm-6.15.0 lib/new_relic/agent/database/obfuscator.rb
newrelic_rpm-6.14.0 lib/new_relic/agent/database/obfuscator.rb
newrelic_rpm-6.13.1 lib/new_relic/agent/database/obfuscator.rb
newrelic_rpm-6.13.0 lib/new_relic/agent/database/obfuscator.rb
newrelic_rpm-6.12.0.367 lib/new_relic/agent/database/obfuscator.rb