MongoDB
Monitor MongoDB database performance and metrics with OpenTelemetry and Last9
Use OpenTelemetry to instrument your MongoDB database and send telemetry data to Last9. This integration provides comprehensive monitoring of database performance, operations profiling, and system resource utilization.
Prerequisites
Before setting up MongoDB monitoring, ensure you have:
- MongoDB 4.x or 5.x installed and running
- Administrative access to create MongoDB users and roles
- OpenTelemetry Collector binary or container runtime
- Last9 account with integration credentials
Database Setup
The setup process varies slightly between MongoDB versions. Choose the appropriate section for your MongoDB version:
-
Create Monitoring Role
Log into your MongoDB instance and create a custom role for monitoring:
use admindb.createRole({role: "systemCollectionsRole",privileges: [{resource: { db: "admin", collection: "system.users" },actions: [ "find", "update", "insert", "remove", "collStats", "dbStats" ]},{resource: { db: "admin", collection: "system.version" },actions: [ "find", "update", "insert", "remove", "collStats", "dbStats" ]}],roles: []}) -
Create Monitoring User
Create a dedicated user for OpenTelemetry monitoring with the necessary permissions:
db.createUser({user: "otel",pwd: "otel",roles: [{ role: "root", db: "admin" },{ role: "userAdminAnyDatabase", db: "admin" },{ role: "clusterMonitor", db: "admin" },{ role: "readWriteAnyDatabase", db: "admin" },{ role: "dbAdminAnyDatabase", db: "admin" },{ role: "systemCollectionsRole", db: "admin" }],mechanisms: [ "SCRAM-SHA-1" ]}) -
Configure MongoDB Settings
Edit the MongoDB configuration file to enable monitoring features:
sudo nano /etc/mongod.confModify the following settings in
mongod.conf:storage:dbPath: /var/lib/mongodbjournal:enabled: truesystemLog:destination: filelogAppend: truepath: /var/log/mongodb/mongod.lognet:port: 27017bindIp: 0.0.0.0processManagement:timeZoneInfo: /usr/share/zoneinfosecurity:authorization: enabledoperationProfiling:mode: allslowOpThresholdMs: 100setParameter:diagnosticDataCollectionEnabled: true -
Restart MongoDB Service
Apply the configuration changes by restarting MongoDB:
sudo systemctl restart mongod
-
Create Monitoring Role
Log into your MongoDB instance and create a custom role for monitoring:
use admindb.createRole({role: "systemCollectionsRole",privileges: [{resource: { db: "admin", collection: "system.users" },actions: [ "find", "update", "insert", "remove", "collStats", "dbStats" ]},{resource: { db: "admin", collection: "system.version" },actions: [ "find", "update", "insert", "remove", "collStats", "dbStats" ]}],roles: []}) -
Create Monitoring User
Create a dedicated user for OpenTelemetry monitoring with the necessary permissions:
db.createUser({user: "otel",pwd: "otel",roles: [{ role: "root", db: "admin" },{ role: "userAdminAnyDatabase", db: "admin" },{ role: "clusterMonitor", db: "admin" },{ role: "readWriteAnyDatabase", db: "admin" },{ role: "dbAdminAnyDatabase", db: "admin" },{ role: "systemCollectionsRole", db: "admin" },],mechanisms: ["SCRAM-SHA-1", "SCRAM-SHA-256"],}); -
Configure MongoDB Settings
Edit the MongoDB configuration file to enable monitoring features:
sudo nano /etc/mongod.confAdd the following settings to the end of
mongod.conf:operationProfiling:mode: allslowOpThresholdMs: 100setParameter:diagnosticDataCollectionEnabled: true -
Restart MongoDB Service
Apply the configuration changes by restarting MongoDB:
sudo systemctl restart mongod
Install OpenTelemetry Collector
Choose the appropriate binary for your system architecture:
-
Download and Install Collector
Download and install the OpenTelemetry Collector for AMD64 systems:
sudo apt-get updatesudo apt-get -y install wget systemctlwget https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v0.110.0/otelcol-contrib_0.110.0_linux_amd64.debsudo dpkg -i otelcol-contrib_0.110.0_linux_amd64.deb
-
Download and Install Collector
Download and install the OpenTelemetry Collector for ARM64 systems:
sudo apt-get updatesudo apt-get -y install wget systemctlwget https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v0.110.0/otelcol-contrib_0.110.0_linux_arm64.debsudo dpkg -i otelcol-contrib_0.110.0_linux_arm64.deb
Configure OpenTelemetry Collector
-
Create Collector Configuration
Create the collector configuration file at
/etc/otelcol-contrib/config.yaml. Note that the MongoDB connection string format varies based on version:- For MongoDB < 4.4:
"127.0.0.1:27017" - For MongoDB ≥ 4.4 and 5.x:
"127.0.0.1:27017/?directConnection=true"
receivers:filelog:# File path pattern to read logs from. Update this to the destination from where you want to read logs.include: [/var/log/mongodb/*.log]include_file_path: trueretry_on_failure:enabled: truehostmetrics:collection_interval: 60sscrapers:cpu:metrics:system.cpu.logical.count:enabled: truememory:metrics:system.memory.utilization:enabled: truesystem.memory.limit:enabled: trueload:disk:filesystem:metrics:system.filesystem.utilization:enabled: truenetwork:paging:mongodb:hosts:- endpoint: "127.0.0.1:27017/?directConnection=true"# replica_set: <name_of_replica_set>username: "MONGODB_USERNAME"password: "MONGODB_PASSWORD"collection_interval: 60sinitial_delay: 1sprocessors:batch:timeout: 5ssend_batch_size: 10000send_batch_max_size: 10000resourcedetection/system:detectors: ["system"]system:hostname_sources: ["os"]transform/add_timestamp:flatten_data: truelog_statements:- context: logstatements:- set(observed_time, Now())- set(time_unix_nano, observed_time_unix_nano) where time_unix_nano == 0transform/add_service:flatten_data: truelog_statements:- context: logstatements:- set(resource.attributes["service.name"], "mongodb")transform/hostmetrics:metric_statements:- context: datapointstatements:- set(attributes["host.name"], resource.attributes["host.name"])- set(attributes["cloud.account.id"], resource.attributes["cloud.account.id"])- set(attributes["cloud.availability_zone"], resource.attributes["cloud.availability_zone"])- set(attributes["cloud.platform"], resource.attributes["cloud.platform"])- set(attributes["cloud.provider"], resource.attributes["cloud.provider"])- set(attributes["cloud.region"], resource.attributes["cloud.region"])- set(attributes["host.type"], resource.attributes["host.type"])- set(attributes["host.image.id"], resource.attributes["host.image.id"])exporters:otlp/last9:endpoint: "{{ .Logs.WriteURL }}"headers:"Authorization": "{{ .Logs.AuthValue }}"debug:verbosity: detailedservice:pipelines:logs:receivers: [filelog]processors:[batch,resourcedetection/system,transform/add_timestamp,transform/add_service,]exporters: [otlp/last9]metrics:receivers: [mongodb, hostmetrics]processors: [batch, resourcedetection/system, transform/hostmetrics]exporters: [otlp/last9]Replace the placeholder values:
MONGODB_USERNAME: Your MongoDB monitoring username (e.g., “otel”)MONGODB_PASSWORD: Your MongoDB monitoring password (e.g., “otel”)- Update the endpoint URL and authorization header with your Last9 credentials
- For MongoDB < 4.4:
-
Start the Collector
Start the OpenTelemetry Collector with the configuration:
otelcol-contrib --config /etc/otelcol-contrib/config.yaml --feature-gates transform.flatten.logs
Understanding the Setup
MongoDB Receiver
The MongoDB receiver collects metrics directly from your MongoDB instance including:
- Database Metrics: Collections, documents, indexes, and storage statistics
- Operations Metrics: Read/write operations, query performance, and connection counts
- Resource Metrics: Memory usage, cache utilization, and network I/O
Host Metrics Collection
The collector also gathers system-level metrics:
- CPU utilization and load
- Memory usage and limits
- Disk I/O and filesystem utilization
- Network statistics
Log Collection
File log receiver monitors MongoDB log files for:
- Database operations and errors
- Connection events
- Query performance information
- Authentication and authorization events
Verification
-
Check Collector Process
Verify the collector is running:
ps aux | grep otelcol-contrib -
Monitor Collector Logs
Check the collector output for successful metric collection:
# If running as a servicesudo journalctl -u otelcol-contrib -f# If running in foreground, check the console output -
Test MongoDB Connection
Verify the collector can connect to MongoDB:
# Check MongoDB statussudo systemctl status mongod# Test connection with monitoring usermongo -u otel -p otel --authenticationDatabase admin -
Verify Metrics in Last9
Log into your Last9 account and check that MongoDB metrics are being received in Grafana.
Best Practices
- Security: Use strong passwords for the monitoring user and limit network access to MongoDB
- Performance: Adjust
slowOpThresholdMsbased on your application’s performance requirements - Storage: Monitor log file sizes and implement log rotation to prevent disk space issues
- Monitoring: Set up alerts for critical MongoDB metrics like connection count and replication lag
Need Help?
If you encounter any issues or have questions:
- Join our Discord community for real-time support
- Contact our support team at support@last9.io