Skip to content

Latest commit

 

History

History
59 lines (46 loc) · 1.27 KB

README.md

File metadata and controls

59 lines (46 loc) · 1.27 KB

sentrusfasthttp

Example

import (
	"github.com/orandin/sentrus"
	sentrusfasthttp "github.com/orandin/sentrus/fasthttp"
)

// Set up Sentry here

// Add hook
logrus.AddHook(sentrus.NewHook(
    []logrus.Level{logrus.WarnLevel, logrus.ErrorLevel},
    sentrus.WithCustomCaptureLog(sentrusfasthttp.CaptureLog),
))

sentryHandler := sentryfasthttp.New(sentryfasthttp.Options{
    Repanic: true,
    WaitForDelivery: true,
})

enhanceSentryEvent := func (handler fasthttp.RequestHandler) fasthttp.RequestHandler {
	return func(ctx *fasthttp.RequestCtx) {
		if hub := sentry.GetHubFromContext(ctx); hub != nil {
			hub.Scope().SetTag("someRandomTag", "maybeYouNeedIt")
		}
		handler(ctx)
	}
}

defaultHandler := enhanceSentryEvent(func (ctx *fasthttp.RequestCtx) {
	logHandler := logrus.WithContext(ctx)
	logHandler.Warn("Ceci est un test")

	ctx.SetStatusCode(fasthttp.StatusOK)
})

fooHandler := enhanceSentryEvent(func (ctx *fasthttp.RequestCtx) {
	panic("y tho")
})

fastHTTPHandler := func (ctx *fasthttp.RequestCtx) {
	switch string(ctx.Path()) {
	case "/foo":
		fooHandler(ctx)
	default:
		defaultHandler(ctx)
	}
}

fmt.Println("Listening and serving HTTP on :3000")

if err := fasthttp.ListenAndServe(":3000", sentryHandler.Handle(fastHTTPHandler)); err != nil {
    panic(err)
}