You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Feature Request:
Pass back the original Fastify request when invoking the callback.
Details:
I'd like to use a single default onRequestClosed callback to handle all aborted requests. When this callback is invoked, I'd like to have access to the underlying Fastify request object if possible.
Alternatively, maybe there is a way to use this plugin in combination with Fastify's built-in request lifecycle hooks to achieve a similar result. (If this works, maybe a quick example in the docs would be helpful to others heading down a similar path)
Background
Fastify provides separate request/response logging out-of-the-box. In our environment, we found by combining the request / response logs - troubleshooting issues based on log data was much easier. Overall this approach works well, but I recently discovered that our current approach will not log request information during a client abort. (This is how I eventually discovered fastify-racing plugin....thanks!)
Our current logging setup:
fastifyInstance.addHook("onRequest",(req,reply,done)=>{// track overall response timereply.startTime=Date.now();done();});// use hook based logging instead of internal logger to provide a single// combined log message per-request, instead of the default request/reply// loggingfastifyInstance.addHook('onResponse',(req: FastifyRequest,res: FastifyReply)=>{if(req.url===healthEndpoint){logger.info({
req,
res
});}else{logger.info({
req,
res
});}});
The text was updated successfully, but these errors were encountered:
I don't see an issue with having the context of the request injected in the callback. But rather than injecting it through the parameters of the callback, set the request object as the callback this context, similar to:
app.get('/',(req,reply)=>{constsignal=req.race(function(evt){constresult=result.type==='aborted' ? '' : `${result}-world`this.log.info({ result },'Request result');reply.send(result)})})
Subsequently, you can also take advantage of the onSend hook, which will always be called, regardless of whether the request has been aborted or not.
You can differentiate when aborted or when not by checking req.race().aborted.
e.g.
app.addHook('onSend',(req,reply,payload,done)=>{constaborted=req.race().aborted;if(aborted)req.log.info('request aborted');elsereq.log.info('request finished');// Remember to call done to allow fastify continue its lifecycledone(null,payload)})
Hey @mjpowersjr, fastify is about to get support to the hook you're looking for soon, take a look at fastify#4582.
This plugin will still useful if you want to take certain action just at the moment the request is closed, but for the purpose you're looking for, the new hook makes a better fit 🙂
Feature Request:
Pass back the original Fastify request when invoking the callback.
Details:
I'd like to use a single default
onRequestClosed
callback to handle all aborted requests. When this callback is invoked, I'd like to have access to the underlying Fastify request object if possible.Alternatively, maybe there is a way to use this plugin in combination with Fastify's built-in request lifecycle hooks to achieve a similar result. (If this works, maybe a quick example in the docs would be helpful to others heading down a similar path)
Background
Fastify provides separate request/response logging out-of-the-box. In our environment, we found by combining the request / response logs - troubleshooting issues based on log data was much easier. Overall this approach works well, but I recently discovered that our current approach will not log request information during a client abort. (This is how I eventually discovered fastify-racing plugin....thanks!)
Our current logging setup:
The text was updated successfully, but these errors were encountered: