Sha256: 904d1c8fe0198616d2a7ebf812872098c507404d2bf0970852f1f3fd33cb6ce7

Contents?: true

Size: 1.13 KB

Versions: 29

Compression:

Stored size: 1.13 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)
          logger << 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)
          debug(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

29 entries across 29 versions & 1 rubygems

Version Path
sapience-1.0.14 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-1.0.13 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-1.0.12 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-1.0.11 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-1.0.10 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-1.0.9 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-1.0.8 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-1.0.7 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-1.0.6 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-1.0.5 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-1.0.4 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-1.0.3 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-1.0.2 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-1.0.1 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-1.0.0 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-0.2.18 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-0.2.17 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-0.2.16 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-0.2.15 lib/sapience/extensions/active_record/log_subscriber.rb
sapience-0.2.14 lib/sapience/extensions/active_record/log_subscriber.rb