-
Notifications
You must be signed in to change notification settings - Fork 44
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
Simplified prom route when using default express route #113
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
'use strict'; | ||
const { expect } = require('chai'); | ||
const Prometheus = require('prom-client'); | ||
const supertest = require('supertest'); | ||
|
||
let app, config; | ||
|
||
describe('when using express framework (default route)', function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I made a new test server in this PR because the default route (wildcard In the existing tests, routes always call next() and the test logic relies on this for the error cases. Open to ideas on how to better integrate this with existing tests. |
||
this.timeout(4000); | ||
before(() => { | ||
config = require('./server/config'); | ||
config.useUniqueHistogramName = true; | ||
app = require('./server/express-server-default-route'); | ||
}); | ||
after(() => { | ||
Prometheus.register.clear(); | ||
delete require.cache[require.resolve('./server/express-server-default-route.js')]; | ||
delete require.cache[require.resolve('../../../src/index.js')]; | ||
delete require.cache[require.resolve('../../../src/metrics-middleware.js')]; | ||
}); | ||
|
||
describe('when start up with default route', () => { | ||
describe('when calling a GET endpoint', () => { | ||
before(() => { | ||
return supertest(app) | ||
.get('/hello') | ||
.expect(200) | ||
.then((res) => {}); | ||
}); | ||
it('should add it to the histogram', () => { | ||
return supertest(app) | ||
.get('/metrics') | ||
.expect(200) | ||
.then((res) => { | ||
expect(res.text).to.contain('method="GET",route="*",code="200"'); | ||
}); | ||
}); | ||
}); | ||
describe('when calling a GET endpoint with one query param', () => { | ||
before(() => { | ||
return supertest(app) | ||
.get('/hello?test=test') | ||
.expect(200) | ||
.then((res) => {}); | ||
}); | ||
it('should add it to the histogram', () => { | ||
return supertest(app) | ||
.get('/metrics') | ||
.expect(200) | ||
.then((res) => { | ||
expect(res.text).to.contain('method="GET",route="*",code="200"'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
'use strict'; | ||
|
||
const express = require('express'); | ||
const middleware = require('../../../../src/index.js').expressMiddleware; | ||
|
||
const app = express(); | ||
app.use(middleware({ includeQueryParams: true })); | ||
|
||
app.all('*', (req, res, next) => { | ||
res.status(200); | ||
res.json({ message: 'Hello World!' }); | ||
}); | ||
|
||
// Error handler | ||
app.use((err, req, res, next) => { | ||
res.statusCode = 500; | ||
// Do not expose your error in production | ||
res.json({ error: err.message }); | ||
}); | ||
|
||
module.exports = app; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are three cases we need to worry about the default route.
:0*
(ln82)Because this affected so many branches, I opted for a short-circuit. Alternatives and suggestions welcome.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as it seems we're missing support
*
in general.according to
express
documentationand also regex support, which we can ignore for now.
shouldn't we support all routes with
*
instead?the main issue here is that we'd need to also support
*
with request params. i.ethis isn't a must for now, but would have been a more complete solution.
lmk what you think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd be interested to pursue the general solution. If somebody has an answer to this question, I'll have a better understanding of the requirements and can take a stab at it. Without that, I'm worried I'm going to break something.
I'll install Nest and play around if I don't heard back.