Sha256: 75e0c0f4ef59136147c0b5bc3e704a2712a578fdb9acce7a2d3b743e39730622

Contents?: true

Size: 1.5 KB

Versions: 16

Compression:

Stored size: 1.5 KB

Contents

# (c) Copyright IBM Corp. 2021
# (c) Copyright Instana Inc. 2021

module Instana
  module Instrumentation
    module ActiveRecord
      IGNORED_NAMES = %w[SCHEMA EXPLAIN CACHE].freeze
      IGNORED_SQL = %w[BEGIN COMMIT SET].freeze
      SANITIZE_REGEXP = /('[\s\S][^']*'|\d*\.\d+|\d+|NULL)/i.freeze

      def log(sql, name = 'SQL', binds = [], *args, **kwargs, &block)
        call_payload = {
          activerecord: {
            adapter: @config[:adapter],
            host: @config[:host],
            username: @config[:username],
            db: @config[:database],
            sql: maybe_sanitize(sql)
          }
        }

        if binds.all? { |b| b.respond_to?(:value_before_type_cast) } && !::Instana.config[:sanitize_sql]
          mapped = binds.map(&:value_before_type_cast)
          call_payload[:activerecord][:binds] = mapped
        end

        maybe_trace(call_payload, name) { super(sql, name, binds, *args, **kwargs, &block) }
      end

      private

      def maybe_sanitize(sql)
        ::Instana.config[:sanitize_sql] ? sql.gsub(SANITIZE_REGEXP, '?') : sql
      end

      def maybe_trace(call_payload, name, &blk)
        if ::Instana.tracer.tracing? && !ignored?(call_payload, name)
          ::Instana.tracer.trace(:activerecord, call_payload, &blk)
        else
          yield
        end
      end

      def ignored?(call_payload, name)
        IGNORED_NAMES.include?(name) ||
          IGNORED_SQL.any? { |s| call_payload[:activerecord][:sql].upcase.start_with?(s) }
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
instana-1.216.0 lib/instana/instrumentation/active_record.rb
instana-1.215.1 lib/instana/instrumentation/active_record.rb
instana-1.215.0 lib/instana/instrumentation/active_record.rb
instana-1.214.4 lib/instana/instrumentation/active_record.rb
instana-1.214.3 lib/instana/instrumentation/active_record.rb
instana-1.214.2 lib/instana/instrumentation/active_record.rb
instana-1.214.1 lib/instana/instrumentation/active_record.rb
instana-1.214.0 lib/instana/instrumentation/active_record.rb
instana-1.213.3 lib/instana/instrumentation/active_record.rb
instana-1.213.2 lib/instana/instrumentation/active_record.rb
instana-1.213.1 lib/instana/instrumentation/active_record.rb
instana-1.213.0 lib/instana/instrumentation/active_record.rb
instana-1.212.0 lib/instana/instrumentation/active_record.rb
instana-1.211.0 lib/instana/instrumentation/active_record.rb
instana-1.210.1 lib/instana/instrumentation/active_record.rb
instana-1.210.0 lib/instana/instrumentation/active_record.rb