Monitoring MongoDB with Prometheus using the MongoDB Exporter

Sumanta Mukhopadhyay
4 min readMar 3, 2023

--

Monitoring databases is a critical aspect of modern software engineering. Without proper monitoring, it can be challenging to identify issues in production environments and optimize application performance. In this post, we will explore how to monitor MongoDB using Prometheus and the MongoDB Exporter.

Prometheus is an open-source monitoring system and time-series database that can be used to collect, store, and query metrics from various sources. Prometheus has become a popular monitoring solution in the DevOps community due to its scalability, reliability, and ease of use.

MongoDB is a NoSQL document-oriented database that is widely used for its flexibility, scalability, and performance. MongoDB is a popular choice for modern web applications due to its ability to handle unstructured data and scale horizontally.

The MongoDB Exporter is a tool that exposes MongoDB metrics in a format that Prometheus can scrape. The MongoDB Exporter can be deployed as a standalone binary or as a Docker container. The MongoDB Exporter supports MongoDB 3.0 and later versions.

Let’s get started with monitoring MongoDB using Prometheus and the MongoDB Exporter.

Prerequisites

Before we get started, make sure you have the following prerequisites:

  • A MongoDB instance running on your system or a remote server.
  • Prometheus is installed on your system or a remote server.
  • Grafana installed on your system or a remote server (optional).

Step 1: Installing the MongoDB Exporter

The first step is to install the MongoDB Exporter. You can download the MongoDB Exporter binary from the official GitHub repository. Once you have downloaded the binary, you can run it as follows:

./mongodb_exporter --mongodb.uri=mongodb://localhost:27017 --web.listen-address=:9001

This command starts the MongoDB Exporter and exposes metrics on port 9001. You can specify the MongoDB connection string using the --mongodb.uri flag.

Alternatively, you can deploy the MongoDB Exporter as a Docker container as follows:

docker run -d --name mongodb_exporter -p 9001:9001 mongodb_exporter --mongodb.uri=mongodb://localhost:27017

This command starts the MongoDB Exporter as a Docker container and maps port 9001 to the container port. You can specify the MongoDB connection string using the --mongodb.uri flag.

Step 2: Configuring Prometheus to Scrape Metrics from the MongoDB Exporter

The next step is to configure Prometheus to scrape metrics from the MongoDB Exporter. You can do this by adding the MongoDB Exporter endpoint to the Prometheus configuration file. Open the Prometheus configuration file (prometheus.yml) and add the following job definition:

- job_name: 'mongodb'
scrape_interval: 5s
static_configs:
- targets: ['localhost:9001']

This job definition configures Prometheus to scrape metrics from the MongoDB Exporter every 5 seconds. You can customize the scrape interval based on your requirements. Make sure to replace localhost:9001 with the hostname and port of your MongoDB Exporter instance.

Step 3: Starting Prometheus and Viewing Metrics

Once you have configured Prometheus, you can start it using the following command:

./prometheus --config.file=prometheus.yml

This command starts Prometheus and loads the configuration from the prometheus.yml file. Once Prometheus is running, you can view the metrics by navigating to http://localhost:9090 in your web browser. Click on the Graph tab to view a list of available metrics.

Step 4: Visualizing Metrics in Grafana (Optional)

If you want to visualize MongoDB metrics in Grafana, you can do so by creating a new dashboard and adding Prometheus as a data source. To create a new dashboard, click on the + icon on the left-hand side of the Grafana interface and select Dashboard. You can then add panels to the dashboard and configure them to display MongoDB metrics.

Here are some examples of MongoDB metrics that you can monitor using Prometheus and the MongoDB Exporter:

  • mongodb_connections: Number of connections to the MongoDB instance.
  • mongodb_database_operations: Number of operations performed on databases (inserts, updates, deletes, queries, etc.).
  • mongodb_memory_bits: Amount of memory used by the MongoDB instance in bits.
  • mongodb_replset_health: Status of the MongoDB replica set (primary, secondary, or other).

You can create a new panel in Grafana by clicking on the Add panel button and selecting the Graph option. In the panel configuration menu, you can select the Prometheus data source and enter a query to retrieve the desired metric. For example, to monitor the number of database operations, you can use the following query:

sum by(database) (mongodb_database_operations)

This query retrieves the mongodb_database_operations metric and groups the results by the database.

Conclusion

Monitoring MongoDB using Prometheus and the MongoDB Exporter is a powerful way to gain insights into the performance and health of your database. With Prometheus, you can collect and store metrics from various sources and use Grafana to visualize the data in real time. The MongoDB Exporter makes it easy to expose MongoDB metrics in a format that Prometheus can scrape. By monitoring MongoDB using Prometheus and the MongoDB Exporter, you can identify issues early and optimize the performance of your applications.

--

--

Sumanta Mukhopadhyay
Sumanta Mukhopadhyay

No responses yet