nestjs-hot-shots
Version:
Hot-shots Module for Nest.js Framework
46 lines (45 loc) • 1.81 kB
JavaScript
;
var MetricsService_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MetricsService = void 0;
const tslib_1 = require("tslib");
const common_1 = require("@nestjs/common");
const hot_shots_1 = require("hot-shots");
const collectors_1 = require("./collectors");
let MetricsService = MetricsService_1 = class MetricsService {
constructor(statsd) {
this.statsd = statsd;
this.logger = new common_1.Logger(MetricsService_1.name);
this.cache = new Map();
}
getCounter(name, options) {
return this.getOrCreate(collectors_1.CounterCollector, name, options);
}
getGauge(name, options) {
return this.getOrCreate(collectors_1.GaugeCollector, name, options);
}
getHistogram(name, options) {
return this.getOrCreate(collectors_1.HistogramCollector, name, options);
}
getTiming(name, options) {
return this.getOrCreate(collectors_1.TimingCollector, name, options);
}
getUpDownCounter(name, options) {
return this.getOrCreate(collectors_1.UpDownCounterCollector, name, options);
}
getOrCreate(clazz, name, options) {
const cacheKey = [clazz.name, name].join(':');
if (this.cache.has(cacheKey)) {
this.logger.warn(`Collector ${clazz.name} with name ${name} already exists. Returning cached instance.`);
return this.cache.get(cacheKey);
}
const instance = new clazz(this.statsd, name, options);
this.cache.set(cacheKey, instance);
return instance;
}
};
exports.MetricsService = MetricsService;
exports.MetricsService = MetricsService = MetricsService_1 = tslib_1.__decorate([
(0, common_1.Injectable)(),
tslib_1.__metadata("design:paramtypes", [hot_shots_1.StatsD])
], MetricsService);