|
| 1 | +<p align="center"> |
| 2 | + <a href="https://sentry.io" target="_blank" align="center"> |
| 3 | + <img src="https://sentry-brand.storage.googleapis.com/sentry-logo-black.png" width="280"> |
| 4 | + </a> |
| 5 | + <br /> |
| 6 | +</p> |
| 7 | + |
| 8 | +# Official Sentry fiber Handler for Sentry-go SDK |
| 9 | + |
| 10 | +**Godoc:** https://godoc.org/github.com/getsentry/sentry-go/fiber |
| 11 | + |
| 12 | +**Example:** https://github.com/getsentry/sentry-go/tree/master/example/fiber |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +```sh |
| 17 | +go get github.com/getsentry/sentry-go/fiber |
| 18 | +``` |
| 19 | + |
| 20 | +```go |
| 21 | +import ( |
| 22 | + "fmt" |
| 23 | + "github.com/gofiber/fiber/v2" |
| 24 | + "github.com/getsentry/sentry-go" |
| 25 | + sentryfiber "github.com/getsentry/sentry-go/fiber" |
| 26 | + "github.com/gofiber/fiber/v2/utils" |
| 27 | +) |
| 28 | +``` |
| 29 | + |
| 30 | +To initialize Sentry's handler, you need to initialize Sentry itself beforehand |
| 31 | + |
| 32 | +```go |
| 33 | +if err := sentry.Init(sentry.ClientOptions{ |
| 34 | + Dsn: "your-public-dsn", |
| 35 | +}); err != nil { |
| 36 | + fmt.Printf("Sentry initialization failed: %v\n", err) |
| 37 | +} |
| 38 | + |
| 39 | +// Create an instance of sentryfiber |
| 40 | +sentryHandler := sentryfiber.New(sentryfiber.Options{}) |
| 41 | + |
| 42 | +// Once it's done, you can attach the handler as one of your middlewares |
| 43 | +app := fiber.New() |
| 44 | + |
| 45 | +app.Use(sentryHandler) |
| 46 | + |
| 47 | +// And run it |
| 48 | +app.Listen(":3000") |
| 49 | +``` |
| 50 | + |
| 51 | +## Configuration |
| 52 | + |
| 53 | +`sentryfiber` accepts a struct of `Options` that allows you to configure how the handler will behave. |
| 54 | + |
| 55 | +Currently it respects 3 options: |
| 56 | + |
| 57 | +```go |
| 58 | +// Repanic configures whether Sentry should repanic after recovery, in most cases it should be set to false, |
| 59 | +// as fasthttp doesn't include it's own Recovery handler. |
| 60 | +Repanic bool |
| 61 | +// WaitForDelivery configures whether you want to block the request before moving forward with the response. |
| 62 | +// Because fasthttp doesn't include it's own `Recovery` handler, it will restart the application, |
| 63 | +// and event won't be delivered otherwise. |
| 64 | +WaitForDelivery bool |
| 65 | +// Timeout for the event delivery requests. |
| 66 | +Timeout time.Duration |
| 67 | +``` |
| 68 | + |
| 69 | +## Usage |
| 70 | + |
| 71 | +`sentryfiber` 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. |
| 72 | +You can access it by using the `sentryfiber.GetHubFromContext()` method on the context itself in any of your proceeding middleware and routes. |
| 73 | +And it 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. |
| 74 | + |
| 75 | +**Keep in mind that `*sentry.Hub` won't be available in middleware attached before to `sentryfiber`!** |
| 76 | + |
| 77 | +```go |
| 78 | +// Later in the code |
| 79 | +sentryHandler := sentryfiber.New(sentryfiber.Options{ |
| 80 | + Repanic: true, |
| 81 | + WaitForDelivery: true, |
| 82 | +}) |
| 83 | + |
| 84 | +enhanceSentryEvent := func(ctx *fiber.Ctx) { |
| 85 | + if hub := sentryfiber.GetHubFromContext(ctx); hub != nil { |
| 86 | + hub.Scope().SetTag("someRandomTag", "maybeYouNeedIt") |
| 87 | + } |
| 88 | + ctx.Next() |
| 89 | +} |
| 90 | + |
| 91 | +app := fiber.New() |
| 92 | + |
| 93 | +app.Use(sentryHandler) |
| 94 | + |
| 95 | +app.All("/foo", enhanceSentryEvent, func(ctx *fiber.Ctx) { |
| 96 | + panic("y tho") |
| 97 | +}) |
| 98 | + |
| 99 | +app.All("/", func(ctx *fiber.Ctx) { |
| 100 | + if hub := sentryfiber.GetHubFromContext(ctx); hub != nil { |
| 101 | + hub.WithScope(func(scope *sentry.Scope) { |
| 102 | + scope.SetExtra("unwantedQuery", "someQueryDataMaybe") |
| 103 | + hub.CaptureMessage("User provided unwanted query string, but we recovered just fine") |
| 104 | + }) |
| 105 | + } |
| 106 | + ctx.Status(fiber.StatusOK) |
| 107 | +}) |
| 108 | + |
| 109 | +app.Listen(":3000") |
| 110 | +``` |
| 111 | + |
| 112 | +### Accessing Context in `BeforeSend` callback |
| 113 | + |
| 114 | +```go |
| 115 | +sentry.Init(sentry.ClientOptions{ |
| 116 | + Dsn: "your-public-dsn", |
| 117 | + BeforeSend: func(event *sentry.Event, hint *sentry.EventHint) *sentry.Event { |
| 118 | + if hint.Context != nil { |
| 119 | + if ctx, ok := hint.Context.Value(sentry.RequestContextKey).(*fiber.Ctx); ok { |
| 120 | + // You have access to the original Context if it panicked |
| 121 | + fmt.Println(ctx.Hostname()) |
| 122 | + } |
| 123 | + } |
| 124 | + return event |
| 125 | + }, |
| 126 | +}) |
| 127 | +``` |
0 commit comments