Skip to content
Last9 named a Gartner Cool Vendor in AI for SRE Observability for 2025! Read more →
Last9

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:

  1. Create Monitoring Role

    Log into your MongoDB instance and create a custom role for monitoring:

    use admin
    db.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: []
    })
  2. 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" ]
    })
  3. Configure MongoDB Settings

    Edit the MongoDB configuration file to enable monitoring features:

    sudo nano /etc/mongod.conf

    Modify the following settings in mongod.conf:

    storage:
    dbPath: /var/lib/mongodb
    journal:
    enabled: true
    systemLog:
    destination: file
    logAppend: true
    path: /var/log/mongodb/mongod.log
    net:
    port: 27017
    bindIp: 0.0.0.0
    processManagement:
    timeZoneInfo: /usr/share/zoneinfo
    security:
    authorization: enabled
    operationProfiling:
    mode: all
    slowOpThresholdMs: 100
    setParameter:
    diagnosticDataCollectionEnabled: true
  4. 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:

  1. Download and Install Collector

    Download and install the OpenTelemetry Collector for AMD64 systems:

    sudo apt-get update
    sudo apt-get -y install wget systemctl
    wget https://github.com/open-telemetry/opentelemetry-collector-releases/releases/download/v0.110.0/otelcol-contrib_0.110.0_linux_amd64.deb
    sudo dpkg -i otelcol-contrib_0.110.0_linux_amd64.deb

Configure OpenTelemetry Collector

  1. 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: true
    retry_on_failure:
    enabled: true
    hostmetrics:
    collection_interval: 60s
    scrapers:
    cpu:
    metrics:
    system.cpu.logical.count:
    enabled: true
    memory:
    metrics:
    system.memory.utilization:
    enabled: true
    system.memory.limit:
    enabled: true
    load:
    disk:
    filesystem:
    metrics:
    system.filesystem.utilization:
    enabled: true
    network:
    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: 60s
    initial_delay: 1s
    processors:
    batch:
    timeout: 5s
    send_batch_size: 10000
    send_batch_max_size: 10000
    resourcedetection/system:
    detectors: ["system"]
    system:
    hostname_sources: ["os"]
    transform/add_timestamp:
    flatten_data: true
    log_statements:
    - context: log
    statements:
    - set(observed_time, Now())
    - set(time_unix_nano, observed_time_unix_nano) where time_unix_nano == 0
    transform/add_service:
    flatten_data: true
    log_statements:
    - context: log
    statements:
    - set(resource.attributes["service.name"], "mongodb")
    transform/hostmetrics:
    metric_statements:
    - context: datapoint
    statements:
    - 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: detailed
    service:
    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
  2. 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

  1. Check Collector Process

    Verify the collector is running:

    ps aux | grep otelcol-contrib
  2. Monitor Collector Logs

    Check the collector output for successful metric collection:

    # If running as a service
    sudo journalctl -u otelcol-contrib -f
    # If running in foreground, check the console output
  3. Test MongoDB Connection

    Verify the collector can connect to MongoDB:

    # Check MongoDB status
    sudo systemctl status mongod
    # Test connection with monitoring user
    mongo -u otel -p otel --authenticationDatabase admin
  4. 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 slowOpThresholdMs based 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: