UNPKG

@christiangalsterer/node-postgres-prometheus-exporter

Version:
169 lines 10.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PgPoolPrometheusExporter = void 0; const prom_client_1 = require("prom-client"); const utils_1 = require("./utils"); /** * Exports metrics for the pg.Pool module */ class PgPoolPrometheusExporter { constructor(pool, register, options) { this.defaultOptions = {}; this.PG_POOL_CONNECTIONS_CREATED_TOTAL = 'pg_pool_connections_created_total'; this.PG_POOL_SIZE = 'pg_pool_size'; this.PG_POOL_MAX = 'pg_pool_max'; this.PG_POOL_ACTIVE_CONNECTIONS = 'pg_pool_active_connections'; this.PG_POOL_WAITING_CONNECTIONS = 'pg_pool_waiting_connections'; this.PG_POOL_IDLE_CONNECTIONS = 'pg_pool_idle_connections'; this.PG_POOL_ERRORS_TOTAL = 'pg_pool_errors_total'; this.PG_POOL_CONNECTIONS_REMOVED_TOTAL = 'pg_pool_connections_removed_total'; this.pool = pool; this.register = register; this.options = { ...this.defaultOptions, ...options }; const poolConnectionsCreatedTotalMetric = this.register.getSingleMetric(this.PG_POOL_CONNECTIONS_CREATED_TOTAL); this.poolConnectionsCreatedTotal = poolConnectionsCreatedTotalMetric instanceof prom_client_1.Counter ? poolConnectionsCreatedTotalMetric : new prom_client_1.Counter({ name: this.PG_POOL_CONNECTIONS_CREATED_TOTAL, help: 'The total number of created connections.', labelNames: (0, utils_1.mergeLabelNamesWithStandardLabels)(['host', 'database'], this.options.defaultLabels), registers: [this.register] }); const poolSizeMetric = this.register.getSingleMetric(this.PG_POOL_SIZE); this.poolSize = poolSizeMetric instanceof prom_client_1.Gauge ? poolSizeMetric : new prom_client_1.Gauge({ name: this.PG_POOL_SIZE, help: 'The current size of the connection pool, including active and idle members.', labelNames: (0, utils_1.mergeLabelNamesWithStandardLabels)(['host', 'database'], this.options.defaultLabels), registers: [this.register] }); const poolSizeMaxMetric = this.register.getSingleMetric(this.PG_POOL_MAX); this.poolSizeMax = poolSizeMaxMetric instanceof prom_client_1.Gauge ? poolSizeMaxMetric : new prom_client_1.Gauge({ name: this.PG_POOL_MAX, help: 'The maximum size of the connection pool.', labelNames: (0, utils_1.mergeLabelNamesWithStandardLabels)(['host', 'database'], this.options.defaultLabels), registers: [this.register], collect: () => { if (this.poolHost != null && this.poolDatabase != null) { this.poolSizeMax.set((0, utils_1.mergeLabelsWithStandardLabels)({ host: this.poolHost + ':' + this.poolPort.toString(), database: this.poolDatabase }, this.options.defaultLabels), this.poolMaxSize); } } }); const poolActiveConnectionsMetric = this.register.getSingleMetric(this.PG_POOL_ACTIVE_CONNECTIONS); this.poolActiveConnections = poolActiveConnectionsMetric instanceof prom_client_1.Gauge ? poolActiveConnectionsMetric : new prom_client_1.Gauge({ name: this.PG_POOL_ACTIVE_CONNECTIONS, help: 'The total number of active connections.', labelNames: (0, utils_1.mergeLabelNamesWithStandardLabels)(['host', 'database'], this.options.defaultLabels), registers: [this.register] }); const poolWaitingConnectionsMetric = this.register.getSingleMetric(this.PG_POOL_WAITING_CONNECTIONS); this.poolWaitingConnections = poolWaitingConnectionsMetric instanceof prom_client_1.Gauge ? poolWaitingConnectionsMetric : new prom_client_1.Gauge({ name: this.PG_POOL_WAITING_CONNECTIONS, help: 'The total number of waiting connections.', labelNames: (0, utils_1.mergeLabelNamesWithStandardLabels)(['host', 'database'], this.options.defaultLabels), registers: [this.register], collect: () => { if (this.poolHost != null && this.poolDatabase != null) { this.poolWaitingConnections.set((0, utils_1.mergeLabelsWithStandardLabels)({ host: this.poolHost + ':' + this.poolPort.toString(), database: this.poolDatabase }, this.options.defaultLabels), this.pool.waitingCount); } } }); const poolIdleConnectionsMetric = this.register.getSingleMetric(this.PG_POOL_IDLE_CONNECTIONS); this.poolIdleConnections = poolIdleConnectionsMetric instanceof prom_client_1.Gauge ? poolIdleConnectionsMetric : new prom_client_1.Gauge({ name: this.PG_POOL_IDLE_CONNECTIONS, help: 'The total number of idle connections.', labelNames: (0, utils_1.mergeLabelNamesWithStandardLabels)(['host', 'database'], this.options.defaultLabels), registers: [this.register], collect: () => { if (this.poolHost != null && this.poolDatabase != null) { this.poolIdleConnections.set((0, utils_1.mergeLabelsWithStandardLabels)({ host: this.poolHost + ':' + this.poolPort.toString(), database: this.poolDatabase }, this.options.defaultLabels), this.pool.idleCount); } } }); const poolErrorsMetric = this.register.getSingleMetric(this.PG_POOL_ERRORS_TOTAL); this.poolErrors = poolErrorsMetric instanceof prom_client_1.Counter ? poolErrorsMetric : new prom_client_1.Counter({ name: this.PG_POOL_ERRORS_TOTAL, help: 'The total number of connection errors with a database.', labelNames: (0, utils_1.mergeLabelNamesWithStandardLabels)(['host', 'database', 'error'], this.options.defaultLabels), registers: [this.register] }); const poolConnectionsRemovedTotalMetric = this.register.getSingleMetric(this.PG_POOL_CONNECTIONS_REMOVED_TOTAL); this.poolConnectionsRemovedTotal = poolConnectionsRemovedTotalMetric instanceof prom_client_1.Counter ? poolConnectionsRemovedTotalMetric : new prom_client_1.Counter({ name: this.PG_POOL_CONNECTIONS_REMOVED_TOTAL, help: 'The total number of removed connections.', labelNames: (0, utils_1.mergeLabelNamesWithStandardLabels)(['host', 'database'], this.options.defaultLabels), registers: [this.register] }); this.poolMaxSize = (0, utils_1.getMaxPoolSize)(pool); this.poolHost = (0, utils_1.getHost)(pool); this.poolPort = (0, utils_1.getPort)(pool); this.poolDatabase = (0, utils_1.getDatabase)(pool); } enableMetrics() { this.pool.on('connect', (client) => { this.onPoolConnect(client); }); this.pool.on('acquire', (client) => { this.onPoolConnectionAcquired(client); }); this.pool.on('error', (error) => { this.onPoolError(error); }); this.pool.on('release', (error, client) => { this.onPoolConnectionReleased(error, client); }); this.pool.on('remove', (client) => { this.onPoolConnectionRemoved(client); }); } onPoolConnect(client) { if (this.poolHost != null && this.poolDatabase != null) { this.poolConnectionsCreatedTotal.inc((0, utils_1.mergeLabelsWithStandardLabels)({ host: this.poolHost + ':' + this.poolPort.toString(), database: this.poolDatabase }, this.options.defaultLabels)); this.poolSize.inc((0, utils_1.mergeLabelsWithStandardLabels)({ host: this.poolHost + ':' + this.poolPort.toString(), database: this.poolDatabase }, this.options.defaultLabels)); } } onPoolConnectionAcquired(client) { if (this.poolHost != null && this.poolDatabase != null) { this.poolActiveConnections.inc((0, utils_1.mergeLabelsWithStandardLabels)({ host: this.poolHost + ':' + this.poolPort.toString(), database: this.poolDatabase }, this.options.defaultLabels)); } } onPoolError(error) { if (this.poolHost != null && this.poolDatabase != null) { this.poolErrors.inc((0, utils_1.mergeLabelsWithStandardLabels)({ host: this.poolHost + ':' + this.poolPort.toString(), database: this.poolDatabase, error: error.message }, this.options.defaultLabels)); } } onPoolConnectionReleased(_error, client) { if (this.poolHost != null && this.poolDatabase != null) { this.poolActiveConnections.dec((0, utils_1.mergeLabelsWithStandardLabels)({ host: this.poolHost + ':' + this.poolPort.toString(), database: this.poolDatabase }, this.options.defaultLabels)); } } onPoolConnectionRemoved(client) { if (this.poolHost != null && this.poolDatabase != null) { this.poolSize.dec((0, utils_1.mergeLabelsWithStandardLabels)({ host: this.poolHost + ':' + this.poolPort.toString(), database: this.poolDatabase }, this.options.defaultLabels)); this.poolConnectionsRemovedTotal.inc((0, utils_1.mergeLabelsWithStandardLabels)({ host: this.poolHost + ':' + this.poolPort.toString(), database: this.poolDatabase }, this.options.defaultLabels)); } } } exports.PgPoolPrometheusExporter = PgPoolPrometheusExporter; //# sourceMappingURL=pgPoolPrometheusExporter.js.map