UNPKG

nestjs-metrics-reporter

Version:

A global static Prometheus metrics reporter for NestJS applications

105 lines 4.32 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MetricsService = void 0; const common_1 = require("@nestjs/common"); const prom_client_1 = require("prom-client"); const constants_1 = require("../constants"); let MetricsService = class MetricsService { constructor(registry, config) { this.registry = registry; this.config = config; this.counter = {}; this.gauge = {}; this.histogram = {}; this.summary = {}; if (this.config.pushgatewayUrl) { this.pushgateway = new prom_client_1.Pushgateway(this.config.pushgatewayUrl, this.config.pushgatewayOptions || [], this.registry); } } incCounter(key, labels, value = 1) { if (!this.counter[key]) { this.counter[key] = new prom_client_1.Counter({ name: key, help: `Counter for ${key}`, labelNames: labels ? Object.keys(labels) : [], registers: [this.registry], }); } this.counter[key].inc(labels || {}, value); } setGauge(key, value, labels) { if (!this.gauge[key]) { this.gauge[key] = new prom_client_1.Gauge({ name: key, help: `Gauge for ${key}`, labelNames: labels ? Object.keys(labels) : [], registers: [this.registry], }); } this.gauge[key].set(labels || {}, value); } observeHistogram(key, value, labels, buckets) { if (!this.histogram[key]) { this.histogram[key] = new prom_client_1.Histogram({ name: key, help: `Histogram for ${key}`, labelNames: labels ? Object.keys(labels) : [], buckets: buckets || [0.1, 0.5, 1, 2, 5], registers: [this.registry], }); } this.histogram[key].observe(labels || {}, value); } observeSummary(key, value, labels, percentiles) { if (!this.summary[key]) { this.summary[key] = new prom_client_1.Summary({ name: key, help: `Summary for ${key}`, labelNames: labels ? Object.keys(labels) : [], percentiles: percentiles || [0.01, 0.05, 0.5, 0.9, 0.95, 0.99], registers: [this.registry], }); } this.summary[key].observe(labels || {}, value); } async pushMetrics(jobName) { if (!this.pushgateway) { return { status: 400, success: false, message: 'Pushgateway is not configured' }; } try { await this.pushgateway.pushAdd({ jobName }); return { status: 200, success: true }; } catch (error) { return { status: 500, success: false, message: error instanceof Error ? error.message : String(error) }; } } }; exports.MetricsService = MetricsService; exports.MetricsService = MetricsService = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Inject)(prom_client_1.Registry)), __param(1, (0, common_1.Inject)(constants_1.CONFIG_OPTIONS)), __metadata("design:paramtypes", [prom_client_1.Registry, Object]) ], MetricsService); //# sourceMappingURL=metrics.service.js.map