@loopback/metrics
Version:
An extension exposes metrics for Prometheus with LoopBack 4
70 lines • 2.39 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.metricsControllerFactory = void 0;
const tslib_1 = require("tslib");
const core_1 = require("@loopback/core");
const rest_1 = require("@loopback/rest");
const prom_client_1 = require("prom-client");
const types_1 = require("../types");
/**
* OpenAPI definition of metrics response
*/
const METRICS_RESPONSE = {
description: 'Metrics Response',
content: {
'text/plain': {
schema: {
type: 'string',
},
},
},
};
/**
* OpenAPI spec for metrics endpoint
*/
const METRICS_SPEC = {
responses: {
'200': METRICS_RESPONSE,
},
};
/**
* OpenAPI spec to hide endpoints
*/
const HIDDEN_SPEC = {
responses: {},
'x-visibility': 'undocumented',
};
function metricsControllerFactory(options = types_1.DEFAULT_METRICS_OPTIONS) {
var _a, _b;
const basePath = (_b = (_a = options.endpoint) === null || _a === void 0 ? void 0 : _a.basePath) !== null && _b !== void 0 ? _b : '/metrics';
const spec = options.openApiSpec ? METRICS_SPEC : HIDDEN_SPEC;
/**
* Controller for metrics endpoint
*/
let MetricsController = class MetricsController {
async report(res) {
// Set the content type from the register
res.contentType(prom_client_1.register.contentType);
const data = await prom_client_1.register.metrics();
res.send(data);
return res;
}
};
tslib_1.__decorate([
(0, rest_1.get)(basePath, spec),
tslib_1.__param(0, (0, core_1.inject)(rest_1.RestBindings.Http.RESPONSE)),
tslib_1.__metadata("design:type", Function),
tslib_1.__metadata("design:paramtypes", [Object]),
tslib_1.__metadata("design:returntype", Promise)
], MetricsController.prototype, "report", null);
MetricsController = tslib_1.__decorate([
(0, core_1.injectable)({ scope: core_1.BindingScope.SINGLETON })
], MetricsController);
return MetricsController;
}
exports.metricsControllerFactory = metricsControllerFactory;
//# sourceMappingURL=metrics.controller.js.map