@loopback/metrics
Version:
An extension exposes metrics for Prometheus with LoopBack 4
53 lines • 2.04 kB
JavaScript
// Copyright IBM Corp. and LoopBack contributors 2019. All Rights Reserved.
// Node module: @loopback/metrics
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
Object.defineProperty(exports, "__esModule", { value: true });
exports.MetricsPushObserver = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@loopback/core");
const prom_client_1 = require("prom-client");
const keys_1 = require("../keys");
const types_1 = require("../types");
/**
* An observer to set up periodical push of metrics to a push gateway
*/
let MetricsPushObserver = class MetricsPushObserver {
constructor(options = types_1.DEFAULT_METRICS_OPTIONS) {
this.options = options;
}
start() {
var _a;
const gwConfig = this.options.pushGateway;
if (!gwConfig)
return;
this.gateway = new prom_client_1.Pushgateway(gwConfig.url);
// eslint-disable-next-line @typescript-eslint/no-misused-promises
this.interval = setInterval(async () => {
var _a;
const params = {
jobName: (_a = gwConfig.jobName) !== null && _a !== void 0 ? _a : 'loopback',
groupings: gwConfig.groupingKey,
};
if (gwConfig.replaceAll) {
await this.gateway.push(params);
}
else {
await this.gateway.pushAdd(params);
}
}, (_a = gwConfig.interval) !== null && _a !== void 0 ? _a : 5000);
}
stop() {
if (this.interval) {
clearInterval(this.interval);
this.interval = undefined;
}
}
};
exports.MetricsPushObserver = MetricsPushObserver;
exports.MetricsPushObserver = MetricsPushObserver = tslib_1.__decorate([
tslib_1.__param(0, (0, core_1.config)({ fromBinding: keys_1.MetricsBindings.COMPONENT })),
tslib_1.__metadata("design:paramtypes", [Object])
], MetricsPushObserver);
//# sourceMappingURL=metrics.push.observer.js.map
;