Unofficial Fiber Handler for Sentry SDK

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

Owner
Reinaldy Rafli
That's life, if we had more time, we could live forever.
Reinaldy Rafli
Similar Resources

A go sdk for baidu netdisk open platform 百度网盘开放平台 Go SDK

Pan Go Sdk 该代码库为百度网盘开放平台Go语言的SDK

Nov 22, 2022

Nextengine-sdk-go: the NextEngine SDK for the Go programming language

NextEngine SDK for Go nextengine-sdk-go is the NextEngine SDK for the Go programming language. Getting Started Install go get github.com/takaaki-s/nex

Dec 7, 2021

Commercetools-go-sdk is fork of original commercetools-go-sdk

commercetools-go-sdk The Commercetools Go SDK is automatically generated based on the official API specifications of Commercetools. It should therefor

Dec 13, 2021

Sdk-go - Go version of the Synapse SDK

synapsesdk-go Synapse Protocol's Go SDK. Currently in super duper alpha, do not

Jan 7, 2022

Redash-go-sdk - An SDK for the programmatic management of Redash, in Go

Redash-go-sdk - An SDK for the programmatic management of Redash, in Go

Redash Go SDK An SDK for the programmatic management of Redash. The main compone

Dec 13, 2022

Unofficial Google Trends API for Go

Google Trends API for Go Unofficial Google Trends API for Golang gogtrends is API wrapper which allows to get reports from Google Trends. All contribu

Nov 21, 2022

Go client for the YNAB API. Unofficial. It covers 100% of the resources made available by the YNAB API.

YNAB API Go Library This is an UNOFFICIAL Go client for the YNAB API. It covers 100% of the resources made available by the YNAB API. Installation go

Oct 6, 2022

Unofficial Anilist.co GraphQL API wrapper for GoLang.

anilistWrapGo Unofficial Anilist.co GraphQL API wrapper for GoLang. Examples All examples are present as tests in test directory. Below are a few snip

Dec 20, 2022

Avanza Unofficial GO API Client

Avanza Unofficial GO API Client Please note that I am not affiliated with Avanza Bank AB in any way. The underlying API can be taken down or changed w

Jan 7, 2023
Twilight is an unofficial Golang SDK for Twilio APIs
Twilight is an unofficial Golang SDK for Twilio APIs

Twilight is an unofficial Golang SDK for Twilio APIs. Twilight was born as a result of my inability to spell Twilio correctly. I searched for a Twillio Golang client library and couldn’t find any, I decided to build one. Halfway through building this, I realized I had spelled Twilio as Twillio when searching for a client library on Github.

Jul 2, 2021
An easy-to-use unofficial SDK for Feishu and Lark Open Platform

go-lark go-lark is an easy-to-use unofficial SDK for Feishu and Lark Open Platform. go-lark implements messaging APIs, with full-fledged supports on b

Jan 2, 2023
Unofficial SDK of official notion API in Go

notion-go A go client for the Notion API Description This aims to be an unofficial Go version of the official SDK which is written in JavaScript. Inst

May 12, 2022
Unofficial Go SDK for GoPay Payments REST API

Unofficial Go SDK for GoPay Payments REST API Installation go get https://github.com/apparently-studio/gopay-go-api Basic usage client := gopay.NewCl

Nov 18, 2022
unofficial NBA Stats API SDK in Go

nba api go (nag) is an unofficial NBA Stats API in Go. it is very much a Go port of nba_api. endpoints alltimeleadersgrids assistleaders assisttracker

Sep 23, 2022
Amplitude unofficial client for Go, inspired in their official SDK for Node

Amplitude Golang SDK Amplitude unofficial client for Go, inspired in their official SDK for Node. For reference, visit HTTP API v2 documentation. Inst

Dec 31, 2022
Unofficial getpocket.com API Golang SDK

GetPocket API Golang SDK https://getpocket.com/developer/ Example usage: package main import ( "context" "fmt" pocket "github.com/ernur-eskermes/p

Nov 28, 2021
Unofficial SDK to access for Open Threat Exchange (OTX) in Go

gotx Unofficial SDK to access for Open Threat Exchange (OTX) API in Go. Usage package main import ( "context" "fmt" "os" "github.com/m-mizutani/

Feb 12, 2022
Golang SDK for Dusupay payment gateway API (Unofficial)

Dusupay API SDK GO (Unofficial) Description Unofficial Dusupay payment gateway API Client for Go API documentation https://docs.dusupay.com/ Installat

Sep 27, 2022
Unofficial Golang SDK for TON (The Open Network)

TonWeb Unofficial Golang SDK for TON (The Open Network) Installation go get github.com/neotoolkit/tonweb Usage package main import ( "fmt" "os" "

Jun 13, 2022