Migration Guide

How to move from raven-go to sentry-go

Installation

raven-go

Copied
go get github.com/getsentry/raven-go

sentry-go

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

Configuration

raven-go

Copied
import "github.com/getsentry/raven-go"

func main() {
	raven.SetDSN("https://examplePublicKey@o0.ingest.sentry.io/0")
}

sentry-go

Copied
import (
	"fmt"
	"github.com/getsentry/sentry-go"
)

func main() {
	err := sentry.Init(sentry.ClientOptions{
		Dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
	})

	if err != nil {
		fmt.Printf("Sentry initialization failed: %v\n", err)
	}
}

raven-go

Copied
SetDSN()
SetDefaultLoggerName()
SetDebug()
SetEnvironment()
SetRelease()
SetSampleRate()
SetIgnoreErrors()
SetIncludePaths()

sentry-go

Copied
sentry.Init(sentry.ClientOptions{
	Dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
	DebugWriter: os.Stderr,
	Debug: true,
	Environment: "environment",
	Release: "my-project-name@1.0.0",
	SampleRate: 0.5,
	// IgnoreErrors: TBD,
	// IncludePaths: TBD
})

Available options: see Configuration section.

Providing SSL Certificates

By default, TLS uses the host's root CA set. If you don't have ca-certificates (which should be your go-to way of fixing the missing certificates issue) and want to use gocertifi instead, you can provide pre-loaded cert files as one of the options to sentry.Init call:

Copied
package main

import (
	"log"

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

sentryClientOptions := sentry.ClientOptions{
	Dsn: "https://examplePublicKey@o0.ingest.sentry.io/0",
}

rootCAs, err := gocertifi.CACerts()
if err != nil {
	log.Println("Couldn't load CA Certificates: %v\n", err)
} else {
	sentryClientOptions.CaCerts = rootCAs
}

sentry.Init(sentryClientOptions)

Usage

Capturing Errors

raven-go

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

sentry-go

Copied
f, err := os.Open("filename.ext")
if err != nil {
	sentry.CaptureException(err)
}

Capturing Panics

raven-go

Copied
raven.CapturePanic(func() {
	// do all of the scary things here
}, nil)

sentry-go

Copied
func() {
	defer sentry.Recover()
	// do all of the scary things here
}()

Capturing Messages

raven-go

Copied
raven.CaptureMessage("Something bad happened and I would like to know about that")

sentry-go

Copied
sentry.CaptureMessage("Something bad happened and I would like to know about that")

Capturing Events

raven-go

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

sentry-go

Copied
event := sentry.NewEvent()
event.Message = "Hand-crafted event"
event.Extra["runtime.Version"] = runtime.Version()
event.Extra["runtime.NumCPU"] = runtime.NumCPU()

sentry.CaptureEvent(event)

Additional Data

See Context section.

Event Sampling

raven-go

Copied
raven.SetSampleRate(0.25)

sentry-go

Copied
sentry.Init(sentry.ClientOptions{
	SampleRate: 0.25,
})
Copied
raven.CaptureMessageAndWait("Something bad happened and I would like to know about that")

sentry-go

Copied
sentry.CaptureMessage("Something bad happened and I would like to know about that")

if sentry.Flush(time.Second * 2) {
	// event delivered
} else {
	// timeout reached
}

Context

Per-event

raven-go

Copied
raven.CaptureError(err, map[string]string{"browser": "Firefox"}, &raven.Http{
	Method: "GET",
	URL: "https://example.com/raven-go"
})

sentry-go

Copied
sentry.WithScope(func(scope *sentry.Scope) {
	scope.SetTag("browser", "Firefox")
	scope.SetContext("Request", map[string]string{
		"Method": "GET",
		"URL": "https://example.com/raven-go",
	})
	sentry.CaptureException(err)
})

Globally

SetHttpContext

raven-go

Copied
raven.SetHttpContext(&raven.Http{
	Method: "GET",
	URL: "https://example.com/raven-go",
})

sentry-go

Copied
sentry.ConfigureScope(func(scope *sentry.Scope) {
	scope.SetContext("Request", map[string]string{
		"Method": "GET",
		"URL": "https://example.com/raven-go",
	})
})
SetTagsContext

raven-go

Copied
t := map[string]string{"day": "Friday", "sport": "Weightlifting"}
raven.SetTagsContext(map[string]string{"day": "Friday", "sport": "Weightlifting"})

sentry-go

Copied
sentry.ConfigureScope(func(scope *sentry.Scope) {
	scope.SetTags(map[string]string{"day": "Friday", "sport": "Weightlifting"})
})
SetUserContext

raven-go

Copied
raven.SetUserContext(&raven.User{
	ID: "1337",
	Username: "kamilogorek",
	Email: "kamil@sentry.io",
	IP: "127.0.0.1",
})

sentry-go

Copied
sentry.ConfigureScope(func(scope *sentry.Scope) {
	scope.SetUser(sentry.User{
		ID: "1337",
		Username: "kamilogorek",
		Email: "kamil@sentry.io",
		IPAddress: "127.0.0.1",
	})
})
ClearContext

raven-go

Copied
raven.ClearContext()

sentry-go

Copied
sentry.ConfigureScope(func(scope *sentry.Scope) {
	scope.Clear()
})
WrapWithExtra

raven-go

Copied
path := "filename.ext"
f, err := os.Open(path)
if err != nil {
	err = raven.WrapWithExtra(err, map[string]string{"path": path, "cwd": os.Getwd()})
	raven.CaptureError(err, nil)
}

sentry-go

Copied
// use `sentry.WithScope`, see "Context / Per-event Section"
path := "filename.ext"
f, err := os.Open(path)
if err != nil {
	sentry.WithScope(func(scope *sentry.Scope) {
		scope.SetExtras(map[string]interface{}{"path": path, "cwd": os.Getwd()})
		sentry.CaptureException(err)
	})
}

Integrations

net/http

raven-go

Copied
mux := http.NewServeMux
http.Handle("/", raven.Recoverer(mux))

// or

func root(w http.ResponseWriter, r *http.Request) {}
http.HandleFunc("/", raven.RecoveryHandler(root))

sentry-go

Copied
go get github.com/getsentry/sentry-go/http
Copied
import sentryhttp "github.com/getsentry/sentry-go/http"
Copied
sentryHandler := sentryhttp.New(sentryhttp.Options{
	Repanic: false,
	WaitForDelivery: true,
})

mux := http.NewServeMux
http.Handle("/", sentryHandler.Handle(mux))

// or

func root(w http.ResponseWriter, r *http.Request) {}
http.HandleFunc("/", sentryHandler.HandleFunc(root))