UNPKG

@cocalc/database

Version:

CoCalc: code for working with our PostgreSQL database

62 lines 1.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.newHistogram = exports.newGauge = exports.newCounter = void 0; const prom_client_1 = require("prom-client"); const PREFIX = "cocalc_database_"; const cache = {}; function newCounter(name, help, labelNames = []) { const key = `counter-${name}`; if (cache[key] != null) { return cache[key]; } // a prometheus counter -- https://github.com/siimon/prom-client#counter // use it like counter.labels(labelA, labelB).inc([positive number or default is 1]) if (!name.endsWith("_total")) { throw Error(`Counter metric names have to end in [_unit]_total but got '${name}' -- https://prometheus.io/docs/practices/naming/`); } const C = new prom_client_1.Counter({ name: PREFIX + name, help, labelNames, }); cache[key] = C; return C; } exports.newCounter = newCounter; function newGauge(name, help, labelNames = []) { const key = `gauge-${name}`; if (cache[key] != null) { return cache[key]; } const G = new prom_client_1.Gauge({ name: PREFIX + name, help, labelNames, }); cache[key] = G; return G; } exports.newGauge = newGauge; function newHistogram(name, help, config = {}) { const key = `hist-${name}`; if (cache[key] != null) { return cache[key]; } // invoked as histogram.observe(value) if (!config.buckets) { config.buckets = [0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1, 2.5, 5, 10]; } if (!config.labels) { config.labels = []; } const H = new prom_client_1.Histogram({ name: PREFIX + name, help, labelNames: config.labels, buckets: config.buckets, }); cache[key] = H; return H; } exports.newHistogram = newHistogram; //# sourceMappingURL=metrics.js.map