# Migration Guide | Sentry for Zap

## [How to move from `raven-go` to `sentry-go`](https://docs.sentry.io/platforms/go/guides/zap/migration.md#how-to-move-from-raven-go-to-sentry-go)

### [Installation](https://docs.sentry.io/platforms/go/guides/zap/migration.md#installation)

raven-go

```bash
go get github.com/getsentry/raven-go
```

sentry-go

```bash
go get github.com/getsentry/sentry-go
```

### [Configuration](https://docs.sentry.io/platforms/go/guides/zap/migration.md#configuration)

raven-go

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

func main() {
	raven.SetDSN("___PUBLIC_DSN___")
}
```

sentry-go

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

func main() {
	err := sentry.Init(sentry.ClientOptions{
		Dsn: "___PUBLIC_DSN___",
		// Adds request headers and IP for users,
        // visit: https://docs.sentry.io/platforms/go/data-management/data-collected/ for more info
        SendDefaultPII: true,
	})

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

raven-go

```go
SetDSN()
SetDefaultLoggerName()
SetDebug()
SetEnvironment()
SetRelease()
SetSampleRate()
SetIgnoreErrors()
SetIncludePaths()
```

sentry-go

```go
sentry.Init(sentry.ClientOptions{
	Dsn: "___PUBLIC_DSN___",
	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](https://docs.sentry.io/platforms/go/configuration/options.md) section.

#### [Providing SSL Certificates](https://docs.sentry.io/platforms/go/guides/zap/migration.md#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:

```go
package main

import (
	"log"

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

sentryClientOptions := sentry.ClientOptions{
	Dsn: "___PUBLIC_DSN___",
}

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](https://docs.sentry.io/platforms/go/guides/zap/migration.md#usage)

#### [Capturing Errors](https://docs.sentry.io/platforms/go/guides/zap/migration.md#capturing-errors)

raven-go

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

sentry-go

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

#### [Capturing Panics](https://docs.sentry.io/platforms/go/guides/zap/migration.md#capturing-panics)

raven-go

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

sentry-go

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

#### [Capturing Messages](https://docs.sentry.io/platforms/go/guides/zap/migration.md#capturing-messages)

raven-go

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

sentry-go

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

#### [Capturing Events](https://docs.sentry.io/platforms/go/guides/zap/migration.md#capturing-events)

raven-go

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

sentry-go

```go
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](https://docs.sentry.io/platforms/go/guides/zap/migration.md#additional-data)

See Context section.

#### [Event Sampling](https://docs.sentry.io/platforms/go/guides/zap/migration.md#event-sampling)

raven-go

```go
raven.SetSampleRate(0.25)
```

sentry-go

```go
sentry.Init(sentry.ClientOptions{
	// ...
	SampleRate: 0.25,
})
```

#### [Awaiting the response (not recommended)](https://docs.sentry.io/platforms/go/guides/zap/migration.md#awaiting-the-response-not-recommended)

```go
raven.CaptureMessageAndWait("Something bad happened and I would like to know about that")
```

sentry-go

```go
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](https://docs.sentry.io/platforms/go/guides/zap/migration.md#context)

#### [Per-event](https://docs.sentry.io/platforms/go/guides/zap/migration.md#per-event)

raven-go

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

sentry-go

```go
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](https://docs.sentry.io/platforms/go/guides/zap/migration.md#globally)

##### [SetHttpContext](https://docs.sentry.io/platforms/go/guides/zap/migration.md#sethttpcontext)

raven-go

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

sentry-go

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

##### [SetTagsContext](https://docs.sentry.io/platforms/go/guides/zap/migration.md#settagscontext)

raven-go

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

sentry-go

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

##### [SetUserContext](https://docs.sentry.io/platforms/go/guides/zap/migration.md#setusercontext)

raven-go

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

sentry-go

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

##### [ClearContext](https://docs.sentry.io/platforms/go/guides/zap/migration.md#clearcontext)

raven-go

```go
raven.ClearContext()
```

sentry-go

```go
sentry.ConfigureScope(func(scope *sentry.Scope) {
	scope.Clear()
})
```

##### [WrapWithExtra](https://docs.sentry.io/platforms/go/guides/zap/migration.md#wrapwithextra)

raven-go

```go
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

```go
// 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](https://docs.sentry.io/platforms/go/guides/zap/migration.md#integrations)

#### [net/http](https://docs.sentry.io/platforms/go/guides/zap/migration.md#nethttp)

raven-go

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

// or

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

sentry-go

```bash
go get github.com/getsentry/sentry-go/http
```

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

```go
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))
```
