Skip to content

Commit

Permalink
feat: Gin integration
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek committed Jun 10, 2019
1 parent 9ade8e9 commit 294eaff
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 1 deletion.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

## 0.0.1-beta.3

- feat: Add `Negroni` framework handler in `sentrynagroni` package
- feat: `Gin` framework support with `sentrygin` package
- feat: `Martini` framework support with `sentrymartini` package
- feat: `Negroni` framework support with `sentrynegroni` package
- feat: Add `Hub.Clone()` for easier frameworks integration
- feat: Return `EventID` from `Recovery` methods
- feat: Add `NewScope` and `NewEvent` functions and use them in the whole codebase
- feat: Add `AddEventProcessor` to the `Client`
- ref: Try to read source files from the root directory, based on the filename as well, to make it work on AWS Lambda
Expand All @@ -11,6 +15,7 @@
- ref: **[breaking]** Allow for integrations to live on the client, by passing client instance in `SetupOnce` method
- ref: **[breaking]** Remove `GetIntegration` from the `Hub`
- ref: **[breaking]** Remove `GlobalEventProcessors` getter from the public API
- fix: Operate on requests body copy instead of the original

## 0.0.1-beta.2

Expand Down
25 changes: 25 additions & 0 deletions example/gin/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package main

import (
"github.com/getsentry/sentry-go"
sentrygin "github.com/getsentry/sentry-go/gin"
"github.com/gin-gonic/gin"
)

func main() {
_ = sentry.Init(sentry.ClientOptions{
Dsn: "https://[email protected]/297378",
Debug: true,
AttachStacktrace: true,
})

r := gin.Default()
r.Use(sentrygin.New(sentrygin.Options{
Repanic: true,
WaitForDelivery: true,
}).Handle())
r.GET("/", func(c *gin.Context) {
panic("y tho")
})
r.Run(":3000")
}
69 changes: 69 additions & 0 deletions gin/sentrygin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package sentrygin

import (
"context"
"net/http"
"time"

"github.com/getsentry/sentry-go"
"github.com/gin-gonic/gin"
)

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() gin.HandlerFunc {
return func(c *gin.Context) {
r := c.Copy().Request
ctx := sentry.SetHubOnContext(
context.WithValue(r.Context(), sentry.RequestContextKey, r),
sentry.CurrentHub().Clone(),
)
defer h.recoverWithSentry(ctx, r)
c.Request = r.WithContext(ctx)
c.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 294eaff

Please sign in to comment.