Monitoring MongoDB Driver with Prometheus in a Node.js application

Christian Galsterer
3 min readNov 25, 2023

--

Photo by Cody Fitzgerald on Unsplash

In a product oriented and data driven organisation having a solid understanding how your product is used, how it behaves and how resources are utilized is crucial for the success of the product. This includes basic questions if your application is running at all, if there are errors, which features are used and how, e.g. are there peaks in usage during certain periods of time to questions if resources are used economically to handle the user requests.

This article provides an overview how you can monitor your MongoDB driver in a Node.js application with Prometheus to address all the different use cases mentioned above so that you can make informed decision about your product quality and user experience.

Step 1: Add dependencies
Add the mongodb-driver-prometheus-exporter dependency to your project which enables monitoring of your MongoDB driver.

npm i @christiangalsterer/mongodb-driver-prometheus-exporter

If not done yet, please add the dependency for prom-client too.

npm i prom-client

Step 2: Set up the mongodb-driver-prometheus-exporter
After adding the dependencies set up your mongo client and register the exporter with the prom-client.

With Typescript you can start using the following example:

import { MongoClient } from "mongodb";
import { Registry, collectDefaultMetrics } from "prom-client";
import { monitorMongoDBDriver } from "@christiangalsterer/mongodb-driver-prometheus-exporter";

...

// set up the MongoDB client, monitorCommands needs to be set to true to enable command monitoring.
const mongoClient = new MongoClient("mongodb", { monitorCommands: true })

// set up the prometheus client
const register = new Registry();
collectDefaultMetrics({ register });

// monitor MongoDB driver
monitorMongoDBDriver(mongoClient, register);

...

// connect to MongoDB after calling monitorMongoDBDriver()
mongoClient.connect();

With Javascript you can start using the following example:

const MongoClient = require('mongodb');
const promClient = require( 'prom-client');
const exporter = require('@christiangalsterer/mongodb-driver-prometheus-exporter')

...

// set up the MongoDB client, monitorCommands needs to be set to true to enable command monitoring.
const mongoClient = new MongoClient("mongodb", { monitorCommands: true })

// set up the prometheus client
const collectDefaultMetrics = promClient.collectDefaultMetrics;
const Registry = promClient.Registry;
const register = new Registry();
collectDefaultMetrics({ register });

// monitor MongoDB driver
exporter.monitorMongoDBDriver(client, register);

...

// connect to MongoDB after calling monitorMongoDBDriver()
mongoClient.connect();

Step 3: Start monitoring

Congratulations!!! You have completed all preparation steps and can start monitoring the MongoDB Driver in your Node.js application.

An example dashboard for Grafana is available here displaying the provided metrics by the exporter.

Here an example for collection metrics:

Here an example for command metrics:

Available Metrics

The complete list of available metrics can be found here

Configuration

The exporter can be configured in a couple of ways. The complete list of configuration options can be found here.

--

--