fix: handle errors from prom-client

This commit is contained in:
Chris West (Faux)
2021-02-19 08:32:48 +00:00
parent 2478e617bb
commit 99d8fc1ea9
2 changed files with 28 additions and 4 deletions

View File

@@ -409,6 +409,27 @@ describe('index', () => {
});
});
it('handles errors in collectors', done => {
const app = express();
const instance = bundle({});
app.use(instance);
spyOn(console, 'error'); // mute console.error
new promClient.Gauge({
name: 'kaboom',
help: 'this metric explodes',
collect() {
throw new Error('kaboom!');
}
});
supertest(app)
.get('/metrics')
.expect(500)
.end((err) => done(err));
});
it('customLabels={foo: "bar"} adds foo="bar" label to metrics', done => {
const app = express();
const instance = bundle({

View File

@@ -140,17 +140,20 @@ function main(opts) {
}
const metricsMiddleware = function(req, res, next) {
res.writeHead(200, {'Content-Type': 'text/plain'});
const sendSuccesss = (output) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(output);
};
const metricsResponse = opts.promRegistry.metrics();
// starting from prom-client@13 .metrics() returns a Promise
if (metricsResponse.then) {
metricsResponse
.then(output => res.end(output))
.then(output => sendSuccesss(output))
.catch(err => next(err));
} else {
// compatibility fallback for previous versions of prom-client@<=12
res.end(metricsResponse);
sendSuccesss(metricsResponse);
}
};
@@ -161,7 +164,7 @@ function main(opts) {
const path = req.originalUrl || req.url; // originalUrl gets lost in koa-connect?
if (opts.autoregister && path.match(metricsMatch)) {
return metricsMiddleware(req, res);
return metricsMiddleware(req, res, next);
}
// bypass() is checked only after /metrics was processed