sentryfiber

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2021 License: MIT Imports: 10 Imported by: 5

README

Fiber Handler for Sentry SDK

GitHub release (latest SemVer including pre-releases) Go Reference Go Report Card GitHub codecov CodeFactor Codacy Badge Build test

Welcome to another "I can't find one, so I made one" episode of mine.

Installation

$ go get github.com/aldy505/sentry-fiber
import "github.com/aldy505/sentry-fiber"

Usage

import (
    "log"

    "github.com/gofiber/fiber/v2"
    "github.com/getsentry/sentry-go"
    sentryfiber "github.com/aldy505/sentry-fiber"
)

func main() {
  // Note that you'll need to have sentry-go in there.
  err := sentry.Init(sentry.ClientOptions{
    Dsn: "your-public-dsn",
  })
  if err != nil {
    log.Fatalln("sentry initialization failed")
  }

  app := fiber.New()
  app.Use(sentryfiber.New(sentryfiber.Options{}))

  app.Get("/", func (c *fiber.Ctx) error {
    return c.Send(fiber.StatusOK).Send([]byte("Hi there"))
  })

  app.Listen(":5000")
}
Sentry Hub

sentryfiber attaches an instance of *sentry.Hub (https://godoc.org/github.com/getsentry/sentry-go#Hub) to the Request's Local context (c.Locals()), which makes it available throughout the rest of the request's lifetime. You can access it by using the sentryfiber.GetHubFromContext() method on the context itself in any of your proceeding middleware and routes. 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.

Keep in mind that *sentry.Hub won't be available in middleware attached before to sentryfiber!

app := fiber.New()

app.Use(sentryfiber.New(sentryfiber.Options{
    Repanic: true,
}))

app.Use(func() fiber.Handler {
  return func(c *fiber.Ctx) error {
    if hub := sentryfiber.GetHubFromContext(ctx); hub != nil {
        hub.Scope().SetTag("someRandomTag", "maybeYouNeedIt")
    }
    return c.Next()
  }
})

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

app.Get("/foo", func(c *fiber.Ctx) error {
    // sentryfiber handler will catch it just fine. Also, because we attached "someRandomTag"
    // in the middleware before, it will be sent through as well
    panic("y tho")
    return nil
})

app.Listen(":3000")

Configuration

This is the configuration available for sentryfiber.Options{} struct.

// Whether Sentry should repanic after recovery, in most cases it should be set to true,
// as gin.Default includes its own Recovery middleware that handles http responses.
Repanic         bool
// Whether you want to block the request before moving forward with the response.
// Because Gin's default `Recovery` handler doesn't restart the application,
// it's safe to either skip this option or set it to `false`.
WaitForDelivery bool
// Timeout for the event delivery requests.
Timeout         time.Duration

Contribute

Yes please! I'm still new to Go and I create this module (or package if you will) to help me fulfill a need on my project. Feel free to refactor, add new feature, fix unknown bugs, and have fun!

License

See LICENSE

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetHubFromContext

func GetHubFromContext(c *fiber.Ctx) *sentry.Hub

GetHubFromContext retrieves attached *sentry.Hub instance from fiber.Ctx.

func New

func New(options Options) fiber.Handler

New initialize Fiber Handler for Sentry. Example:

err := sentry.Init(sentry.ClientOptions{
	Dsn: "your-public-dsn",
})
if err != nil {
	log.Fatalln("sentry initialization failed")
}

app := fiber.New()
app.Use(sentryfiber.New(sentryfiber.Options{}))

Types

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

func (*Handler) Handle

func (h *Handler) Handle(c *fiber.Ctx) error

Handle wraps fiber.Ctx and recovers from caught panics.

type Options

type Options struct {
	// Repanic configures whether Sentry should repanic after recovery, in most cases it should be set to true,
	// as Fiber.Default includes it's own Recovery middleware what handles http responses.
	Repanic bool
	// WaitForDelivery configures whether you want to block the request before moving forward with the response.
	// Because Fiber's default Recovery handler doesn't restart the application,
	// it's safe to either skip this option or set it to false.
	WaitForDelivery bool
	// Timeout for the event delivery requests.
	Timeout time.Duration
}

Jump to

Keyboard shortcuts

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