UNPKG

@christiangalsterer/node-postgres-prometheus-exporter

Version:
54 lines 2.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PgClientPrometheusExporter = void 0; const prom_client_1 = require("prom-client"); const utils_1 = require("./utils"); /** * Exports metrics for the pg.Client module */ class PgClientPrometheusExporter { constructor(client, register, options) { this.defaultOptions = {}; this.PG_CLIENT_ERRORS_TOTAL = 'pg_client_errors_total'; this.PG_CLIENT_DISCONNECTS_TOTAL = 'pg_client_disconnects_total'; this.client = client; this.register = register; this.options = { ...this.defaultOptions, ...options }; const clientErrorsMetric = this.register.getSingleMetric(this.PG_CLIENT_ERRORS_TOTAL); this.clientErrors = clientErrorsMetric instanceof prom_client_1.Counter ? clientErrorsMetric : new prom_client_1.Counter({ name: this.PG_CLIENT_ERRORS_TOTAL, help: 'The total number of connection errors with a database.', labelNames: (0, utils_1.mergeLabelNamesWithStandardLabels)(['host', 'database'], this.options.defaultLabels), registers: [this.register] }); const clientDisconnectsMetric = this.register.getSingleMetric(this.PG_CLIENT_DISCONNECTS_TOTAL); this.clientDisconnects = clientDisconnectsMetric instanceof prom_client_1.Counter ? clientDisconnectsMetric : new prom_client_1.Counter({ name: this.PG_CLIENT_DISCONNECTS_TOTAL, help: 'The total number of disconnected connections.', labelNames: (0, utils_1.mergeLabelNamesWithStandardLabels)(['host', 'database'], this.options.defaultLabels), registers: [this.register] }); } enableMetrics() { this.client.on('error', (error) => { this.onClientError(error); }); this.client.on('end', () => { this.onClientEnd(); }); } onClientError(_error) { this.clientErrors.inc((0, utils_1.mergeLabelsWithStandardLabels)({ host: this.client.host + ':' + this.client.port.toString(), database: this.client.database }, this.options.defaultLabels)); } onClientEnd() { this.clientDisconnects.inc((0, utils_1.mergeLabelsWithStandardLabels)({ host: this.client.host + ':' + this.client.port.toString(), database: this.client.database }, this.options.defaultLabels)); } } exports.PgClientPrometheusExporter = PgClientPrometheusExporter; //# sourceMappingURL=pgClientPrometheusExporter.js.map