Skip to main content
Version: Next

Sentry

Release Discord Test

Sentry support for Fiber.

Compatible with Fiber v3.

Go version support

We only support the latest two versions of Go. Visit https://go.dev/doc/devel/release for more information.

Install

go get -u github.com/gofiber/fiber/v3
go get -u github.com/gofiber/contrib/v3/sentry
go get -u github.com/getsentry/sentry-go

Signature

fiberSentry.New(config ...fiberSentry.Config) fiber.Handler
fiberSentry.GetHubFromContext(ctx any) *sdk.Hub // sdk "github.com/getsentry/sentry-go"
fiberSentry.MustGetHubFromContext(ctx any) *sdk.Hub // sdk "github.com/getsentry/sentry-go"

GetHubFromContext and MustGetHubFromContext each accept a fiber.Ctx, fiber.CustomCtx, *fasthttp.RequestCtx, or a standard context.Context (e.g. the value returned by c.Context() when PassLocalsToContext is enabled). The Must* variant panics if the hub is not found. *sdk.Hub is *sentry.Hub from github.com/getsentry/sentry-go.

Config

PropertyTypeDescriptionDefault
RepanicboolRepanic configures whether Sentry should repanic after recovery. Set to true, if Recover middleware is used.false
WaitForDeliveryboolWaitForDelivery configures whether you want to block the request before moving forward with the response. If Recover middleware is used, it's safe to either skip this option or set it to false.false
Timeouttime.DurationTimeout for the event delivery requests.time.Second * 2

Usage

sentry attaches an instance of *sentry.Hub (https://godoc.org/github.com/getsentry/sentry-go#Hub) to the request's context, which makes it available throughout the rest of the request's lifetime. You can access it by using the sentry.GetHubFromContext() or sentry.MustGetHubFromContext() method on the context itself in any of your proceeding middleware and routes. Keep in mind that *sentry.Hub should be used instead of the global sentry.CaptureMessage, sentry.CaptureException, or any other calls, as it keeps the separation of data between the requests.

  • Keep in mind that *sentry.Hub won't be available in middleware attached before sentry. In this case, GetHubFromContext() returns nil, and MustGetHubFromContext() will panic.
package main

import (
"fmt"
"log"

sdk "github.com/getsentry/sentry-go"
fiberSentry "github.com/gofiber/contrib/v3/sentry"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/utils"
)

func main() {
_ = sdk.Init(sdk.ClientOptions{
Dsn: "",
BeforeSend: func(event *sdk.Event, hint *sdk.EventHint) *sdk.Event {
if hint.Context != nil {
if c, ok := hint.Context.Value(sdk.RequestContextKey).(fiber.Ctx); ok {
// You have access to the original Context if it panicked
fmt.Println(utils.ImmutableString(c.Hostname()))
}
}
fmt.Println(event)
return event
},
Debug: true,
AttachStacktrace: true,
})

app := fiber.New()

app.Use(fiberSentry.New(fiberSentry.Config{
Repanic: true,
WaitForDelivery: true,
}))

enhanceSentryEvent := func(c fiber.Ctx) error {
if hub := fiberSentry.GetHubFromContext(c); hub != nil {
hub.Scope().SetTag("someRandomTag", "maybeYouNeedIt")
}
return c.Next()
}

app.All("/foo", enhanceSentryEvent, func(c fiber.Ctx) error {
panic("y tho")
})

app.All("/", func(c fiber.Ctx) error {
if hub := fiberSentry.GetHubFromContext(c); hub != nil {
hub.WithScope(func(scope *sdk.Scope) {
scope.SetExtra("unwantedQuery", "someQueryDataMaybe")
hub.CaptureMessage("User provided unwanted query string, but we recovered just fine")
})
}
return c.SendStatus(fiber.StatusOK)
})

log.Fatal(app.Listen(":3000"))
}

Accessing Context in BeforeSend callback

import (
"fmt"

"github.com/gofiber/fiber/v3"
sdk "github.com/getsentry/sentry-go"
)

sdk.Init(sdk.ClientOptions{
Dsn: "your-public-dsn",
BeforeSend: func(event *sdk.Event, hint *sdk.EventHint) *sdk.Event {
if hint.Context != nil {
if c, ok := hint.Context.Value(sdk.RequestContextKey).(fiber.Ctx); ok {
// You have access to the original Context if it panicked
fmt.Println(c.Hostname())
}
}
return event
},
})

Retrieving the hub with PassLocalsToContext

When fiber.Config{PassLocalsToContext: true} is set, the Sentry hub stored by the middleware is also available in the underlying context.Context. Use GetHubFromContext or MustGetHubFromContext with any of the supported context types:

// From a fiber.Ctx (most common usage)
hub := fiberSentry.GetHubFromContext(c)

// From the underlying context.Context (useful in service layers or when PassLocalsToContext is enabled)
hub := fiberSentry.GetHubFromContext(c.Context())

MustGetHubFromContext panics if the hub is not found (e.g. in middleware that runs before sentry):

hub := fiberSentry.MustGetHubFromContext(c)