Skip to content

add setHubOnContext functions for integrations that use custom context #931

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@

- Every integration is now a separate module, reducing the binary size and number of dependencies. Once you update `sentry-go` to latest version, you'll need to `go get` the integration you want to use. For example, if you want to use the `echo` integration, you'll need to run `go get github.com/getsentry/sentry-go/echo` ([#919](github.com/getsentry/sentry-go/pull/919)).

### Features

Add ability to override `hub` in `context` for integrations that use custom context ([#931](https://github.com/getsentry/sentry-go/pull/931))

## 0.30.0

The Sentry SDK team is happy to announce the immediate availability of Sentry Go SDK v0.30.0.
Expand Down
4 changes: 4 additions & 0 deletions echo/sentryecho.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ func GetHubFromContext(ctx echo.Context) *sentry.Hub {
return nil
}

func SetHubOnContext(ctx echo.Context, hub *sentry.Hub) {
ctx.Set(valuesKey, hub)
}

// GetSpanFromContext retrieves attached *sentry.Span instance from echo.Context.
// If there is no transaction on echo.Context, it will return nil.
func GetSpanFromContext(ctx echo.Context) *sentry.Span {
Expand Down
43 changes: 43 additions & 0 deletions echo/sentryecho_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,49 @@ func TestIntegration(t *testing.T) {
}
}

func TestSetHubOnContext(t *testing.T) {
err := sentry.Init(sentry.ClientOptions{})
if err != nil {
t.Fatal(err)
}

hub := sentry.CurrentHub().Clone()
router := echo.New()
router.GET("/set-hub", func(c echo.Context) error {
sentryecho.SetHubOnContext(c, hub)
retrievedHub := sentryecho.GetHubFromContext(c)
if retrievedHub == nil {
t.Error("expecting hub to be set on context")
}
if retrievedHub != hub {
t.Error("expecting retrieved hub to be the same as the set hub")
}
return c.NoContent(http.StatusOK)
})

srv := httptest.NewServer(router)
defer srv.Close()

c := srv.Client()
c.Timeout = time.Second

req, err := http.NewRequest("GET", srv.URL+"/set-hub", nil)
if err != nil {
t.Fatal(err)
}
res, err := c.Do(req)
if err != nil {
t.Fatal(err)
}
if res.StatusCode != 200 {
t.Errorf("Status code = %d expected: %d", res.StatusCode, 200)
}
err = res.Body.Close()
if err != nil {
t.Fatal(err)
}
}

func TestGetSpanFromContext(t *testing.T) {
err := sentry.Init(sentry.ClientOptions{
EnableTracing: true,
Expand Down
4 changes: 4 additions & 0 deletions fasthttp/sentryfasthttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ func GetHubFromContext(ctx *fasthttp.RequestCtx) *sentry.Hub {
return nil
}

func SetHubOnContext(ctx *fasthttp.RequestCtx, hub *sentry.Hub) {
ctx.SetUserValue(valuesKey, hub)
}

// GetSpanFromContext retrieves attached *sentry.Span instance from *fasthttp.RequestCtx.
// If there is no transaction on *fasthttp.RequestCtx, it will return nil.
func GetSpanFromContext(ctx *fasthttp.RequestCtx) *sentry.Span {
Expand Down
16 changes: 16 additions & 0 deletions fasthttp/sentryfasthttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,19 @@ func TestGetTransactionFromContext(t *testing.T) {
})
}
}

func TestSetHubOnContext(t *testing.T) {
hub := sentry.NewHub(sentry.CurrentHub().Client(), sentry.NewScope())
ctx := &fasthttp.RequestCtx{}

sentryfasthttp.SetHubOnContext(ctx, hub)

retrievedHub := sentryfasthttp.GetHubFromContext(ctx)
if retrievedHub == nil {
t.Fatal("expected hub to be set on context, but got nil")
}

if !reflect.DeepEqual(hub, retrievedHub) {
t.Fatalf("expected hub to be %v, but got %v", hub, retrievedHub)
}
}
4 changes: 4 additions & 0 deletions fiber/sentryfiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ func GetHubFromContext(ctx *fiber.Ctx) *sentry.Hub {
return nil
}

func SetHubOnContext(ctx *fiber.Ctx, hub *sentry.Hub) {
ctx.Locals(valuesKey, hub)
}

func GetSpanFromContext(ctx *fiber.Ctx) *sentry.Span {
if span, ok := ctx.Locals(transactionKey).(*sentry.Span); ok {
return span
Expand Down
33 changes: 33 additions & 0 deletions fiber/sentryfiber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,36 @@ func TestHandlers(t *testing.T) {
})
}
}

func TestSetHubOnContext(t *testing.T) {
app := fiber.New()
hub := sentry.NewHub(sentry.CurrentHub().Client(), sentry.NewScope())

app.Get("/test", func(c *fiber.Ctx) error {
sentryfiber.SetHubOnContext(c, hub)
retrievedHub := sentryfiber.GetHubFromContext(c)
if retrievedHub == nil {
t.Fatal("expected hub to be set on context, but got nil")
}
if !reflect.DeepEqual(hub, retrievedHub) {
t.Fatalf("expected hub to be %v, but got %v", hub, retrievedHub)
}
return nil
})

req, err := http.NewRequest(http.MethodGet, "/test", nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set("User-Agent", "fiber")

resp, err := app.Test(req)
if err != nil {
t.Fatalf("Request failed: %s", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
t.Fatalf("Expected status code %d, got %d", http.StatusOK, resp.StatusCode)
}
}
5 changes: 5 additions & 0 deletions gin/sentrygin.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,11 @@ func GetHubFromContext(ctx *gin.Context) *sentry.Hub {
return nil
}

// SetHubOnContext sets *sentry.Hub instance to gin.Context.
func SetHubOnContext(ctx *gin.Context, hub *sentry.Hub) {
ctx.Set(valuesKey, hub)
}

// GetSpanFromContext retrieves attached *sentry.Span instance from gin.Context.
// If there is no transaction on echo.Context, it will return nil.
func GetSpanFromContext(ctx *gin.Context) *sentry.Span {
Expand Down
12 changes: 12 additions & 0 deletions gin/sentrygin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"net/http"
"net/http/httptest"
"reflect"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -410,3 +411,14 @@ func TestIntegration(t *testing.T) {
t.Fatalf("Transaction status codes mismatch (-want +got):\n%s", diff)
}
}

func TestSetHubOnContext(t *testing.T) {
hub := sentry.CurrentHub()
ctx := &gin.Context{}
sentrygin.SetHubOnContext(ctx, hub)
got := sentrygin.GetHubFromContext(ctx)

if !reflect.DeepEqual(hub, got) {
t.Fatalf("Hub mismatch: got %v want %v", got, hub)
}
}
4 changes: 4 additions & 0 deletions iris/sentryiris.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@ func GetHubFromContext(ctx iris.Context) *sentry.Hub {
return nil
}

func SetHubOnContext(ctx iris.Context, hub *sentry.Hub) {
ctx.Values().Set(valuesKey, hub)
}

// GetSpanFromContext retrieves attached *sentry.Span instance from iris.Context.
// If there is no transaction on iris.Context, it will return nil.
func GetSpanFromContext(ctx iris.Context) *sentry.Span {
Expand Down
27 changes: 27 additions & 0 deletions iris/sentryiris_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"net/http"
"reflect"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -477,3 +478,29 @@ func TestGetSpanFromContext(t *testing.T) {
}
}
}

func TestSetHubOnContext(t *testing.T) {
app := iris.New()

app.Get("/with-hub", func(ctx iris.Context) {
hub := sentry.CurrentHub().Clone()
sentryiris.SetHubOnContext(ctx, hub)

newHub := sentryiris.GetHubFromContext(ctx)
if newHub == nil {
t.Error("expecting hub to be not nil")
}

if !reflect.DeepEqual(hub, newHub) {
t.Error("expecting hub to be the same")
}

ctx.StatusCode(http.StatusOK)
})

srv := httptest.New(t, app)

res := srv.Request(http.MethodGet, "/with-hub").Expect()

res.Status(http.StatusOK)
}
Loading