Docs · Go

SDKs

Go

Bugzor works with the open-source sentry-go SDK. Tested with sentry-go 0.50 on Go 1.25.

Install#

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

Configure#

main.go
package main

import (
	"log"
	"os"
	"time"

	"github.com/getsentry/sentry-go"
)

func main() {
	err := sentry.Init(sentry.ClientOptions{
		Dsn:         os.Getenv("SENTRY_DSN"), // https://<public-key>@www.bugzor.com/<project-id>
		Environment: "production",
		Release:     "my-app@1.0.0",
		// Include the user's IP (needed for "users affected").
		SendDefaultPII: true,
	})
	if err != nil {
		log.Fatalf("sentry.Init: %v", err)
	}
	// Events are sent in the background; wait for them before the program exits.
	defer sentry.Flush(2 * time.Second)

	sentry.CaptureMessage("Hello from Go")
}

Capture errors#

Go
if _, err := strconv.Atoi(input); err != nil {
	sentry.CaptureException(fmt.Errorf("parsing quantity: %w", err))
}

Wrapped errors (%w, or any type with Unwrap() error) show every cause in the chain.

Recover panics in goroutines you start yourself:

Go
go func() {
	defer sentry.Recover()
	work()
}()

HTTP servers#

Use the middleware for your router. For net/http:

Go
import sentryhttp "github.com/getsentry/sentry-go/http"

handler := sentryhttp.New(sentryhttp.Options{Repanic: true})
http.Handle("/", handler.Handle(mux))

There are packages for Gin (sentry-go/gin), Echo (sentry-go/echo), Fiber (sentry-go/fiber), FastHTTP, Iris and Negroni. They attach the request to every event and recover panics.

Add context#

Go
sentry.WithScope(func(scope *sentry.Scope) {
	scope.SetTag("component", "checkout")
	scope.SetUser(sentry.User{ID: "42", Email: "ann@example.com"})
	scope.SetContext("cart", sentry.Context{"items": 3, "total_cents": 4599})
	sentry.CaptureMessage("Cart total mismatch")
})

Filter events#

Go
sentry.Init(sentry.ClientOptions{
	Dsn: os.Getenv("SENTRY_DSN"),
	BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event {
		if event.Tags["healthcheck"] == "yes" {
			return nil // dropped: never sent, never counted
		}
		return event
	},
})

Test it#

Terminal
SENTRY_DSN="https://<public-key>@www.bugzor.com/<project-id>" go run .

Set Debug: true in ClientOptions to log every request and its response.

Something unclear or out of date? Tell us.