diff --git a/echo/sentryecho.go b/echo/sentryecho.go index c4357adec..cdab29b72 100644 --- a/echo/sentryecho.go +++ b/echo/sentryecho.go @@ -55,10 +55,8 @@ func New(options Options) echo.MiddlewareFunc { func (h *handler) handle(next echo.HandlerFunc) echo.HandlerFunc { return func(ctx echo.Context) error { - var hub *sentry.Hub - if sentry.HasHubOnContext(ctx.Request().Context()) { - hub = sentry.GetHubFromContext(ctx.Request().Context()) - } else { + hub := sentry.GetHubFromContext(ctx.Request().Context()) + if hub == nil { hub = sentry.CurrentHub().Clone() } hub.Scope().SetRequest(ctx.Request()) diff --git a/fasthttp/sentryfasthttp.go b/fasthttp/sentryfasthttp.go index 2675b07bb..336b2e6b5 100644 --- a/fasthttp/sentryfasthttp.go +++ b/fasthttp/sentryfasthttp.go @@ -63,6 +63,10 @@ func New(options Options) *Handler { // Handle wraps fasthttp.RequestHandler and recovers from caught panics. func (h *Handler) Handle(handler fasthttp.RequestHandler) fasthttp.RequestHandler { return func(ctx *fasthttp.RequestCtx) { + // Unlike for other integrations, we don't support getting an existing + // hub from the current request context because fasthttp doesn't use the + // standard net/http.Request and because fasthttp.RequestCtx implements + // context.Context but requires string keys. hub := sentry.CurrentHub().Clone() scope := hub.Scope() scope.SetRequest(convert(ctx)) diff --git a/gin/sentrygin.go b/gin/sentrygin.go index 872f8943a..d7a7dfbbc 100644 --- a/gin/sentrygin.go +++ b/gin/sentrygin.go @@ -57,7 +57,10 @@ func New(options Options) gin.HandlerFunc { } func (h *handler) handle(ctx *gin.Context) { - hub := sentry.CurrentHub().Clone() + hub := sentry.GetHubFromContext(ctx.Request.Context()) + if hub == nil { + hub = sentry.CurrentHub().Clone() + } hub.Scope().SetRequest(ctx.Request) ctx.Set(valuesKey, hub) defer h.recoverWithSentry(hub, ctx.Request) diff --git a/http/sentryhttp.go b/http/sentryhttp.go index b429413f4..b8071648e 100644 --- a/http/sentryhttp.go +++ b/http/sentryhttp.go @@ -51,12 +51,13 @@ func New(options Options) *Handler { // Handle wraps http.Handler and recovers from caught panics. func (h *Handler) Handle(handler http.Handler) http.Handler { return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { - hub := sentry.CurrentHub().Clone() + ctx := r.Context() + hub := sentry.GetHubFromContext(ctx) + if hub == nil { + hub = sentry.CurrentHub().Clone() + } hub.Scope().SetRequest(r) - ctx := sentry.SetHubOnContext( - r.Context(), - hub, - ) + ctx = sentry.SetHubOnContext(ctx, hub) defer h.recoverWithSentry(hub, r) handler.ServeHTTP(rw, r.WithContext(ctx)) }) @@ -65,12 +66,13 @@ func (h *Handler) Handle(handler http.Handler) http.Handler { // HandleFunc wraps http.HandleFunc and recovers from caught panics. func (h *Handler) HandleFunc(handler http.HandlerFunc) http.HandlerFunc { return func(rw http.ResponseWriter, r *http.Request) { - hub := sentry.CurrentHub().Clone() + ctx := r.Context() + hub := sentry.GetHubFromContext(ctx) + if hub == nil { + hub = sentry.CurrentHub().Clone() + } hub.Scope().SetRequest(r) - ctx := sentry.SetHubOnContext( - r.Context(), - hub, - ) + ctx = sentry.SetHubOnContext(ctx, hub) defer h.recoverWithSentry(hub, r) handler(rw, r.WithContext(ctx)) } diff --git a/iris/sentryiris.go b/iris/sentryiris.go index 26ed4eb98..d0175a479 100644 --- a/iris/sentryiris.go +++ b/iris/sentryiris.go @@ -56,7 +56,10 @@ func New(options Options) iris.Handler { } func (h *handler) handle(ctx iris.Context) { - hub := sentry.CurrentHub().Clone() + hub := sentry.GetHubFromContext(ctx.Request().Context()) + if hub == nil { + hub = sentry.CurrentHub().Clone() + } hub.Scope().SetRequest(ctx.Request()) ctx.Values().Set(valuesKey, hub) defer h.recoverWithSentry(hub, ctx.Request()) diff --git a/martini/sentrymartini.go b/martini/sentrymartini.go index 2225376fa..4c867f515 100644 --- a/martini/sentrymartini.go +++ b/martini/sentrymartini.go @@ -52,7 +52,10 @@ func New(options Options) martini.Handler { } func (h *handler) handle(rw http.ResponseWriter, r *http.Request, ctx martini.Context) { - hub := sentry.CurrentHub().Clone() + hub := sentry.GetHubFromContext(r.Context()) + if hub == nil { + hub = sentry.CurrentHub().Clone() + } hub.Scope().SetRequest(r) ctx.Map(hub) defer h.recoverWithSentry(hub, r) diff --git a/negroni/sentrynegroni.go b/negroni/sentrynegroni.go index 804270b5e..0f550aec6 100644 --- a/negroni/sentrynegroni.go +++ b/negroni/sentrynegroni.go @@ -52,10 +52,14 @@ func New(options Options) negroni.Handler { } func (h *handler) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) { - hub := sentry.CurrentHub().Clone() + ctx := r.Context() + hub := sentry.GetHubFromContext(ctx) + if hub == nil { + hub = sentry.CurrentHub().Clone() + } hub.Scope().SetRequest(r) - ctx := sentry.SetHubOnContext( - context.WithValue(r.Context(), sentry.RequestContextKey, r), + ctx = sentry.SetHubOnContext( + context.WithValue(ctx, sentry.RequestContextKey, r), hub, ) defer h.recoverWithSentry(hub, r)