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

Force Sentry to wait for every captureException call #15058

Open
cideM opened this issue Jan 17, 2025 · 2 comments
Open

Force Sentry to wait for every captureException call #15058

cideM opened this issue Jan 17, 2025 · 2 comments

Comments

@cideM
Copy link

cideM commented Jan 17, 2025

Problem Statement

I have been working with the Go SDK for a while now and it's very pleasant to use and even includes a Sync transport.

I am now working in a Javascript project that uses a framework which unfortunately calls process.exit quite often. As a result, Sentry errors never make it to our dashboard, since Sentry doesn't have time to flush the buffer. I thought I could just set some option and force Sentry to wait for every single captureException call

Solution Brainstorm

One potential workaround would be to overwrite process.exit and call Sentry.close() in there.

I thought that I could also provide a sync transport (there appears to be none) so I tried something very simple:

const makeTransport = (options: NodeTransportOptions): Transport => {
  return {
    send: (e: Envelope): Promise<TransportMakeRequestResponse> => {
      console.log("send", e) // <-- console.log that transport is working
      return Promise.resolve({
        statusCode: 200,
        headers: {
          "x-sentry-rate-limits": null,
          "retry-after": null,
        },
      });
    },
    flush: (timeout?: number): Promise<boolean> => {
      console.log("flush");
      return Promise.resolve(true);
    },
  };
};

Later in this file I am doing the following to trigger the custom transport:

Sentry.init({
  debug: true,
  enabled: true,
  dsn: "....",
  transport: makeTransport,
  attachStacktrace: true,
});
Sentry.captureException("test");

I'd expect to see something in my console that indicates that send was called, but I don't. Is there some buffering going on before my custom transport is called?

It seems that there is currently no way to force Sentry to wait until every single captureException call has finished.

@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 3 Jan 17, 2025
@cideM cideM changed the title Sync transport for NodeJS like in Golang Force Sentry to wait for every captureException call Jan 17, 2025
@andreiborza
Copy link
Member

Hello @cideM, I tried adding your makeTransport to a sample project of mine and I'm seeing the logs.

Could you please provide a sample reproduction repo?

Also, check out https://docs.sentry.io/platforms/javascript/configuration/transports/#custom-transport, we have a helper called createTransport that you could use.

Although, if your framework exits with process.exit I don't think flushing will be awaited.

@AbhiPrasad
Copy link
Member

process.exit instructs Node.js to terminate synchronously and is designed to make the process exit as quickly as possible. All pending I/O operations (like a network request) will get cancelled. Only sync code can be run in process.on('exit', ...) as well.

Our transport creates and resolves promises (async code) as sending the event is I/O, and so therefore there never will be a strong guarantee that all events will get flushed. I recommend re-architecting the app so that a call to Sentry.flush is executed before the process is exited instead of trying to listen on process exit or change how the transport works.

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

No branches or pull requests

3 participants