module MongoInstrumentation
  # Used to extend ActionController to output additional logging information on
  # the duration of Mongo queries.
  module ControllerRuntime
    extend ActiveSupport::Concern

    protected

    def append_info_to_payload(payload)
      super
      payload[:mongo_runtime] = MongoInstrumentation::MopedSubscriber.runtime || 0
      MongoInstrumentation::MopedSubscriber.reset_runtime
    end

    module ClassMethods
      def log_process_action(payload)
        super.tap do |messages|
          runtime = payload[:mongo_runtime]
          messages << ("Mongo: %.1fms" % runtime.to_f)
        end
      end
    end
  end
end

ActiveSupport.on_load(:action_controller) do
  include MongoInstrumentation::ControllerRuntime
end

module MongoInstrumentation
  # Used to extend ActionController to output additional logging information on
  # the duration of Mongo queries.
  module ControllerRuntime
    extend ActiveSupport::Concern

    protected

    def append_info_to_payload(payload)
      super
      payload[:db_runtime] = MongoInstrumentation::MopedSubscriber.runtime || 0
      MongoInstrumentation::MopedSubscriber.reset_runtime
    end

    module ClassMethods
      def log_process_action(payload)
        super.tap do |messages|
          runtime = payload[:db_runtime]
          messages << ("Mongo: %.1fms" % runtime.to_f)
        end
      end
    end
  end
end

ActiveSupport.on_load(:action_controller) do
  include MongoInstrumentation::ControllerRuntime
end

Use DB payload key logstasher is looking for
Use DB payload key logstasher is looking for

By using the same payload key as ActiveRecord, we get the DB duration into the
logstasher output for free.

Note that this assumes that the application is only running mongoid and not
ActiveRecord as well, otherwise the recorded duration would get clobbered by one
or the other.
