-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9ade8e9
commit 294eaff
Showing
3 changed files
with
100 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |