sentry

package module
v1.1.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 1, 2026 License: MIT Imports: 11 Imported by: 0

README


id: sentry

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

Property Type Description Default
Repanic bool Repanic configures whether Sentry should repanic after recovery. Set to true, if Recover middleware is used. false
WaitForDelivery bool WaitForDelivery 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
Timeout time.Duration Timeout 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)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfigDefault = Config{
	Repanic:         false,
	WaitForDelivery: false,
	Timeout:         time.Second * 2,
}

ConfigDefault is the default config

Functions

func GetHubFromContext

func GetHubFromContext(ctx any) *sentry.Hub

GetHubFromContext returns the Sentry hub from context. It accepts fiber.CustomCtx, fiber.Ctx, *fasthttp.RequestCtx, and context.Context.

func MustGetHubFromContext

func MustGetHubFromContext(ctx any) *sentry.Hub

MustGetHubFromContext returns the Sentry hub from context. It accepts fiber.CustomCtx, fiber.Ctx, *fasthttp.RequestCtx, and context.Context. Panics if the hub is not found or has an unexpected type.

func New

func New(config ...Config) fiber.Handler

New creates a new middleware handler

Types

type Config

type Config struct {
	// Repanic configures whether Sentry should repanic after recovery.
	// Set to true, if Recover middleware is used.
	// https://github.com/gofiber/fiber/tree/master/middleware/recover
	// Optional. Default: false
	Repanic bool

	// WaitForDelivery 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.
	// https://github.com/gofiber/fiber/tree/master/middleware/recover
	// Optional. Default: false
	WaitForDelivery bool

	// Timeout for the event delivery requests.
	// Optional. Default: 2 Seconds
	Timeout time.Duration
}

Config defines the config for middleware.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL