Skip to content

Commit

Permalink
feat: Iris integration
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek committed Jun 10, 2019
1 parent 294eaff commit 3013272
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 25 additions & 0 deletions example/iris/main.go
Original file line number Diff line number Diff line change
@@ -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://[email protected]/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"))
}
69 changes: 69 additions & 0 deletions iris/sentryiris.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit 3013272

Please sign in to comment.