Sha256: 014bc47e2ad2ba4addb51ce6373dd4bd2247a9ef5320e96487da2df62039da2a

Contents?: true

Size: 933 Bytes

Versions: 3

Compression:

Stored size: 933 Bytes

Contents

# frozen_string_literal: true

require 'elastic_apm/util/lru_cache'

module ElasticAPM
  # @api private
  class SqlSummarizer
    DEFAULT = 'SQL'
    TABLE_REGEX = %{["'`]?([A-Za-z0-9_]+)["'`]?}

    REGEXES = {
      /^BEGIN/iu => 'BEGIN',
      /^COMMIT/iu => 'COMMIT',
      /^SELECT .* FROM #{TABLE_REGEX}/iu => 'SELECT FROM ',
      /^INSERT INTO #{TABLE_REGEX}/iu => 'INSERT INTO ',
      /^UPDATE #{TABLE_REGEX}/iu => 'UPDATE ',
      /^DELETE FROM #{TABLE_REGEX}/iu => 'DELETE FROM '
    }.freeze

    FORMAT = '%s%s'

    def self.cache
      @cache ||= Util::LruCache.new
    end

    def summarize(sql)
      sql = sql.encode('utf-8', invalid: :replace, undef: :replace)
      self.class.cache[sql] ||=
        REGEXES.find do |regex, sig|
          if (match = sql[0...1000].match(regex))
            break format(FORMAT, sig, match[1] && match[1].gsub(/["']/, ''))
          end
        end || DEFAULT
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
elastic-apm-3.6.0 lib/elastic_apm/sql_summarizer.rb
elastic-apm-3.5.0 lib/elastic_apm/sql_summarizer.rb
elastic-apm-3.4.0 lib/elastic_apm/sql_summarizer.rb