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

Requesting to be passed the original Fastify request object in callback #17

Open
mjpowersjr opened this issue Jan 24, 2023 · 2 comments · May be fixed by #43
Open

Requesting to be passed the original Fastify request object in callback #17

mjpowersjr opened this issue Jan 24, 2023 · 2 comments · May be fixed by #43
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@mjpowersjr
Copy link

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 time
    reply.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
// logging
fastifyInstance.addHook('onResponse', (req: FastifyRequest, res: FastifyReply) => {
    if (req.url === healthEndpoint) {
        logger.info({
            req,
            res
        });
    } else {
        logger.info({
            req,
            res
        });
    }
});
@mjpowersjr mjpowersjr added the enhancement New feature or request label Jan 24, 2023
@metcoder95
Copy link
Owner

metcoder95 commented Jan 24, 2023

Hey! Thanks for the issue, and the feedback! 🙂

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) => {
    const signal = req.race(function (evt) {
        const result = 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) => {
  const aborted = req.race().aborted;

  if (aborted) req.log.info('request aborted');
  else req.log.info('request finished');

  // Remember to call done to allow fastify continue its lifecycle
  done(null, payload)
})

PRs are always welcomed 😄

@metcoder95
Copy link
Owner

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 🙂

@metcoder95 metcoder95 added the good first issue Good for newcomers label May 9, 2023
@metcoder95 metcoder95 linked a pull request Dec 28, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants