Sha256: f879c3cc756dbe514e0227f968c1aaf2f45f2846045170debb634c83046571f9

Contents?: true

Size: 1.39 KB

Versions: 6

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

require "json"

module Skylight
  module Normalizers
    # Normalizer for SQL requests
    class SQL < Normalizer
      CAT = "db.sql.query"

      # @param trace [Skylight::Messages::Trace::Builder] ignored, only present to match API
      # @param name [String] ignored, only present to match API
      # @param payload [Hash]
      # @option payload [String] [:name] The SQL operation
      # @option payload [Hash] [:binds] The bound parameters
      # @return [Array]
      def normalize(_trace, name, payload)
        case payload[:name]
        when "SCHEMA", "CACHE"
          return :skip
        else
          name = CAT
          title = payload[:name] || "SQL"
        end

        # We can only handle UTF-8 encoded strings.
        # (Construction method here avoids extra allocations)
        sql = String.new.concat("<sk-sql>", payload[:sql], "</sk-sql>").force_encoding(Encoding::UTF_8)

        unless sql.valid_encoding?
          if config[:log_sql_parse_errors]
            config.logger.error "[#{Skylight::SqlLexError.formatted_code}] Unable to extract binds from non-UTF-8 " \
                                  "query. " \
                                  "encoding=#{payload[:sql].encoding.name} " \
                                  "sql=#{payload[:sql].inspect} "
          end

          sql = nil
        end

        [name, title, sql]
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
skylight-5.2.0 lib/skylight/normalizers/sql.rb
skylight-5.2.0.beta2 lib/skylight/normalizers/sql.rb
skylight-5.2.0.beta lib/skylight/normalizers/sql.rb
skylight-5.1.1 lib/skylight/normalizers/sql.rb
skylight-5.1.0.beta3 lib/skylight/normalizers/sql.rb
skylight-5.1.0.beta2 lib/skylight/normalizers/sql.rb