Sha256: b0fbcdb0588ef9014b676b399f74fe681eaa45c2e9f57255cd3fa550dcf67c05

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 KB

Contents

# frozen_string_literal: true

require 'active_support/notifications'
require 'active_record/log_subscriber'
require 'logstasher/custom_fields'

module LogStasher
  module ActiveRecord
    class LogSubscriber < ::ActiveRecord::LogSubscriber
      include CustomFields::LogSubscriber

      def identity(event)
        lsevent = logstash_event(event)
        logger << "#{lsevent.to_json}\n" if logger && lsevent
      end
      alias sql identity

      def logger
        LogStasher.logger
      end

      private

      def logstash_event(event)
        self.class.runtime += event.duration
        data = event.payload.dup

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

        # A connection cannot be converted to JSON as it fails with
        # SystemStackError when running against ActiveSupport JSON patches.
        data.delete(:connection)

        data.merge! runtimes(event)
        data.merge! extract_sql(data)
        data.merge! request_context
        data.merge! LogStasher.store
        data.merge! extract_custom_fields(data)

        tags = ['request']
        tags.push('exception') if data[:exception]
        LogStasher.build_logstash_event(data, tags)
      end

      def request_context
        LogStasher.request_context
      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
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
logstasher-2.1.5 lib/logstasher/active_record/log_subscriber.rb
logstasher-2.1.3 lib/logstasher/active_record/log_subscriber.rb
logstasher-2.1.2 lib/logstasher/active_record/log_subscriber.rb