UNPKG

zamza

Version:

Apache Kafka discovery, indexing, searches, storage, hooks and HTTP gateway

80 lines (79 loc) 2.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const Debug = require("debug"); const promClient = require("prom-client"); const debug = Debug("zamza:metrics"); const promDefaultMetrics = promClient.collectDefaultMetrics; const promRegistry = promClient.Registry; class Metrics { constructor(prefix = "") { this.prefix = prefix; this.register = new promRegistry(); this.metrics = {}; // Stores metric objects } exportType() { return this.register.contentType; } exportMetrics() { return this.register.metrics(); } getRegister() { return this.register; } getCounter(key) { if (this.metrics[key]) { return this.metrics[key]; } this.metrics[key] = new promClient.Counter({ name: `${key}`, help: `${key}_help`, registers: [this.register], labelNames: ["na", "prefix"], }); return this.metrics[key]; } getGauge(key) { // prefix key = `${key}_gauge`; if (this.metrics[key]) { return this.metrics[key]; } this.metrics[key] = new promClient.Gauge({ name: `${key}`, help: `${key}_help`, registers: [this.register], labelNames: ["na", "prefix"], }); return this.metrics[key]; } inc(key, val = 1) { const prefix = this.prefix; const fullKey = prefix ? `${prefix}_${key}` : key; const counter = this.getCounter(fullKey); counter.inc({ prefix }, val, Date.now()); } set(key, val) { if (val === null || val === undefined) { throw new Error(`Please provide value on set ${key}`); } const prefix = this.prefix; const fullKey = prefix ? `${prefix}_${key}` : key; const gauge = this.getGauge(fullKey); gauge.set({ prefix }, val, Date.now()); } registerDefault() { this.defaultMetricsIntv = promDefaultMetrics({ register: this.register, timeout: 5000, }); debug("default metrics active."); } close() { if (this.defaultMetricsIntv) { clearInterval(this.defaultMetricsIntv); } this.metrics = {}; this.register.clear(); } } exports.Metrics = Metrics;