/sentry-go

Official Sentry SDK for Go

Primary LanguageGoBSD 2-Clause "Simplified" LicenseBSD-2-Clause


Official Sentry SDK for Go

Build Status Go Report Card Discord GoDoc go.dev

sentry-go provides a Sentry client implementation for the Go programming language. This is the next line of the Go SDK for Sentry, intended to replace the raven-go package.

Looking for the old raven-go SDK documentation? See the Legacy client section here. If you want to start using sentry-go instead, check out the migration guide.

Requirements

The only requirement is a Go compiler.

We verify this package against the 3 most recent releases of Go. Those are the supported versions. The exact versions are defined in .travis.yml.

In addition, we run tests against the current master branch of the Go toolchain, though support for this configuration is best-effort.

Installation

sentry-go can be installed like any other Go library through go get:

$ go get github.com/getsentry/sentry-go

Or, if you are already using Go Modules, you may specify a version number as well:

$ go get github.com/getsentry/sentry-go@latest

Check out the list of released versions.

Configuration

To use sentry-go, you’ll need to import the sentry-go package and initialize it with your DSN and other options.

If not specified in the SDK initialization, the DSN, Release and Environment are read from the environment variables SENTRY_DSN, SENTRY_RELEASE and SENTRY_ENVIRONMENT, respectively.

More on this in the Configuration section of the official Sentry documentation.

Usage

The SDK must be initialized with a call to sentry.Init. The default transport is asynchronous and thus most programs should call sentry.Flush to wait until buffered events are sent to Sentry right before the program terminates.

Typically, sentry.Init is called in the beginning of func main and sentry.Flush is deferred right after.

Note that if the program terminates with a call to os.Exit, either directly or indirectly via another function like log.Fatal, deferred functions are not run.

In that case, and if it is important for you to report outstanding events before terminating the program, arrange for sentry.Flush to be called before the program terminates.

Example:

// 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: "",
		// 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)
		}
	}
}

For your convenience, this example is available at example/basic/main.go. There are also more examples in the example directory.

For more detailed information about how to get the most out of sentry-go, checkout the official documentation:

Resources

License

Licensed under The 2-Clause BSD License, see LICENSE.

Community

Join Sentry's #go channel on Discord to get involved and help us improve the SDK!