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

Why do I need to call log.flush() before returning on a server component? #214

Open
Dacaramo opened this issue Jul 17, 2024 · 3 comments
Open

Comments

@Dacaramo
Copy link

I'm using the next-axiom library for Next.js and I'm trying to understand why does log.flush() needs to be called before returning on server components?

It will be great to know a little bit more about this function to really understand when to use it, that's what I'm proposing here, cause until know the docs of this library does not tell anything about that function, they only present a single use case out of 20 of more posible use cases.

Maybe you want to create a function that process errors and one of the tasks of that function is sending the error info to an Axiom dataset, so does that function will need to call log.flush() internally? What if I call this function multiple times on the server components? Does calling log.flush() multiple times will cause unwanted behavior?

Maybe you want to ingest data from middleware.ts, generateMetadata or any other context that is executed on the server, but that is not a server component, what do I do in those cases?

Do I need to call log.flush before throwing also?

It will be great to have this specified on the docs, thank you so much!

@radziminski
Copy link

On top of that, log.flush takes significant amount of time, so it also impacts server-components performance (as described here).

@cereallarceny
Copy link

It seems to me that all log.flush does is actually "flush" the queue of logs to Axiom throttled in batches. In other words, it guarantees that any unsent logs are sent before the server action completes execution. If you want to guarantee all logs are sent in the server component, you must flush. Otherwise, you may have orphaned logs that never get sent before the component is returned from the server.

What isn't clear to me is why server component, like route handlers, can't "auto flush". I can't imagine why they wouldn't be able to do that, but I have limited understanding of the library's internals.

@dasfmi
Copy link
Collaborator

dasfmi commented Dec 9, 2024

@cereallarceny that's right, flush ensures any unset logs are shipped to Axiom. Auto flushing sounds good if we can do it, we do that with the route handlers using withAxiom(). If we can achieve the same that would be great, is it possible to wrap Server components?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants