How the Sentry Iris Hub works
masterThe sentryiris handler attaches an instance of *sentry.Hub to the iris.Context. This ensures that Sentry data (like tags and scopes) is isolated per request and does not leak between different users or requests.
Key Rules:
- Accessing the Hub: Use
sentryiris.GetHubFromContext(ctx)to retrieve the hub within middleware or route handlers. - Avoid Global Calls: Use the retrieved
*sentry.Hub(e.g.,hub.CaptureMessage) instead of global functions likesentry.CaptureMessageto maintain request-level data separation. - Middleware Order: The
*sentry.Hubis not available in any middleware that is attached before thesentryirishandler in the middleware chain.
Example: Using the Hub in Middleware and Routes
app := iris.Default()
// Attach handler first
app.Use(sentryiris.New(sentryiris.Options{
Repanic: true,
}))
// Middleware that uses the Hub
app.Use(func(ctx iris.Context) {
if hub := sentryiris.GetHubFromContext(ctx); hub != nil {
hub.Scope().SetTag("someRandomTag", "maybeYouNeedIt")
}
ctx.Next()
})
// Route that uses the Hub
app.Get("/", func(ctx iris.Context) {
if hub := sentryiris.GetHubFromContext(ctx); hub != nil {
hub.WithScope(func(scope *sentry.Scope) {
scope.SetTag("unwantedQuery", "someQueryDataMaybe")
hub.CaptureMessage("User provided unwanted query string, but we recovered just fine")
})
}
})
app.Run(iris.Addr(":3000"))app := iris.Default()
app.Use(sentryiris.New(sentryiris.Options{
Repanic: true,
}))
app.Use(func(ctx iris.Context) {
if hub := sentryiris.GetHubFromContext(ctx); hub != nil {
hub.Scope().SetTag("someRandomTag", "maybeYouNeedIt")
}
ctx.Next()
})
app.Get("/", func(ctx iris.Context) {
if hub := sentryiris.GetHubFromContext(ctx); hub != nil {
hub.WithScope(func(scope *sentry.Scope) {
scope.SetTag("unwantedQuery", "someQueryDataMaybe")
hub.CaptureMessage("User provided unwanted query string, but we recovered just fine")
})
}
})
app.Run(iris.Addr(":3000"))