Usage
Use the SDK to capture errors and other events.
Sentry's SDK hooks into your runtime environment, letting recover from panics and also manually capture and report errors and other events.
Key terms:
- An event is one instance of sending data to Sentry. Generally, this data is an error or panic.
- An issue is a grouping of similar events.
- The reporting of an event is called capturing. When an event is captured, it’s sent to Sentry.
The most common form of capturing is to capture errors. What can be captured as an error varies by platform. In general, if you have something that looks like an exception, it can be captured. For some SDKs, you can also omit the argument to CaptureException and Sentry will attempt to capture the current exception. It is also useful for manual reporting of errors or messages to Sentry.
While capturing an event, you can also record the breadcrumbs that lead up to that event. Breadcrumbs are different from events: they will not create an event in Sentry, but will be buffered until the next event is sent. Learn more about breadcrumbs in our Breadcrumbs documentation.
To capture an event in Go, you can pass any struct implementing an error interface to CaptureException(). The SDK automatically unwraps and captures all errors in the error chain, providing comprehensive error context to Sentry. The SDK also supports third-party libraries and extracts all stack traces in the chain.
The SDK supports multiple error wrapping patterns:
- Standard library:
fmt.Errorfwith%wanderrors.Join(Go 1.20+) - Single error chains: Errors implementing
Unwrap() error - Third-party libraries: Errors implementing
Cause() error
The SDK is fully compatible with (but not limited to):
github.com/pkg/errorsgithub.com/go-errors/errorsgithub.com/pingcap/errorsgithub.com/rotisserie/eris
If there is an errors package that's not working out of the box, let us know!
When you use errors.Join to combine multiple errors, the SDK captures them as an exception group. This allows you to see all related errors together in Sentry, properly structured with their relationships preserved.
err1 := errors.New("first error")
err2 := errors.New("second error")
joinedErr := errors.Join(err1, err2)
// This will be captured as an exception group in Sentry
sentry.CaptureException(joinedErr)
For errors wrapped with fmt.Errorf or single-error chains, the SDK captures each error in the chain individually, maintaining the causal relationship.
f, err := os.Open("filename.ext")
if err != nil {
sentry.CaptureException(err)
}
Another common operation is to capture a bare message. A message is textual information that should be sent to Sentry. Typically, our SDKs don't automatically capture messages, but you can capture them manually.
Messages show up as issues on your issue stream, with the message as the issue name.
sentry.CaptureMessage("Something went wrong")
By default, Sentry's Go SDK uses an asynchronous transport. That means that calls to CaptureException, CaptureEvent, and CaptureMessage return without waiting for network operations. Instead, events are buffered and sent over the network in a background goroutine. Call sentry.Flush to wait for event delivery before the program terminates. You can change the default behavior by using a different transport, for example HTTPSyncTransport. More details in the Transports section.
Another common operation is to capture logs. Check Logs for how to setup your logging integration to send log entries to Sentry.
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").