unleash-server
Version:
Unleash is an enterprise ready feature flag service. It provides different strategies for handling feature flags.
81 lines • 3.06 kB
JavaScript
import Controller from '../../../routes/controller.js';
import { NONE } from '../../../types/permissions.js';
import { emptyResponse } from '../../../openapi/util/standard-responses.js';
export default class CustomMetricsController extends Controller {
constructor({ customMetricsService, openApiService, }, config) {
super(config);
const { getLogger } = config;
this.logger = getLogger('/admin-api/custom-metrics');
this.openApiService = openApiService;
this.customMetricsService = customMetricsService;
this.route({
method: 'get',
path: '',
handler: this.getCustomMetrics,
permission: NONE,
middleware: [
this.openApiService.validPath({
tags: ['Metrics'],
summary: 'Get stored custom metrics',
description: `Retrieves the stored custom metrics data.`,
operationId: 'getCustomMetrics',
responses: {
200: emptyResponse,
},
}),
],
});
this.route({
method: 'get',
path: '/prometheus',
handler: this.getPrometheusMetrics,
permission: NONE,
middleware: [
this.openApiService.validPath({
tags: ['Metrics'],
summary: 'Get metrics in Prometheus format',
description: `Exposes all custom metrics in Prometheus text format for scraping.`,
operationId: 'getPrometheusMetrics',
responses: {
200: {
description: 'Prometheus formatted metrics',
content: {
'text/plain': {
schema: {
type: 'string',
},
},
},
},
},
}),
],
});
}
async getCustomMetrics(_req, res) {
try {
const allMetrics = this.customMetricsService.getMetrics();
res.json({
metrics: allMetrics,
count: allMetrics.length,
metricNames: this.customMetricsService.getMetricNames(),
});
}
catch (e) {
this.logger.error('Error retrieving custom metrics', e);
res.status(500).end();
}
}
async getPrometheusMetrics(_req, res) {
try {
const output = this.customMetricsService.getPrometheusMetrics();
res.set('Content-Type', 'text/plain');
res.send(output);
}
catch (e) {
this.logger.error('Error generating Prometheus metrics', e);
res.status(500).end();
}
}
}
//# sourceMappingURL=custom-metrics-controller.js.map