Sha256: 63e4737ac47dba7135f3da50968e1e1e6a09339fb7bf8a8260642912c8238d23

Contents?: true

Size: 859 Bytes

Versions: 9

Compression:

Stored size: 859 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/i => 'BEGIN',
      /^COMMIT/i => 'COMMIT',
      /^SELECT .* FROM #{TABLE_REGEX}/i => 'SELECT FROM ',
      /^INSERT INTO #{TABLE_REGEX}/i => 'INSERT INTO ',
      /^UPDATE #{TABLE_REGEX}/i => 'UPDATE ',
      /^DELETE FROM #{TABLE_REGEX}/i => 'DELETE FROM '
    }.freeze

    FORMAT = '%s%s'

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

    def summarize(sql)
      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

9 entries across 9 versions & 1 rubygems

Version Path
elastic-apm-3.1.0 lib/elastic_apm/sql_summarizer.rb
elastic-apm-3.0.0 lib/elastic_apm/sql_summarizer.rb
elastic-apm-2.12.0 lib/elastic_apm/sql_summarizer.rb
elastic-apm-2.11.0 lib/elastic_apm/sql_summarizer.rb
elastic-apm-2.10.1 lib/elastic_apm/sql_summarizer.rb
elastic-apm-2.10.0 lib/elastic_apm/sql_summarizer.rb
elastic-apm-2.9.1 lib/elastic_apm/sql_summarizer.rb
elastic-apm-2.9.0 lib/elastic_apm/sql_summarizer.rb
elastic-apm-2.8.1 lib/elastic_apm/sql_summarizer.rb