Go
On this page, we get you up running with Sentry's Go SDK, automatically reporting errors and exceptions in your application.
If you don't already have an account and Sentry project established, head over to sentry.io, then return to this page.
Using a framework? Take a look at our specific guides to get started.
Install
sentry-go can be installed like any other Go library through go get, and if using Go Modules, it will pick up the latest tagged version:
go get github.com/getsentry/sentry-goConfigure
To use sentry-go, you’ll need to import the sentry-go package and initialize it with the client options that will include your DSN. If you specify the SENTRY_DSN environment variable, you can omit this value from options and it will be picked up automatically for you. The release and environment can also be specified in the environment variables SENTRY_RELEASE and SENTRY_ENVIRONMENT respectively.
More on this in Configuration section.
Usage
The example below shows a sample usage of one of the three primary methods, the CaptureException method that allows for sending error data to Sentry.
The other two being CaptureMessage and Recover which you can read more about in Error Reporting and Capturing Panics.
// This is an example program that makes an HTTP request and prints response
// headers. Whenever a request fails, the error is reported to Sentry.
//
// Try it by running:
//
// go run main.go
// go run main.go https://sentry.io
// go run main.go bad-url
//
// To actually report events to Sentry, set the DSN either by editing the
// appropriate line below or setting the environment variable SENTRY_DSN to
// match the DSN of your Sentry project.
package main
import (
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/getsentry/sentry-go"
)
func main() {
if len(os.Args) < 2 {
log.Fatalf("usage: %s URL", os.Args[0])
}
err := sentry.Init(sentry.ClientOptions{
// Either set your DSN here or set the SENTRY_DSN environment variable.
Dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
// Enable printing of SDK debug messages.
// Useful when getting started or trying to figure something out.
Debug: true,
})
if err != nil {
log.Fatalf("sentry.Init: %s", err)
}
// Flush buffered events before the program terminates.
// Set the timeout to the maximum duration the program can afford to wait.
defer sentry.Flush(2 * time.Second)
resp, err := http.Get(os.Args[1])
if err != nil {
sentry.CaptureException(err)
log.Printf("reported to Sentry: %s", err)
return
}
defer resp.Body.Close()
for header, values := range resp.Header {
for _, value := range values {
fmt.Printf("%s=%s\n", header, value)
}
}
}Awaiting Event Delivery
By default, Sentry Go SDK uses an asynchronous transport. That means that
calls to CaptureException, CaptureMessage and CaptureEvent 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.
Resources:
- Package:
- github:getsentry/sentry-go
- Version:
- 0.7.0
- Repository:
- https://github.com/getsentry/sentry-go/