getsentry / getsentry/sentry-go

The root `sentry-go` packaged uses a different context value vs `sentry-go/gin`

Open
#536 2 comments 0 reactions 0 assignees View on GitHub
Go Improvement
Dominant language
Go
Stars
1.1k
Forks
262
Avg merge
1d 48m
Merged PRs (30d)
7

Description

## Summary

The `GetHubFromContext` in the `github.com/getsentry/sentry-go` package isn't compatible with the `GetHubFromContext` from `github.com/getsentry/sentry-go/gin` since they both use a different value when putting the `Hub` into their respective contexts.

## Steps To Reproduce

```go

package main

import (
"context"
"fmt"

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

func check(err error) {
if err != nil {
panic(err)
}
}

func main() {
err := sentry.Init(sentrySdk.ClientOptions{
Dsn: "https://00000000000000000000000000000000@o62611.ingest.sentry.io/0000008",
Debug: true,
})
check(err)

ctx := context.Background()
ctx = sentry.SetHubOnContext(ctx)

fmt.Printf("I will be found %t", sentry.GetHubFromContext(ctx) != nil)
fmt.Printf("I not will be found %t", sentrygin.GetHubFromContext(ctx) != nil)
}
```

The value used to set the `hub` in context in gin is `"sentry"`, [here](https://github.com/getsentry/sentry-go/blob/v0.17.0/gin/sentrygin.go#L15). The value used to set `Hub` in the is `HubContextKey` [here](https://github.com/getsentry/sentry-go/blob/v0.17.0/hub.go#L15).

This is probably the case since `gin.Context` doesn't play well with the `context.Context` interface and has it's own way of setting values. The `HubContextKey` is exported from the `github.com/getsentry/sentry-go` package so it should be possible to switch it over to keep everything consistent.

The problem would be if you would have to set both for some time in case a user has hard-coded the key in their code instead of using the function. In that case, it could be a breaking change even if the value isn't exported out of the `sentry-go/gin` package.

## Expected Behavior

Expected that using the sentry package would be compatible across. This leads to the application having to know what the higher context is doing instead of letting the `context.Context` interface get passed through. I was very confused when my gin servers weren't reporting any events.

### SDK
- `sentry-go` version: `v0.17.0`
- Go version: `19`
- Using Go Modules? yes

### Sentry
- Using hosted Sentry in sentry.io? yes
- Anything particular to your environment that could be related to this issue? no

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.