Skip to content

Commit

Permalink
feat: use Hub from request context when available (#259)
Browse files Browse the repository at this point in the history
This allows users to setup a request-scoped Hub with relevant data in a
middleware that runs before the web frameworks we integrate with.

A typical case is AWS Lambda + Web Framework. See [1] for an example.

[1]: #217 (comment)
  • Loading branch information
rhcarvalho committed Jul 17, 2020
1 parent dc614c4 commit d099f87
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 20 deletions.
6 changes: 2 additions & 4 deletions echo/sentryecho.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
4 changes: 4 additions & 0 deletions fasthttp/sentryfasthttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
5 changes: 4 additions & 1 deletion gin/sentrygin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
22 changes: 12 additions & 10 deletions http/sentryhttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
})
Expand All @@ -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))
}
Expand Down
5 changes: 4 additions & 1 deletion iris/sentryiris.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
5 changes: 4 additions & 1 deletion martini/sentrymartini.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 7 additions & 3 deletions negroni/sentrynegroni.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d099f87

Please sign in to comment.