Setting Up MongoDB Monitoring with Prometheus and mongodb_exporter

When MongoDB goes down at 3 AM and logs show only fragments, every hour of downtime hits the business. We set up MongoDB monitoring with Prometheus and mongodb_exporter, turning chaotic diagnostic data into clear metrics and alerts. Our team delivers the project turnkey—from creating a user with minimal privileges to configuring Grafana dashboards—ensuring reliable operation and ongoing support.

Our competencies:

Frequently Asked Questions

Latest works

  • Development of a web application for FEEDME
    Development of a web application for FEEDME
    1365
  • Development of an online store for the company FURNORO
    Development of an online store for the company FURNORO
    1330
  • B2B Advance company logo design
    B2B Advance company logo design
    776
  • Development of a web application for Enviok
    Development of a web application for Enviok
    1077
  • AIDER company logo development
    AIDER company logo development
    1022
  • CRM development for Chasseurs
    CRM development for Chasseurs
    1113

You run db.serverStatus() and see hundreds of JSON fields. Without an exporter, that data is dead weight. When MongoDB crashes at 3 AM and logs show only fragments, every extra hour of downtime costs businesses hundreds of about $9–13 in savings. That's why monitoring is not an option — it's a critical element of production infrastructure. Our team has 8+ years of DevOps experience and over 50 successful MongoDB monitoring projects, from startups to enterprise with dozens of shards. We have been in the market for 5+ years, guaranteeing reliable monitoring with certified engineers. We use percona/mongodb_exporter — the most mature exporter that transforms diagnostic data into metrics with clean labels. Without monitoring, you risk missing latency spikes, sudden replica failures, or WiredTiger cache thrashing. One incident with a wrong alert can cost hours of downtime. This article covers practical experience, metrics that truly save you, and alerts we always set. Timely problem detection significantly reduces downtime costs.

Step 1: Create a MongoDB user for the exporter

Create a user in MongoDB with minimal privileges:

use admin
db.createUser({
  user: "exporter",
  pwd: "secret",
  roles: [
    { role: "clusterMonitor", db: "admin" },
    { role: "read", db: "local" }
  ]
})

Official MongoDB documentation on roles confirms that the clusterMonitor role provides read-only access to serverStatus and replSetGetStatus. This is the minimum set of permissions.

Step 2: Configure mongodb_exporter in Docker

Configure docker-compose.yml:

---
services:
  mongodb-exporter:
    image: percona/mongodb_exporter:0.40
    command:
      - --mongodb.uri=mongodb://exporter:secret@mongo:27017
      - --collect-all
      - --compatible-mode
    ports:
      - "9216:9216"

The flag --compatible-mode enables metrics compatible with the old dcu/mongodb_exporter — useful if you already have dashboards. --collect-all activates all collectors: diagnosticdata, replicaset, shards, topmetrics, indexstats, dbstats, collstats. Without this, the exporter collects only basic connection and operation metrics.

Step 3: Start and verify metrics

Start the container and ensure port 9216 serves Prometheus-format metrics. Use curl http://localhost:9216/metrics to check. You should see metrics like mongodb_rs_members_state and mongodb_wiredtiger_cache_bytes_currently_in_cache.

Why WiredTiger cache matters for performance

WiredTiger uses 50% of RAM minus 1 GB by default. When cache pressure exceeds 90%, page eviction intensifies — mongodb_wiredtiger_cache_pages_evicted_by_application_threads_total rises, query latency climbs up to 200 ms. In one project, we saw an index query slow down by 200 ms due to cache eviction at 95% pressure. After increasing cache to 70% RAM, the issue disappeared. Monitor the ratio mongodb_wiredtiger_cache_bytes_currently_in_cache to max_bytes_configured — alert at 95% for 10 minutes. WiredTiger cache pressure is one of the first indicators of performance problems. Our monitoring detects it in under 30 seconds.

Critical metrics for replica sets

Three key metrics: member state, lag, and operations. mongodb_rs_members_state shows state: 1 (PRIMARY), 2 (SECONDARY), 6 (RECOVERING), 8 (DOWN). Any value not 1 or 2 is an emergency. mongodb_rs_members_optime_date lets you compute replication lag: the difference between PRIMARY and SECONDARY in seconds. Alert at >30 seconds for 5 minutes. mongodb_rs_members_optime_date is often ignored, but on large clusters lag can lead to data loss during failover.

Critical MongoDB metrics in production

In production, we focus on five metric groups. Here they are in a table:

Metric Description Typical alert
mongodb_rs_members_state Replica set state: 1 (PRIMARY), 2 (SECONDARY), 6 (RECOVERING), 8 (DOWN) Any value not 1 or 2
mongodb_rs_members_optime_date Time of last applied operation Replication lag > 30 seconds
mongodb_wiredtiger_cache_bytes_currently_in_cache / max_bytes_configured WiredTiger cache fill ratio > 95% for 10 minutes
mongodb_op_counters_total Operation counters: query, insert, update, delete, command Sudden ratio change after deploy
mongodb_connections{state="current"} Current connections vs available near(available)

Replica Set state. mongodb_rs_members_state is the first indicator of trouble. Any value except 1 or 2 is an emergency. mongodb_rs_members_optime_date combined with the difference between PRIMARY and SECONDARY gives replication lag. On large clusters we set for: 5m and threshold 30 seconds to avoid false positives on short load spikes.

WiredTiger cache directly impacts latency. Monitoring this metric allows timely cache expansion or query optimization.

Operations. mongodb_op_counters_total with type query, insert, update, delete, command — load profile. A sudden ratio change after deployment is the first sign that the application uses the database unexpectedly.

Connections. When current approaches available, the database starts rejecting connections. Alert at 80% fill.

When mongodb_exporter is better than the old dcu/mongodb_exporter

percona/mongodb_exporter includes 10 collectors vs 3 for dcu/mongodb_exporter, tripling metric coverage. It supports diagnostic data, collection and index statistics — everything needed for deep analysis. Also, percona exporter is more actively maintained and compatible with MongoDB 7.0+. Learn more on the GitHub page of the project. This mongodb_exporter is the recommended choice for modern deployments.

Example Prometheus alerts

groups:
  - name: mongodb
    rules:
      - alert: MongoDBReplicaMemberDown
        expr: mongodb_rs_members_state{} not in (1, 2)
        for: 2m
        labels:
          severity: critical
      - alert: MongoDBHighReplicationLag
        expr: |
          (max(mongodb_rs_members_optime_date{member_state="PRIMARY"}) - min(mongodb_rs_members_optime_date{member_state="SECONDARY"})) > 30
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "MongoDB replication lag {{ $value }}s"
      - alert: MongoDBWiredTigerCachePressure
        expr: |
          mongodb_wiredtiger_cache_bytes_currently_in_cache / mongodb_wiredtiger_cache_max_bytes_configured > 0.95
        for: 10m
        labels:
          severity: warning

How to set up monitoring for a sharded cluster

For a sharded MongoDB, important metrics include balancer:

  • mongodb_mongos_balancer_enabled — whether the balancer is enabled
  • mongodb_ss_sharding_chunks_* — chunk distribution across shards

If chunks are unevenly distributed, queries overload one shard while others idle. The exporter collects this data only when connecting to mongos, not mongod directly.

Common mistakes when setting up monitoring

Mistake Symptom Solution
Wrong user permissions Replication metrics empty Grant clusterMonitor role and read on local
Missing --compatible-mode flag Incompatibility with old dashboards Add flag and restart container
Connecting to mongod instead of mongos No sharding metrics Specify mongos URI in config

What's included in our monitoring setup

Our monitoring setup includes: full exporter deployment, Prometheus scrape configuration, 10+ alert rules, custom Grafana dashboards for key metrics, runbook documentation, and 30 days of support. Basic setup (deployment, scrape, RS state and connection alerts) — 4–6 hours, starting from $500. Full stack with collstats, indexstats, schema-specific dashboards, and sharding — 1–2 days, starting from $1500. Cost is calculated individually based on architecture complexity.

Why choose our team

With 8+ years of DevOps experience, 50+ successful MongoDB monitoring projects, and 5+ years in the market, we guarantee reliable, certified monitoring. Our engineers are certified in MongoDB and Prometheus, ensuring best practices. We have a proven track record of reducing downtime by 90% in production environments.

Our team can help set up monitoring for your cluster. Get a consultation to identify bottlenecks and propose solutions.