@christiangalsterer/mongodb-driver-prometheus-exporter

中文文档
3.0.0发布于 2025/12/13MITTypeScriptCommonJS构建来源已验证近 30 天下载 9949

Prometheus exporter to monitor the MongoDB Node.js driver

原始使用文档
文档目录5 节
  1. Available Metrics
  2. Example Output
  3. Add Dependency
  4. TypeScript
  5. JavaScript

GitHub Actions CI Status codecov Coveralls Known Vulnerabilities npm downloads npm version npm license semver Conventional Commits renovate code style: prettier github stars

Prometheus Exporter for official MongoDB Node.js driver

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.

Available Metrics

The exporter provides the following metrics.

Metric NameDescriptionLabelsSince
mongodb_driver_pool_sizethe current size of the connection pool, including idle and in-use members
  • server_address: URL of the MongoDB Service instance where the driver is connected to
    1.0.0
    mongodb_driver_pool_minthe minimum size of the connection pool
    • server_address: URL of the MongoDB Service instance where the driver is connected to
      1.0.0
      mongodb_driver_pool_maxthe maximum size of the connection pool
      • server_address: URL of the MongoDB Service instance where the driver is connected to
        1.0.0
        mongodb_driver_pool_checkedoutthe count of connections that are currently in use
        • server_address: URL of the MongoDB Service instance where the driver is connected to
          1.0.0
          mongodb_driver_pool_waitqueuesizethe current size of the wait queue for a connection from the pool
          • server_address: URL of the MongoDB Service instance where the driver is connected to
            1.0.0
            mongodb_driver_pool_waitqueue_seconds_sumTotal duration of wait queue in seconds
            * Requires mongodb >=6.9.0
            • server_address: URL of the MongoDB Service instance where the driver is connected to
            • status: SUCCESS or FAILED
              2.3.0
              mongodb_driver_pool_waitqueue_seconds_countTotal number of connections checked out from the waitqueue
              * Requires mongodb >=6.9.0
              • server_address: URL of the MongoDB Service instance where the driver is connected to
              • status: SUCCESS or FAILED
                2.3.0
                mongodb_driver_commands_seconds_sumDuration of the executed command in seconds
                • server_address: URL of the MongoDB Service instance where the driver is connected to
                • command: Name if the command
                • status: SUCCESS or FAILED
                  1.0.0
                  mongodb_driver_commands_seconds_countNumber of executed commands
                  • server_address: URL of the MongoDB Service instance where the driver is connected to
                  • command: Name if the command
                  • status: SUCCESS or FAILED
                    1.0.0

                    Example Output

                    Here an example output in the prometheus format of the provided metrics.

                    # HELP 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
                    
                    # HELP mongodb_driver_pool_min the minimum size of the connection pool
                    # TYPE mongodb_driver_pool_min gauge
                    mongodb_driver_pool_min{server_address="127.0.0.1:27017"} 0
                    mongodb_driver_pool_min{server_address="127.0.0.2:27017"} 0
                    mongodb_driver_pool_min{server_address="127.0.0.3:27017"} 0
                    
                    # HELP mongodb_driver_pool_max the maximum size of the connection pool
                    # TYPE mongodb_driver_pool_max gauge
                    mongodb_driver_pool_max{server_address="127.0.0.1:27017"} 100
                    mongodb_driver_pool_max{server_address="127.0.0.2:27017"} 100
                    mongodb_driver_pool_max{server_address="127.0.0.3:27017"} 100
                    
                    # HELP mongodb_driver_pool_checkedout the count of connections that are currently in use
                    # TYPE mongodb_driver_pool_checkedout gauge
                    mongodb_driver_pool_checkedout{server_address="127.0.0.1:27017"} 0
                    mongodb_driver_pool_checkedout{server_address="127.0.0.2:27017"} 0
                    mongodb_driver_pool_checkedout{server_address="127.0.0.3:27017"} 1
                    
                    # HELP mongodb_driver_pool_waitqueuesize the current size of the wait queue for a connection from the pool
                    # TYPE mongodb_driver_pool_waitqueuesize gauge
                    mongodb_driver_pool_waitqueuesize{server_address="127.0.0.1:27017"} 0
                    mongodb_driver_pool_waitqueuesize{server_address="127.0.0.2:27017"} 0
                    mongodb_driver_pool_waitqueuesize{server_address="127.0.0.3:27017"} 0
                    
                    # HELP mongodb_driver_pool_waitqueue_seconds Duratrion of mongodb connection wait queue
                    # TYPE mongodb_driver_pool_waitqueue_seconds histogram
                    mongodb_driver_pool_waitqueue_seconds_bucket{le="0.001",server_address="127.0.0.3:27017",status="SUCCESS"} 141
                    mongodb_driver_pool_waitqueue_seconds_bucket{le="0.005",server_address="127.0.0.3:27017",status="SUCCESS"} 141
                    mongodb_driver_pool_waitqueue_seconds_bucket{le="0.01",server_address="127.0.0.3:27017",status="SUCCESS"} 145
                    mongodb_driver_pool_waitqueue_seconds_bucket{le="0.02",server_address="127.0.0.3:27017",status="SUCCESS"} 148
                    mongodb_driver_pool_waitqueue_seconds_bucket{le="0.03",server_address="127.0.0.3:27017",status="SUCCESS"} 151
                    mongodb_driver_pool_waitqueue_seconds_bucket{le="0.04",server_address="127.0.0.3:27017",status="SUCCESS"} 152
                    mongodb_driver_pool_waitqueue_seconds_bucket{le="0.05",server_address="127.0.0.3:27017",status="SUCCESS"} 155
                    mongodb_driver_pool_waitqueue_seconds_bucket{le="0.1",server_address="127.0.0.3:27017",status="SUCCESS"} 214
                    mongodb_driver_pool_waitqueue_seconds_bucket{le="0.2",server_address="127.0.0.3:27017",status="SUCCESS"} 223
                    mongodb_driver_pool_waitqueue_seconds_bucket{le="0.5",server_address="127.0.0.3:27017",status="SUCCESS"} 223
                    mongodb_driver_pool_waitqueue_seconds_bucket{le="1",server_address="127.0.0.3:27017",status="SUCCESS"} 223
                    mongodb_driver_pool_waitqueue_seconds_bucket{le="2",server_address="127.0.0.3:27017",status="SUCCESS"} 223
                    mongodb_driver_pool_waitqueue_seconds_bucket{le="5",server_address="127.0.0.3:27017",status="SUCCESS"} 223
                    mongodb_driver_pool_waitqueue_seconds_bucket{le="10",server_address="127.0.0.3:27017",status="SUCCESS"} 223
                    mongodb_driver_pool_waitqueue_seconds_bucket{le="+Inf",server_address="127.0.0.3:27017",status="SUCCESS"} 223
                    mongodb_driver_pool_waitqueue_seconds_sum{server_address="127.0.0.3:27017",status="SUCCESS"} 6.4500000000000055
                    mongodb_driver_pool_waitqueue_seconds_count{server_address="127.0.0.3:27017",status="SUCCESS"} 223
                    
                    # HELP mongodb_driver_commands_seconds Timer of mongodb commands
                    # TYPE mongodb_driver_commands_seconds histogram
                    mongodb_driver_commands_seconds_bucket{le="0.005",command="ping",server_address="127.0.0.3:27017",status="SUCCESS"} 0
                    mongodb_driver_commands_seconds_bucket{le="0.01",command="ping",server_address="127.0.0.3:27017",status="SUCCESS"} 0
                    mongodb_driver_commands_seconds_bucket{le="0.025",command="ping",server_address="127.0.0.3:27017",status="SUCCESS"} 0
                    mongodb_driver_commands_seconds_bucket{le="0.05",command="ping",server_address="127.0.0.3:27017",status="SUCCESS"} 0
                    mongodb_driver_commands_seconds_bucket{le="0.1",command="ping",server_address="127.0.0.3:27017",status="SUCCESS"} 0
                    mongodb_driver_commands_seconds_bucket{le="0.25",command="ping",server_address="127.0.0.3:27017",status="SUCCESS"} 0
                    mongodb_driver_commands_seconds_bucket{le="0.5",command="ping",server_address="127.0.0.3:27017",status="SUCCESS"} 0
                    mongodb_driver_commands_seconds_bucket{le="1",command="ping",server_address="127.0.0.3:27017",status="SUCCESS"} 0
                    mongodb_driver_commands_seconds_bucket{le="2.5",command="ping",server_address="127.0.0.3:27017",status="SUCCESS"} 0
                    mongodb_driver_commands_seconds_bucket{le="5",command="ping",server_address="127.0.0.3:27017",status="SUCCESS"} 0
                    mongodb_driver_commands_seconds_bucket{le="10",command="ping",server_address="127.0.0.3:27017",status="SUCCESS"} 0
                    mongodb_driver_commands_seconds_bucket{le="+Inf",command="ping",server_address="127.0.0.3:27017",status="SUCCESS"} 21
                    mongodb_driver_commands_seconds_sum{command="ping",server_address="127.0.0.3:27017",status="SUCCESS"} 971000
                    mongodb_driver_commands_seconds_count{command="ping",server_address="127.0.0.3:27017",status="SUCCESS"} 21
                    

                    Usage

                    Add Dependency

                    Add the following dependency to your project to download the package from npm.

                    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.

                    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.

                    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();
                    

                    Configuration

                    The exporter can be configured via properties specified on the optional parameter of type MongoDBDriverExporterOptions.

                    propertyDescriptionExampleSince
                    mongodbDriverCommandsSecondsHistogramBucketsBuckets 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
                    waitQueueSecondsHistogramBucketsBuckets 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
                    defaultLabelsDefault labels added to each metrics.{'foo':'bar', 'alice': 3}1.1.0
                    prefixMetric name prefix for all metrics.'service1_'2.1.0

                    Grafana Dashboard

                    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

                    Changelog

                    The changes to project can be found in the changelog.

                    Compatibility

                    The following table list the compatibility of exporter versions with different MongoDB Driver and prom-client versions.

                    Exporter VersionMongoDB Driver Versionprom-client version
                    ^1.0.0^5.8.0^14.2.0
                    ^2.0.0^6.0.0^15.0.0

                    Contributions

                    Contributions are highly welcome. If you want to contribute to this project please follow the steps described in the contribution guidelines.

                    Projects Using The Exporter

                    If you want to support this project, please add a link to your project and/or company when you use this exporter.

                    Related Projects

                    If you are looking for a way to monitor KafkaJS for Node.js you may have a look at https://github.com/christiangalsterer/kafkajs-prometheus-exporter.

                    If you are looking for a way to monitor node-postgres you may have a look at https://github.com/christiangalsterer/node-postgres-prometheus-exporter.

                    版本与发布记录

                    @christiangalsterer/mongodb-driver-prometheus-exporter 共发布 18 个版本,首次发布于 2023/07/21;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

                    版本发布日期距今
                    3.0.0latest2025/12/1310 个月前
                    2.3.02024/12/141.9 年前
                    2.2.22024/10/052 年前
                    2.2.12024/10/032 年前
                    2.2.02024/05/082 年前
                    2.1.02023/11/192 年前

                    关于 @christiangalsterer/mongodb-driver-prometheus-exporter 的常见问题

                    @christiangalsterer/mongodb-driver-prometheus-exporter 是什么?

                    Prometheus exporter to monitor the MongoDB Node.js driver

                    如何安装 @christiangalsterer/mongodb-driver-prometheus-exporter?

                    在项目目录运行 npm install @christiangalsterer/mongodb-driver-prometheus-exporter,或使用 pnpm add / yarn add / bun add。

                    @christiangalsterer/mongodb-driver-prometheus-exporter 支持 TypeScript 吗?

                    支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

                    @christiangalsterer/mongodb-driver-prometheus-exporter 支持 ESM 还是 CommonJS?

                    以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

                    @christiangalsterer/mongodb-driver-prometheus-exporter 需要什么版本的 Node.js?

                    package.json 的 engines 字段要求 Node.js >=18。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

                    @christiangalsterer/mongodb-driver-prometheus-exporter 可以商用吗?

                    @christiangalsterer/mongodb-driver-prometheus-exporter 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

                    @christiangalsterer/mongodb-driver-prometheus-exporter 的下载量有多少?

                    根据 npm 官方下载统计,@christiangalsterer/mongodb-driver-prometheus-exporter 在 2026/10/10 – 2026/11/08 这 30 天内被下载 9,949 次,日均约 332 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

                    @christiangalsterer/mongodb-driver-prometheus-exporter 还在维护吗?

                    最新版本 3.0.0 发布于 2025/12/13(10 个月前);至今共发布 18 个版本,其中过去 12 个月发布了 1 个;首次发布于 2023/07/21。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。