Lesson 4: MongoDB Server Log Customizations / Learn
Code Summary: MongoDB Server Log Customizations
Review the following code, which demonstrates how to:
- Set a
slowmsthreshold - Find slow operations in a log
- Set the verbosity level
- Generate log messages related to slow operations
Set a slowms Threshold
To set a slowms threshold for an M10-or-above Atlas cluster or a self-managed MongoDB deployment, use the db.setProfilingLevel() method in mongosh. This method accepts two parameters: the profiler level and an options object. The profiler level is set to 0 to disable profiling completely, set to 1 for profiling operations that take longer than the threshold, and set to 2 for profiling all operations.
In the following example, the command leaves the profile disabled but changes the slowms threshold to 30 milliseconds:
db.setProfilingLevel(0, { slowms: 30 })
Note: When the profiler is disabled, db.setProfilingLevel() configures which slow operations are written to the diagnostic log.
Find Slow Operations in a Log
The following example shows how to find slow operations in a log file.
First, find a specific document in the listingsAndReviews collection by running the following code:
db.listingsAndReviews.findOne({ "host.host_id": '1282196'})
Then find all documents sorted by the number of listings by running the following code. Without an index, the query will be slow.
db.listingsAndReviews.find({}).sort( {"host.host_total_listings_count":-1})
To find log messages related to slow operations, use the grep command to find instances of the phrase “Slow query”, and then pipe the result into jq, a utility for processing and pretty-printing JSON:
sudo grep "Slow query" /var/log/mongodb/mongod.log | jq
Set the Verbosity Level
The following code sets the verbosity level on a self-managed instance by using the configuration file.
First, open the file in the Vim text editor to make some changes:
vim /etc/mongod.conf
To set a global verbosity level for all components, add the following property to the mongod.conf file under the systemLog section:
...
systemLog:
verbosity: 1
...
To set the verbosity level for a single component, add the following to the systemLog section of the file:
...
systemLog:
...
component:
index:
verbosity: 1
...
Restart the mongod service on a Linux environment that is managed by systemctl by using the following command:
sudo systemctl mongod restart
Generate Log Messages Related to Slow Operations
To generate log messages related to the index component, create an index on the host field to support the slow query. To do so, use the createIndex() method in mongosh. This command also uses the --eval parameter, which allows you to immediately pass commands to mongosh without entering the shell. The --quiet option reduces noise in the output.
"mongodb://localhost:27017/sample_airbnb" --quiet --eval "db.listingsAndReviews.createIndex({ host: 1 })"
To find log messages related to the INDEX component, use the following command:
sudo grep INDEX /var/log/mongodb/mongod.log | jq