-
Notifications
You must be signed in to change notification settings - Fork 24
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
Enable calculating id based on http request #47
Conversation
@tan-tan-kanarek thanks for your contribution. Could you explain the use case behind this addition? |
I will share my use case, but I'm sure there are many other similar use cases.
|
I see. Let me review the changes. |
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.
Could you also add tests to verify this change?
Do you need any advice on tests? I see some changes, but they're not adding tests. |
Thank you, advice on tests will be appreciated. |
It could be something like the following test: test('calls request id factory with req', () => {
const app = express()
const idFactory = (req) => req
app.use(rTracer.expressMiddleware({
requestIdFactory: idFactory
}))
app.get('/test', (req, res) => {
if (req === rTracer.id()) {
res.end()
} else {
res.status(500).end()
}
})
return request(app).get('/test')
.then(res => {
expect(res.statusCode).toBe(200)
})
}) One more thing to notice here is that the suggested implementation always passed request object into |
@@ -96,7 +96,7 @@ const fastifyPlugin = (fastify, options, next) => { | |||
if (useFastifyRequestId) { | |||
requestId = requestId || request.id | |||
} | |||
requestId = requestId || requestIdFactory() | |||
requestId = requestId || (requestIdFactory ? requestIdFactory(request.raw) : uuidv1()) |
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.
requestId = requestId || (requestIdFactory ? requestIdFactory(request.raw) : uuidv1()) | |
requestId = requestId || (requestIdFactory ? requestIdFactory(request) : uuidv1()) |
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.
The raw contains all the info one might need from the request and will be compatible between all versions of fastify.
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.
Other non-Express middlewares and plugins from this PR provide framework specific request objects, so having request.raw
only in Fastify middleware doesn't seem to be consistent. Also, frameworks usually expose convenient APIs in their request wrapper objects, so to me it makes more sense to allow user to deal with the familiar API (which is framework's request wrapper).
Closing this one in favor of #48 |
Refs: #25