Shutdown and Draining
Learn more about the default behavior of our SDK if the application shuts down unexpectedly.
By default, the SDK uses an asynchronous transport running in its own separate goroutine to send events over the network. As a result, some events may be lost if the application shuts down unexpectedly. The SDK provides mechanisms to handle this scenario.
To avoid unintentionally dropping events when the program terminates, arrange for sentry.Flush to be called, typically using defer.
If you use multiple clients, arrange for each of them to be flushed as appropriate.
Flush waits until any buffered events are sent to the Sentry server, blocking for at most the given timeout. It returns false if the timeout was reached. In that case, some events may not have been sent.
// err := sentry.Init(...)
defer sentry.Flush(2 * time.Second)
sentry.CaptureMessage("my message")
FlushWithContext is an alternative to Flush that uses a context.Context to manage the timeout or cancellation of the flush operation. This approach is particularly useful in applications where you are already using contexts for managing deadlines and cancellations.
// err := sentry.Init(...)
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
sentry.CaptureMessage("my message")
sentry.FlushWithContext(ctx)
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").