- Reference >
- MongoDB Package Components >
mongoreplay
mongoreplay¶
On this page
- Synopsis
- Required Access
- Options
- Commands
mongoreplayReport Format- Examples
Synopsis¶
New in version 3.4.
Availability
Available for Linux and macOS.
mongoreplay is a traffic capture and replay tool for MongoDB
that you can use to inspect and record commands sent to a MongoDB
instance, and then replay those commands back onto another host at a
later time.
mongoreplay can help you preview how your MongoDB deployment
will perform a production workload under a different environment,
such as with a different storage engine, on different hardware, or
with a different operating system configuration.
mongoreplay can also help reproduce and investigate an issue by
recording and replaying the operations that trigger the issue.
Finally, mongoreplay serves as a more flexible version of
the legacy mongosniff tool to help you investigate database activity.
Run mongoreplay from the system command line, not the mongo shell.
Required Access¶
mongoreplay requires access to the network interface that
the record or monitor commands will
listen on. You may need to run mongoreplay with root privileges
to access the network device.
mongoreplay will not work with MongoDB instances using an SSL connection.
Warning
Only use root privileges when connecting to trusted sources.
If you are using play to connect to a MongoDB deployment
that enforces access control, you must
connect as a user with the required privileges to execute the
recorded operations. Include the user’s credentials in the
--host MongoDB connection string.
Options¶
-
mongoreplay¶
-
--verbosity,-v¶ Increases the amount of internal reporting returned on standard output or in log files. Increase the verbosity with the
-vform by including the option multiple times, (e.g.-vvvvv.)
-
--debug,-d¶ Increases the amount of detail about mongoreplay operations and errors recorded in log files. Increase the debugging detail with the
-dform by including the option multiple times, (e.g.-ddd.)
-
--silent,-s¶ When set, mongoreplay does not produce any log output.
-
--help¶ Returns information on the options and use of mongoreplay.
Commands¶
mongoreplay includes the following commands to record,
play back, and monitor MongoDB network traffic.
record¶
record produces a playback file based on
network traffic. record supports collecting network
traffic directly or can
accept a pcap file
to produce the playback file.
The playback file contains a list of all requests sent to the
mongod instance during the recording as well as all
responses transmitted to the client during the recording. The playback
file also records metadata for each request, such as the connection
identifier and timestamp.
The following prototype uses mongoreplay to record data
on the loopback network interface and creates a playback file
located at ~/recordings/playback.
Similarly, the following prototype uses mongoreplay
to produce a playback file from an existing pcap file:
record supports the following options:
-
mongoreplay record¶
-
record¶
-
-f<path>¶ Specifies the path to a pcap file that
recordshould read to produce a playback file.Use
-fas an alternative to capturing network traffic using-i. You must specify either-for-i. If you include both options, mongoreplay record produces an error.
-
-b<number>¶ Size of heap used to merge separate streams together.
-
--expr<filter expression>,-e<filter expression>¶ An expression in Berkeley Packet Filter (BPF) syntax to apply to incoming traffic to record. Required if you are capturing traffic from a network interface using
-i.For example, to capture traffic from a MongoDB instance running on port 27017, you would specify
-e 'port 27017'.
-
-i<interface>¶ Specifies the network interface that
recordshould listen on to capture network traffic.Use with
-e.Use
-ias an alternative to reading an existing pcap file with-i. You must specify either-for-i. If you include both options, mongoreplay record produces an error.
-
--playback-file<path>,-p<path>¶ Specifies the path to which to write the playback file.
The produced playback file is a BSON file.
See
Use record for examples of using
mongoreplay with the record command.
play¶
play replays a playback file created with
record against a mongod instance.
The following prototype uses mongoreplay to replay the
~/recordings/playback playback file to the mongod instance running on
192.168.0.4:27018:
play supports the following options:
-
mongoreplay play¶
-
play¶
-
--collect<json|format|none>¶ Default: format
Specifies the output format for the collected statistics.
json: outputs stat information as jsonformat: uses the formatting specified in the--formatoption to produce the output file.none: does not provide any output
-
--report<path>¶ Specifies the path to which to write an execution report. Use
--collectto specify the output format for the report.If you do not specify
--report,playwrites to STDOUT.
-
--no-truncate¶ If specified, disables truncation of large reply payload data in the
playlog output.
-
--format¶ Default:
%F{blue}%t%f %F{cyan}(Connection: %o:%i)%f %F{yellow}%l%f %F{red}%T %c%f %F{white}%n%f %F{green}%Q{Request:}%f%q%F{green}%R{Response:}%f%r)Specifies the format for terminal output. You can specify arguments immediately after the format ‘verbs’ by wrapping them in curly braces, as in
%Q{<arg>}.If you specify
--format, also specifyformatas the value for the--collectoption.--formatsupports the following verbs:%n: namespace%l: latency%t: time. You may optionally specify the date layout using the Go Programming Language’s time formatting. Go usesMon Jan 2 15:04:05 MST 2006as its reference time. You must specify the time format using the reference time. Thus, if you wanted to print the date in formatyyyy-mm-dd hh:mm, you would specify%t{2006-01-02 15:04}. Refer to the Go time formatting documentation for more information.%T: op time%c: command%o: number of connections%i: request ID%q: request. You may optinally specified a dot-delimited field within the JSON structure, as in,%q{command_args.documents}.%r: response. You may optinally specified a dot-delimited field within the JSON structure, as in,%q{command_args.documents}.%Q{<arg>}: display<arg>on presence of request data%R{<arg>}: display<arg>on presence of response data
In addition,
--formatsupports the following start/end ANSI escape sequences:%B/%b: bold%U/%u: underline%S/%s: standout%F/%f: text color (required arg – word or number, 8-color)%K/%k: background color (required arg – same as %F/%f)
-
--no-colors¶ When set, removes colors from the
default format.
-
--playback-file<path>,-p<path>¶ Specifies the path from which to read the playback file.
If the playback file was created using the
--gzipoption, you must also specify--gzipwhen runningplay.
-
--speednumber¶ Default: 1.0
Specifies a multiplier to adjust playback speed.
--speed 1.0processes the playback file in real time;--speed 0.5at half speed;--speed 3.0at triple speed.The specified speed is a target speed. If mongoreplay play encounters a bottleneck, playback may be slower than the specified multiplier.
-
--host<hostname><:port>,-h<hostname><:port>¶ Default: localhost:27017
Specifies a MongoDB connection string for the MongoDB deployment to which to play back the captured network traffic.
By default,
playattempts to connect to amongodinstance running on the localhost on port number27017.
-
--repeatnumber¶ Default: 1
Specifies the number of times to play the playback file.
-
--queueTimenumber¶ Default: 15
Specifies the maximum time, in seconds, to queue operations in advance of transmitting them.
-
--no-preprocess¶ When set,
playdoes not preprocess the input file to pre-map data such as MongoDB cursor IDs.
See
Use play for examples of using
mongoreplay with the play command.
monitor¶
monitor inspects live or pre-recorded
MongoDB network traffic.
The following prototype uses mongoreplay to produce a
JSON report based on the playback.bson playback file in the ~/recordings directory:
monitor supports the following options:
-
mongoreplay monitor¶
-
monitor¶
-
--collect<json|format|none>¶ Default: format
Specifies the output format for the collected statistics.
json: outputs stat information as jsonformat: uses the formatting specified in the--formatoption to produce the output file.none: does not provide any output
-
--report<path>¶ Specifies the path to which to write an execution report. Use
--collectto specify the output format for the report.If you do not specify
--report,monitorwrites to STDOUT.
-
--no-truncate¶ If specified, disables truncation of large reply payload data in the
monitorlog output.
-
--format¶ Default:
%F{blue}%t%f %F{cyan}(Connection: %o:%i)%f %F{yellow}%l%f %F{red}%T %c%f %F{white}%n%f %F{green}%Q{Request:}%f%q%F{green}%R{Response:}%f%r)Specifies the format for terminal output. You can specify arguments immediately after the format ‘verbs’ by wrapping them in curly braces, as in
%Q{<arg>}.If you specify
--format, also specifyformatas the value for the--collectoption.--formatsupports the following verbs:%n: namespace%l: latency%t: time. You may optionally specify the date layout using the Go Programming Language’s time formatting. Go usesMon Jan 2 15:04:05 MST 2006as its reference time. You must specify the time format using the reference time. Thus, if you wanted to print the date in formatyyyy-mm-dd hh:mm, you would specify%t{2006-01-02 15:04}. Refer to the Go time formatting documentation for more information.%T: op time%c: command%o: number of connections%i: request ID%q: request. You may optinally specified a dot-delimited field within the JSON structure, as in,%q{command_args.documents}.%r: response. You may optinally specified a dot-delimited field within the JSON structure, as in,%q{command_args.documents}.%Q{<arg>}: display<arg>on presence of request data%R{<arg>}: display<arg>on presence of response data
In addition,
--formatsupports the following start/end ANSI escape sequences:%B/%b: bold%U/%u: underline%S/%s: standout%F/%f: text color (required arg – word or number, 8-color)%K/%k: background color (required arg – same as %F/%f)
-
--no-colors¶ When set, removes colors from the
default format.
-
-f<path>¶ Specifies the path to a pcap file that
monitorshould read to produce a playback file.Use
-fas an alternative to capturing network traffic using-i. You must specify either-for-i. If you include both options, mongoreplay monitor produces an error.
-
-b<number>¶ Size of heap used to merge separate streams together.
-
--expr<filter expression>,-e<filter expression>¶ An expression in Berkeley Packet Filter (BPF) syntax to apply to incoming traffic to record. Required if you are capturing traffic from a network interface using
-i.For example, to capture traffic from a MongoDB instance running on port 27017, you would specify
-e 'port 27017'.
-
-i<interface>¶ Specifies the network interface that
monitorshould listen on to capture network traffic.Use with
-e.Use
-ias an alternative to reading an existing pcap file with-i. You must specify either-for-i. If you include both options, mongoreplay monitor produces an error.
-
--playback-file<path>,-p<path>¶ Specifies the path from which to read the playback file.
See
Use monitor for examples of using
mongoreplay with the monitor command.
mongoreplay Report Format¶
monitor and play can produce
reports based on a playback file when run with the --report option.
Fields¶
mongoreplay reports can include the following fields:
-
order A monotonically increasing key indicating the order in which the operations were recorded and played back. This can be used to reconstruct the ordering of the series of operations executed on a connection, since the order in which they appear in the report file may not match the playback order.
-
op The type of operation represented by the request: i.e. “query”, “insert”, “command”, “getmore”.
-
command The name of the database command performed, such as
isMasterorgetLastError. This field is left blank for operations that are not commands, such as queries and inserts.
-
ns The namespace on which the request was executed.
-
request_data The payload of the operation.
- Query operations:
request_datacontains the actual query that was issued. - Insert operations:
request_datacontains the documents being inserted. - Update operations:
request_datacontains the query selector and the update modifier.
- Query operations:
-
reply_data The payload of the reply to the request.
-
nreturned The number of documents returned as a result of the operation.
-
played_at The time at which the
playcommand executed the operation.
-
play_at The time at which the operation was supposed to be executed by the
playcommand.
-
playbacklag_us The difference in microseconds in time between
played_atandplay_at. Higher values generally indicate that the target server is not able to keep up with the rate at which requests need to be executed according to the playback file.
-
connection_num A key that identifies the connection on which the request was executed. All requests/replies that executed on the same connection have the same value for
connection_num.The
connection_numvalue does not match the connection ID logged on the server side.
-
latency_us The time difference in microseconds between when the request was sent by the client and when a response from the server was received.
-
errors Lists any errors returned from the server.
-
msg Lists the error message returned from the server.
-
seen The time at which the operation was originally captured.
-
request_id The ID of the MongoDB operation. The
request_idfor a request operation is the same as theresponse_idfor the corresponding reply.
Output Formatting with --format¶
monitor and play output to either the
terminal or, when run with --report (i.e. monitor --report or play --report), to a file.
Use the --collect (monitor --collect or play --collect)
option to specify the format of the output:
Examples¶
Use record¶
Capture TCP data with record¶
To create a recording of traffic, use the record command. The
following operation records traffic through port 27017 on the
eth0 network interface and writes the captured traffic to
~/recordings/recording.bson:
The produced playback file contains everything needed to re-execute the workload on another system.
Record a Playback File from Existing pcap Data¶
If you do not wish to use mongoreplay to capture traffic, you
can capture traffic using a utility such as tcpdump and then create a
mongoreplay recording from the static pcap file.
Warning
Only use root privileges when connecting to a trusted source.
The following operation uses tcpdump to capture traffic through
port 27017 on the eth0 network interface and writes the captured
data to a pcap file called traffic.pcap:
To create the mongoreplay playback file, you can use
record with the -f
option to specify the pcap file from which to create the playback file,
as in the following:
The produced playback file contains everything needed to re-execute the workload on another system.
Use play¶
Execute a Playback File Against a Target Host¶
play takes a playback file and executes the
recorded operations against the mongodb://example.com:27018 host.
Since the mongod enforces authentication, the
connection string specified to
--host also includes the username, password, and authentication
database so that mongoreplay can write to the database.
By default, ~bin.play executes the playback
file at the rate of the recording. --speed lets you modify
the playback speed. For example, the following operation executes
the operations in recording.bson at twice the recording speed:
Log Metrics About Execution Performance during Playback¶
play can produce a report with detailed metrics
about the performance of each operation executed during playback.
The following example executes playback against the
mongodb://example.com:27017 host and produces a JSON report written to
~/reports/playback-report.json
The play report contents would resemble:
Refer to mongoreplay Report Format for a description of each field.
Use monitor¶
Inspect Recorded Operations¶
monitor can create a report based on the
contents of a playback file. monitor’s report includes
all operations and some metadata about each operation’s execution.
The following operation uses monitor to create a JSON
report based on the contents of the recording.bson playback file
located in the ~/recordings directory and write the report to
~/reports/monitoring-report.json:
The report contents would resemble:
Refer to mongoreplay Report Format for a description of each field.
Inspect Live MongoDB Traffic¶
monitor can also inspect live traffic and, optionally,
create a report based on the observed operations.
To monitor traffic in real time in your terminal, omit the
--report option, as in the
following:
Optionally, you can create a report based on the operations observed
using monitor. The following example creates a JSON
report written to ~/reports/monitor-live.json based on the traffic
through port 27017 on the eth0 network interface: