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

[BUG] Request context not working when using FastifyAdapter #546

Closed
itheodoro opened this issue Sep 13, 2021 · 25 comments
Closed

[BUG] Request context not working when using FastifyAdapter #546

itheodoro opened this issue Sep 13, 2021 · 25 comments
Assignees
Labels
bug Something isn't working

Comments

@itheodoro
Copy link

[x] I've read the docs of nestjs-pino

[x] I've read the docs of pino

[x] I couldn't find the same open issue of nestjs-pino

What is the current behavior?
I'm using NestJS with Fastify. nestjs-pino works pretty well with HTTP GET requests and with other HTTP requests that doesn't have a body. When requests have a body, the logger does not brings context and only prints the basic log message, even when log is invoked inside the request context.

What is the expected behavior?
When logging inside a request context, all log entries should have the correct context.

  • Correct log entry for request without body:
{"level":30,"time":1631560507217,"pid":448510,"hostname":"host","req":{"id":"req-1","method":"GET","url":"/","headers":{"content-type":"application/json","accept":"*/*","cache-control":"no-cache","host":"localhost:5000","accept-encoding":"gzip, deflate, br","connection":"keep-alive","content-length":"20"},"remoteAddress":"127.0.0.1","remotePort":35350},"context":"AppController","msg":"Hi from getHello"}
  • Incorrect log entry for request with body:
{"level":30,"time":1631560533607,"pid":448510,"hostname":"host","context":"AppController","msg":"Hi from postHello"}

Please provide minimal example repo. Without it this issue will be closed
Example repo with log in app.controller.ts

Please mention other relevant information such as Node.js version and Operating System.
Node.js version: v14.17.6
OS: Ubuntu 20.04.3 LTS
nestjs-pino version: ^2.2.0
@nestjs/platform-fastify version: ^8.0.6

Important to mention that the wrong behavior doesn't occurs when using Nest with Express.
I also noticed a possible fix, but I don't know if it could cause some issue since I'm not very familiar with AsyncLocalStorage. The possible fix consists in replace AsyncLocalStorage run by AsyncLocalStorage enterWith method in LoggerModule.

Thanks for creating such a helpful package! 🙏

@itheodoro itheodoro added the bug Something isn't working label Sep 13, 2021
@iamolegga
Copy link
Owner

@itheodoro hi, have you tried to play with routing params

WARNING
The fastify package uses the latest version of the path-to-regexp package, which no longer supports wildcard asterisks . Instead, you must use parameters (e.g., (.), :splat*).

looks like the problem is that nest adapters use different routing libs, and it doesn't provide any clean way to detect which adapter is used currently to set proper default routing params, so here some basic routing params are set that works for both express and fastify in tests

@itheodoro
Copy link
Author

Hi @iamolegga, thanks for your reply.

I tried to configure routing params to match all available routes and got the same results as before. Is this what you suggest or I misunderstood something?

LoggerModule.forRoot({
    forRoutes: [
        {
            path: '(.*)',
            method: RequestMethod.ALL,
        },
    ],
})

With the above code, this log is also displayed without req object. On the other hand, the automatic request log (the one that displays res object) is still working fine and including the correct context.

@iamolegga
Copy link
Owner

interesting, will look deeper into this

@itheodoro
Copy link
Author

Thanks! Let me know if I can help with something.

Just another behavior that may help is that PinoLogger.assign method throws an error with the following message in the same scenario: PinoLogger: unable to assign extra fields out of request scope.

@sgrigorev
Copy link

Hi! Do you have any updates regarding the issue?

@iamolegga
Copy link
Owner

Sorry, not yet. Not sure if will do in the nearest future, but anyone may feel free to investigate and fix this

@jasyes
Copy link

jasyes commented Nov 7, 2021

It's looks like there is a bug in latest versions of @nestjs/platform-fastify 8.1.2. The Middleware from module do not fire.
In NestJS github the issue was reported then probably resolved in version 8.0.11 however than Kamil revert commit with bug fix.
However after research I found the workaround.

in main.ts you can add global middleware and use pino from Fastify.

import {storage, Store} from 'nestjs-pino/storage'
import pino from 'pino';

async function bootstrap() {
    const logger = pino({
      level: process.env.NODE_ENV !== 'production' ? 'debug' : 'info',
    })
    const app = await NestFactory.create<NestFastifyApplication>(ApplicationModule, new FastifyAdapter({
      logger,
    }), {
      bufferLogs: true,
    });
    app.useLogger(app.get(Logger));
    app.use((
      req: any,
      _:any,
      next: ()=> void,
    ) => {
      // @ts-ignore
      storage.run(new Store(req.log), next);
    });
   await app.listen(PORT, '0.0.0.0');
}
bootstrap();

@iamolegga
Copy link
Owner

thanks @jasyes closing this as this is not a bug of the current lib, but will pin it in issues so everyone can see it. Please ping me here when this bug will be fixed.

@iamolegga iamolegga pinned this issue Nov 12, 2021
@iamolegga iamolegga changed the title [BUG] Request context not working when using FastifyAdapter [NESTJS BUG] Request context not working when using FastifyAdapter Nov 12, 2021
@thomaschaaf
Copy link

@jasyes have you created an issue in the nestjs repo? can you also post where the commit was reverted?

@jasyes
Copy link

jasyes commented Nov 18, 2021

The main issue was that different version of fastify installed as dependency of @nestjs/platform-fastify and as dependency for project.

@adworacz
Copy link

I just encountered this bug myself, thanks for pinning this issue!

Looks like it's still not fixed upstream.

@n4an
Copy link

n4an commented Dec 20, 2021

Hey, you @tomtastico, @iamolegga, @SirReiva !

How to do that nestjs/nest#8809 (comment)

Who undrestand how to reproduce that problem without any third-party libraries?

@n4an
Copy link

n4an commented Dec 21, 2021

@jmcdo29 thanks, but for that I needed to get a ban.

These mens and womens can when want to do something.

@adworacz
Copy link

Based on @jmcdo29's investigation in this issue, it appears that the root cause may be because of how the FastifyRequest wraps the native Node IncomingRequest object.

In order to access attach properties properly to request for Fastify, they should be retrieved from request.raw.foo instead of request.foo.

@iamolegga Does this make sense to you? Perhaps some changes need to be made to the middleware to adapt based on which adapters (Express vs Fastify) is in use?

@jmcdo29
Copy link

jmcdo29 commented Dec 22, 2021

Another option, just as an FYI, would be to use an onRequest hook for Fastify, though f I recall the onRequest hook doesn't have support for path mapping. I've done this in nest-cookies as a way to manage cookies before Nest added support for @Res({ passthrough: true })

@iamolegga
Copy link
Owner

Thank you for the investigation. So, it looks like this is a bug in this library, not the framework. I'm a bit busy now (will have time next week) but if someone knows what to do, please open PR (with correct tests), I'll check it asap.

@iamolegga iamolegga reopened this Dec 22, 2021
@iamolegga iamolegga changed the title [NESTJS BUG] Request context not working when using FastifyAdapter [BUG] Request context not working when using FastifyAdapter Dec 22, 2021
@iamolegga
Copy link
Owner

FYI https://github.com/fastify/middie/issues/124

@itheodoro
Copy link
Author

FYI fastify/fastify#3571

@iamolegga
Copy link
Owner

Thanks, but I think it's enough of FYI's here, it won't help, only spam notifications, I'm already subscribed to all the related issues and watching the situation.

It looks like we only need to change the storage.run with storage.enterWith in the code. But that thing should be tested correctly, as current tests pass for fastify, so the main challenge here is to write correct tests for this first, check that current behavior is broken, and the new one is correct.

@itheodoro
Copy link
Author

itheodoro commented Dec 27, 2021

If I get it right, Fastify v3.25.2 already solves the issue. Since we are using Fastify via platform-fastify, a new version of the package with updated Fastify version (>= v3.25.2) must solve the problem without the need of implementations here in nestjs-pino. Also, the need of a new version of platform-fastify was already pointed here.

@itheodoro
Copy link
Author

Closing this issue since @nestjs/[email protected] fixes the wrong behavior by updating fastify version. Thanks all!

@iamolegga
Copy link
Owner

Cool, @itheodoro have you tested this fix already?

@itheodoro
Copy link
Author

Yes, I've tested and it's working fine, @iamolegga 🎉

@iamolegga iamolegga unpinned this issue Jan 21, 2022
@adworacz
Copy link

For anyone else who stumbles across this issue, I was still having issues with logs not having request context information, as shown in the OP.

I finally tracked it down to having fastify in my devDependencies. If I removed that AND deleted my node_modules folder AND deleted my package-lock.json folder (forcing it to be completely rebuild) THEN I started receiving request context information accordingly.

I was able to replicate this with a fresh NestJS project too, where simply having the fastify dep in my package.json was enough to break the request context being added to my logs.

Hope this helps someone else!

@yanivfranco
Copy link

@adworacz Literally life saver!!!
I already encountered some issues when having both fastify and @nestjs/platform-fastify present, and it is always consumed so much time & energy to reach this conclusion

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

9 participants