Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update express-middleware.js #62

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/express-middleware.js
Expand Up @@ -85,7 +85,7 @@ class ExpressMiddleware {
return route;
}

middleware(req, res, next) {
async middleware(req, res, next) {
if (!this.setupOptions.server && req.socket) {
this.setupOptions.server = req.socket.server;
this._collectDefaultServerMetrics(this.setupOptions.defaultMetricsInterval);
Expand All @@ -96,11 +96,13 @@ class ExpressMiddleware {
if (routeUrl === this.setupOptions.metricsRoute) {
debug('Request to /metrics endpoint');
res.set('Content-Type', Prometheus.register.contentType);
return res.end(Prometheus.register.metrics());
const promResponse = await Prometheus.register.metrics()
return res.end(promResponse);
}
if (routeUrl === `${this.setupOptions.metricsRoute}.json`) {
debug('Request to /metrics endpoint');
return res.json(Prometheus.register.getMetricsAsJSON());
const promResponse = await Prometheus.register.getMetricsAsJSON()
return res.json(promResponse);
}

req.metrics = {
Expand Down
20 changes: 12 additions & 8 deletions test/unit-test/metric-middleware-test.js
Expand Up @@ -378,14 +378,18 @@ describe('metrics-middleware', () => {
end: end,
set: set
});
sinon.assert.calledOnce(end);
// eslint-disable-next-line no-control-regex
const endFormalized = end.getCall(0).args[0].replace(/ ([0-9]*[.])?[0-9]+[\x0a]/g, ' #num\n');
// eslint-disable-next-line no-control-regex
const apiFormalized = Prometheus.register.metrics().replace(/ ([0-9]*[.])?[0-9]+[\x0a]/g, ' #num\n');
expect(endFormalized).to.eql(apiFormalized);
sinon.assert.calledWith(set, 'Content-Type', Prometheus.register.contentType);
sinon.assert.calledOnce(set);

setTimeout(() => {
sinon.assert.calledOnce(end);
// eslint-disable-next-line no-control-regex
const endFormalized = end.getCall(0).args[0].replace(/ ([0-9]*[.])?[0-9]+[\x0a]/g, ' #num\n');
// eslint-disable-next-line no-control-regex
let apiFormalized = Prometheus.register.metrics()
apiFormalized = apiFormalized.replace(/ ([0-9]*[.])?[0-9]+[\x0a]/g, ' #num\n');
expect(endFormalized).to.eql(apiFormalized);
sinon.assert.calledWith(set, 'Content-Type', Prometheus.register.contentType);
sinon.assert.calledOnce(set);
})
});
after(() => {
Prometheus.register.clear();
Expand Down