From 3013272b2db3138258b45c4e53ca543922188771 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Mon, 10 Jun 2019 15:57:45 +0200 Subject: [PATCH] feat: Iris integration --- CHANGELOG.md | 1 + example/iris/main.go | 25 ++++++++++++++++ iris/sentryiris.go | 69 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 example/iris/main.go create mode 100644 iris/sentryiris.go diff --git a/CHANGELOG.md b/CHANGELOG.md index c88026f6c..d79ca99e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## 0.0.1-beta.3 +- feat: `Iris` framework support with `sentryiris` package - feat: `Gin` framework support with `sentrygin` package - feat: `Martini` framework support with `sentrymartini` package - feat: `Negroni` framework support with `sentrynegroni` package diff --git a/example/iris/main.go b/example/iris/main.go new file mode 100644 index 000000000..f3e5d1f54 --- /dev/null +++ b/example/iris/main.go @@ -0,0 +1,25 @@ +package main + +import ( + "github.com/getsentry/sentry-go" + sentryiris "github.com/getsentry/sentry-go/iris" + "github.com/kataras/iris" +) + +func main() { + _ = sentry.Init(sentry.ClientOptions{ + Dsn: "https://363a337c11a64611be4845ad6e24f3ac@sentry.io/297378", + Debug: true, + AttachStacktrace: true, + }) + + app := iris.Default() + app.Use(sentryiris.New(sentryiris.Options{ + Repanic: true, + WaitForDelivery: true, + }).Handle()) + app.Get("/", func(ctx iris.Context) { + panic("y tho") + }) + app.Run(iris.Addr(":3000")) +} diff --git a/iris/sentryiris.go b/iris/sentryiris.go new file mode 100644 index 000000000..3a624fcef --- /dev/null +++ b/iris/sentryiris.go @@ -0,0 +1,69 @@ +package sentryiris + +import ( + "context" + "net/http" + "time" + + "github.com/kataras/iris" + + "github.com/getsentry/sentry-go" +) + +type Handler struct { + repanic bool + waitForDelivery bool + timeout time.Duration +} + +type Options struct { + Repanic bool + WaitForDelivery bool + Timeout time.Duration +} + +func New(options Options) *Handler { + handler := Handler{ + repanic: false, + timeout: time.Second * 2, + waitForDelivery: false, + } + + if options.Repanic { + handler.repanic = true + } + + if options.WaitForDelivery { + handler.waitForDelivery = true + } + + return &handler +} + +func (h *Handler) Handle() func(ctx iris.Context) { + return func(ctx iris.Context) { + r := ctx.Request() + c := sentry.SetHubOnContext( + context.WithValue(r.Context(), sentry.RequestContextKey, r), + sentry.CurrentHub().Clone(), + ) + defer h.recoverWithSentry(c, r) + ctx.Next() + } +} + +func (h *Handler) recoverWithSentry(ctx context.Context, r *http.Request) { + if err := recover(); err != nil { + hub := sentry.GetHubFromContext(ctx) + hub.ConfigureScope(func(scope *sentry.Scope) { + scope.SetRequest(sentry.Request{}.FromHTTPRequest(r)) + }) + eventId := hub.RecoverWithContext(ctx, err) + if eventId != nil && h.waitForDelivery { + hub.Flush(h.timeout) + } + if h.repanic { + panic(err) + } + } +}