Sha256: 4bfe5bae61e18f50eb1ce722bd86ab2188b9a6ebb0c7f8326f60544e0a13e5bb

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

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)
          lsevent = logstash_event(event)
          debug(lsevent.to_json + "\n") if logger && lsevent
        end
        alias_method :sql, :identity

        private

        def logstash_event(event)
          data = event.payload

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

          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

1 entries across 1 versions & 1 rubygems

Version Path
sapience-1.0.15 lib/sapience/extensions/active_record/log_subscriber.rb