@christiangalsterer/node-postgres-prometheus-exporter

中文文档
1.4.0发布于 2025/11/27MITTypeScriptCommonJS近 30 天下载 6.5万

A prometheus exporter for node-postgres

原始使用文档
文档目录9 节
  1. Available Metrics
  2. pg.Client Metrics
  3. pg.Pool Metrics
  4. Example Output
  5. Add Dependency
  6. TypeScript
  7. JavaScript
  8. PgClientExporterOptions
  9. PgPoolExporterOptions

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

Prometheus Exporter for node-postgres

A prometheus exporter exposing metrics for node-postgres.

Available Metrics

The exporter provides the following metrics.

pg.Client Metrics

Metric NameDescriptionLabelsSince
pg_client_errors_totalThe total number of connection errors with a database
  • host: The host of the database.
  • database: The database name
    1.0.0
    pg_client_disconnects_totalThe total number of disconnected connections
    • host: The host of the database.
    • database: The database name
      1.0.0

      pg.Pool Metrics

      Metric NameDescriptionLabelsSince
      pg_pool_connections_created_totalThe total number of created connections
      • host: The host of the database.
      • database: The database name
        1.0.0
        pg_pool_sizeThe current size of the connection pool, including active and idle members
        • host: The host of the database.
        • database: The database name
          1.0.0
          pg_pool_active_connectionsThe total number of active connections
          • host: The host of the database.
          • database: The database name
            1.0.0
            pg_pool_errors_totalThe total number of connection errors with a database
            • host: The host of the database.
            • database: The database name
              1.0.0
              pg_pool_connections_removed_totalThe total number of removed connections
              • host: The host of the database.
              • database: The database name
                1.0.0

                Example Output

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

                # HELP pg_client_errors_total The total number of connection errors with a database.
                # TYPE pg_client_errors_total counter
                pg_client_errors_total{host="localhost:5432",database="node_postgres_test1"} 1
                
                # HELP pg_client_disconnects_total The total number of disconnected connections.
                # TYPE pg_client_disconnects_total counter
                pg_client_disconnects_total{host="localhost:5432",database="node_postgres_test1"} 1
                
                # HELP pg_pool_connections_created_total The total number of created connections.
                # TYPE pg_pool_connections_created_total counter
                pg_pool_connections_created_total{host="localhost:5432",database="node_postgres_test1"} 19
                
                # HELP pg_pool_size The current size of the connection pool, including active and idle members.
                # TYPE pg_pool_size gauge
                pg_pool_size{host="localhost:5432",database="node_postgres_test1"} 10
                
                # HELP pg_pool_max The maximum size of the connection pool.
                # TYPE pg_pool_max gauge
                pg_pool_max{host="localhost:5432",database="node_postgres_test1"} 10
                
                # HELP pg_pool_active_connections The total number of active connections.
                # TYPE pg_pool_active_connections gauge
                pg_pool_active_connections{host="localhost:5432",database="node_postgres_test1"} 10
                
                # HELP pg_pool_waiting_connections The total number of waiting connections.
                # TYPE pg_pool_waiting_connections gauge
                pg_pool_waiting_connections{host="localhost:5432",database="node_postgres_test1"} 1
                
                # HELP pg_pool_idle_connections The total number of idle connections.
                # TYPE pg_pool_idle_connections gauge
                pg_pool_idle_connections{host="localhost:5432",database="node_postgres_test1"} 0
                
                # HELP pg_pool_errors_total The total number of connection errors with a database.
                # TYPE pg_pool_errors_total counter
                pg_pool_errors_total{host="localhost:5432",database="node_postgres_test1"} 1
                
                # HELP pg_pool_connections_removed_total The total number of removed connections.
                # TYPE pg_pool_connections_removed_total counter
                pg_pool_connections_removed_total{host="localhost:5432",database="node_postgres_test1"} 9
                

                Usage

                Add Dependency

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

                npm i @christiangalsterer/node-postgres-prometheus-exporter
                

                TypeScript

                The following example illustrates how to use the exporter to enable monitoring for the node-postgres.

                import { Client, Pool } from 'pg'
                import { Registry, collectDefaultMetrics } from 'prom-client'
                import { monitorPgClient, monitorPgPool } from '@christiangalsterer/node-postgres-prometheus-exporter'
                
                ...
                
                // set up a pg.Client
                const client = new Client()
                
                // set up a pg.Pool
                const pool = new Pool()
                
                // set up the prometheus client
                const register = new Registry();
                collectDefaultMetrics({ register })
                
                // monitor the pg.Client
                monitorPgClient(client, register)
                
                // monitor the pg.Pool
                monitorPgPool(pool, register)
                
                ...
                
                // connect to PostgreSQL *after* calling monitorPgClient() / monitorPgPool()
                await client.connect()
                await pool.connect()
                

                JavaScript

                The following example illustrates how to use the exporter to enable monitoring for node-postgres.

                const pg = require('pg')
                const promClient = require( 'prom-client');
                const postgresExporter = require('@christiangalsterer/node-postgres-prometheus-exporter')
                
                // set up a pg.Client
                const client = new pg.Client()
                
                // set up a pg.Pool
                const pool = new pg.Pool()
                
                // set up the prometheus client
                const collectDefaultMetrics = promClient.collectDefaultMetrics;
                const Registry = promClient.Registry;
                const register = new Registry();
                collectDefaultMetrics({ register });
                
                // monitor the pg.Client
                postgresExporter.monitorPgClient(client, register)
                
                // monitor the pg.Pool
                postgresExporter.monitorPgPool(pool, register)
                
                // connect to Postgres *after* calling monitorPgClient() / monitorPgPool()
                await client.connect()
                await pool.connect()
                

                Configuration

                The exporter can be configured via properties specified on the optional parameter of type PgClientExporterOptions and PgPoolExporterOptions respectively.

                PgClientExporterOptions

                propertyDescriptionExampleSince
                defaultLabelsDefault labels added to each metrics.{'foo':'bar', 'alice': 3}1.0.0

                PgPoolExporterOptions

                propertyDescriptionExampleSince
                defaultLabelsDefault labels added to each metrics.{'foo':'bar', 'alice': 3}1.0.0

                Grafana Dashboard

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

                Here an example for node-postgres client metrics: Grafana:node-postgres Client Metrics

                Here an example for node-postgres pool metrics: Grafana:node-postgres Pool Metrics

                Changelog

                The changes to project can be found in the changelog.

                Compatibility

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

                Exporter Versionnode-postgres Versionprom-client version
                ^1.0.0^8.11.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 your MongoDB Driver for Node.js you may have a look at https://github.com/christiangalsterer/mongodb-driver-prometheus-exporter.

                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.

                版本与发布记录

                @christiangalsterer/node-postgres-prometheus-exporter 共发布 6 个版本,首次发布于 2024/01/18;过去 12 个月发布了 1 个版本。

                版本发布日期距今
                1.4.0latest2025/11/2711 个月前
                1.3.12025/05/191.5 年前
                1.3.02024/07/212 年前
                1.2.02024/04/212 年前
                1.1.02024/04/192 年前
                1.0.02024/01/182 年前

                关于 @christiangalsterer/node-postgres-prometheus-exporter 的常见问题

                @christiangalsterer/node-postgres-prometheus-exporter 是什么?

                A prometheus exporter for node-postgres

                如何安装 @christiangalsterer/node-postgres-prometheus-exporter?

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

                @christiangalsterer/node-postgres-prometheus-exporter 支持 TypeScript 吗?

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

                @christiangalsterer/node-postgres-prometheus-exporter 支持 ESM 还是 CommonJS?

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

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

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

                @christiangalsterer/node-postgres-prometheus-exporter 可以商用吗?

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

                @christiangalsterer/node-postgres-prometheus-exporter 的下载量有多少?

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

                @christiangalsterer/node-postgres-prometheus-exporter 还在维护吗?

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