Usage

In Go, there are both errors and panics, and Raven can handle both. To learn more about the differences, please read Error handling and Go.

To handle normal error responses, we have two options: CaptureErrorAndWait and CaptureError. The former is a blocking call, for a case where you’d like to exit the application after reporting, and the latter is non-blocking.

Copied
f, err := os.Open("filename.ext")
if err != nil {
	raven.CaptureErrorAndWait(err, nil)
	log.Panic(err)
}

Capturing a panic is pretty simple as well. We just need to wrap our code in CapturePanic. CapturePanic will execute the func and if a panic happened, we will record it, and gracefully continue.

Copied
raven.CapturePanic(func() {
	// do all of the scary things here
	panic("My first Sentry error!")
}, nil)

You can verify your Sentry integration by using the above snippet to send a test event.

Other than regular Errors and Panics, there are also two additional methods that allow sending information to Sentry.

CaptureMessage and it's sibling CaptureMessageAndWait let you send arbitrary messages that the SDK will log in your Sentry project.

Copied
raven.CaptureMessageAndWait("Something bad happened and I would like to know about that", map[string]string{"category": "logging"})

Capture is a low-level method that can be used to deliver hand-crafted events (named type Packet in The Sentry Go SDK). It has no CaptureAndWait counterpart, but returns a channel in case you want to verify delivery status. To form a Packet, you can use Packet type directly, or NewPacket and NewPacketWithExtra helper methods which creates an empty event with a message and optional extra data.

Copied
packet := &raven.Packet{
    Message: "Hand-crafted event",
    Extra: &raven.Extra{
        "runtime.Version": runtime.Version(),
        "runtime.NumCPU": runtime.NumCPU(),
    },
}
raven.Capture(packet, nil)

All Capture* calls can take additional arguments which can help you make more sense out of your reports. To read more about it, see Context section.

To set up client-side sampling you can use SetSampleRate Client function. The Sentry SDK disables error sampling by default (sampleRate=1).

Copied
raven.SetSampleRate(0.25)
Help improve this content
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").