lib/mongo/protocol/msg.rb in mongo-2.8.0 vs lib/mongo/protocol/msg.rb in mongo-2.9.0.rc0
- old
+ new
@@ -29,10 +29,16 @@
# The identifier for the database name to execute the command on.
#
# @since 2.5.0
DATABASE_IDENTIFIER = '$db'.freeze
+ # Keys that the driver adds to commands. These are going to be
+ # moved to the end of the hash for better logging.
+ #
+ # @api private
+ INTERNAL_KEYS = Set.new(%w($clusterTime lsid signature txnNumber)).freeze
+
# Creates a new OP_MSG protocol message
#
# @example Create a OP_MSG wire protocol message
# Msg.new([:more_to_come], {}, { ismaster: 1 },
# { type: 1, payload: { identifier: 'documents', sequence: [..] } })
@@ -80,13 +86,30 @@
#
# @return [ BSON::Document ] The event payload.
#
# @since 2.5.0
def payload
+ # Reorder keys in global_args for better logging - see
+ # https://jira.mongodb.org/browse/RUBY-1591.
+ # Note that even without the reordering, the payload is not an exact
+ # match to what is sent over the wire because the command as used in
+ # the published eent combines keys from multiple sections of the
+ # payload sent over the wire.
+ ordered_command = {}
+ skipped_command = {}
+ command.each do |k, v|
+ if INTERNAL_KEYS.member?(k.to_s)
+ skipped_command[k] = v
+ else
+ ordered_command[k] = v
+ end
+ end
+ ordered_command.update(skipped_command)
+
BSON::Document.new(
- command_name: command.keys.first.to_s,
+ command_name: ordered_command.keys.first.to_s,
database_name: global_args[DATABASE_IDENTIFIER],
- command: command,
+ command: ordered_command,
request_id: request_id,
reply: sections[0]
)
end