Sha256: a8af22ffe4022631ae5e6fbc48e4b1376509f2fc6efcbf7ed33f512cb10e0635

Contents?: true

Size: 1.81 KB

Versions: 2

Compression:

Stored size: 1.81 KB

Contents

module Hickory
  module LogSubscribers
    class ActionController < ActiveSupport::LogSubscriber
      def process_action(event)
        return if Hickory::Rails.ignore_if.present? && Hickory::Rails.ignore_if.call(event)

        payload = event.payload
        default_keys = %i[method format controller action user_id request_id]

        metadata = payload.slice(*default_keys).tap { |data|
          data.merge!(uri(payload))
          data.merge!(runtimes(event, payload))
          data.merge!(status(payload))
          data.merge!(params(payload))
        }

        logger.info("canonical-request #{payload[:method]} #{payload[:path]}", metadata)
      end

      private

      def uri(payload)
        return {} unless payload.key?(:path)

        _uri = URI.parse(payload[:path])

        { path: _uri.path }.tap { |data|
          data[:query] = _uri.query unless _uri.query.nil?
        }
      end

      def runtimes(event, payload)
        { duration: event.duration.to_f.round(2) }.tap { |data|
          data[:view] = payload[:view_runtime].to_f.round(2) if payload.key?(:view_runtime)
          data[:db]  = payload[:db_runtime].to_f.round(2) if payload.key?(:db_runtime)
        }
      end

      def status(payload)
        if status = payload[:status]
          { status: status.to_i }
        elsif error = payload[:exception]
          exception, message = error
          { status: get_error_status_code(exception), error: "#{exception}: #{message}" }
        else
          { status: 0 }
        end
      end

      def params(payload)
        {
          params: payload[:params].except(:controller, :action, :format)
        }
      end

      def get_error_status_code(exception)
        status = ActionDispatch::ExceptionWrapper.rescue_responses[exception]
        Rack::Utils.status_code(status)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hickory-0.1.6 lib/hickory/log_subscribers/action_controller.rb
hickory-0.1.5 lib/hickory/log_subscribers/action_controller.rb