Sha256: f42f3c77d1e2ccd9402afc81d131ba75a939ec160ed41a7f9cabcc8427d7c1e0

Contents?: true

Size: 1.09 KB

Versions: 16

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true
require "active_support/notifications"
require "active_record/log_subscriber"

module Sapience
  module Extensions
    module ActiveRecord
      class LogSubscriber < ::ActiveRecord::LogSubscriber
        include Sapience::Loggable

        def identity(event)
          event = normalize(event)
          debug(event) if logger && event
        end
        alias sql identity

        private

        def normalize(event)
          data = event.payload

          return if data[:name] == "SCHEMA"

          data.merge! runtimes(event)
          data.merge! extract_sql(data)

          data.merge! tags(data)
        end

        def runtimes(event)
          if event.duration
            { duration: event.duration.to_f.round(2) }
          else
            {}
          end
        end

        def extract_sql(data)
          { sql: data[:sql].squeeze(" ") }
        end

        def tags(data)
          tags = Sapience.tags.dup
          tags.push("request")
          tags.push("exception") if data[:exception]
          { tags: tags }
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
sapience-3.0 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-2.15 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-2.14 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-2.13 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-2.12 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-2.11 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-2.10 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-2.9.1 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-2.8 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-2.7.0 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-2.6.1 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-2.6.0 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-2.5.4 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-2.5.3 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-2.5.2 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-2.5.0 lib/sapience/extensions/active_record/log_subscriber.rb