Prometheus exporter to monitor the MongoDB Node.js driver
npm install @christiangalsterer/mongodb-driver-prometheus-exporter










!github stars
A prometheus exporter exposing metrics for the official MongoDB Node.js driver.
Metrics names follow the same naming convention used by micrometer. This allows to use the same metrics in dashboards and alerts across different technology stacks, e.g. when you use Spring Boot and Node.js in different applications.
The exporter provides the following metrics.
|Metric Name|Description|Labels|Since|
|---|---|---|---|
|mongodb_driver_pool_size | the current size of the connection pool, including idle and in-use members |
mongodb >=6.9.0| mongodb >=6.9.0|Here an example output in the prometheus format of the provided metrics.
``shHELP mongodb_driver_pool_size the current size of the connection pool, including idle and and in-use members
TYPE mongodb_driver_pool_size gauge
mongodb_driver_pool_size{server_address="127.0.0.1:27017"} 0
mongodb_driver_pool_size{server_address="127.0.0.2:27017"} 0
mongodb_driver_pool_size{server_address="127.0.0.3:27017"} 1
Usage
Add Dependency
Add the following dependency to your project to download the package from npm.
`sh
npm i @christiangalsterer/mongodb-driver-prometheus-exporter
`TypeScript
The following example illustrates how to use the exporter to enable monitoring for the MongoDB Node.js driver.
`ts
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();
`JavaScript
The following example illustrates how to use the exporter to enable monitoring for the MongoDB Node.js driver.
`js
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();
``The exporter can be configured via properties specified on the optional parameter of type
_MongoDBDriverExporterOptions_.
|property|Description|Example|Since |
|---|---|---|---|
| mongodbDriverCommandsSecondsHistogramBuckets | Buckets for the mongodb_driver_commands_seconds_bucket metric in seconds. Default buckets are [0.001, 0.005, 0.01, 0.02, 0.03, 0.04, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10] | [0.001, 0.005, 0.01, 0.02, 0.03, 0.04, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10]| 1.0.0|
| waitQueueSecondsHistogramBuckets | Buckets for the mongodb_driver_pool_waitqueue_seconds_bucket metric in seconds. Default buckets are [0.001, 0.005, 0.01, 0.02, 0.03, 0.04, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10] | [0.001, 0.005, 0.01, 0.02, 0.03, 0.04, 0.05, 0.1, 0.2, 0.5, 1, 2, 5, 10]| 2.3.0|
| defaultLabels | Default labels added to each metrics. | {'foo':'bar', 'alice': 3} | 1.1.0 |
| prefix | Metric name prefix for all metrics. | 'service1_' | 2.1.0 |
An example dashboard for Grafana is available here displaying the provided metrics by the exporter.
Here an example for collection metrics:
!Grafana:Collection Metrics
Here an example for command metrics:
!Grafana:Commands Metrics
The changes to project can be found in the changelog.
The following table list the compatibility of exporter versions with different MongoDB Driver and prom-client versions.
|Exporter Version|MongoDB Driver Version|prom-client version|
|---|---|---|
|^1.0.0|^5.8.0|^14.2.0|
|^2.0.0|^6.0.0|^15.0.0|
Contributions are highly welcome. If you want to contribute to this project please follow the steps described in the contribution guidelines.
If you want to support this project, please add a link to your project and/or company when you use this exporter.
If you are looking for a way to monitor KafkaJS for Node.js you may have a look at
If you are looking for a way to monitor node-postgres you may have a look at