1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

chore: exclude static endpoints from sentry tracing

chore: include user info to sentry tracing again
This commit is contained in:
Ferdinand Mütsch
2021-04-29 21:19:43 +02:00
parent 37d4d58b57
commit 5a8287a06b
3 changed files with 43 additions and 4 deletions

31
middlewares/sentry.go Normal file
View File

@@ -0,0 +1,31 @@
package middlewares
import (
"context"
"github.com/getsentry/sentry-go"
sentryhttp "github.com/getsentry/sentry-go/http"
"net/http"
)
// SentryMiddleware is a wrapper around sentryhttp to include user information to traces
type SentryMiddleware struct {
handler http.Handler
}
func NewSentryMiddleware() func(http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
return sentryhttp.New(sentryhttp.Options{
Repanic: true,
}).Handle(&SentryMiddleware{handler: h})
}
}
func (h *SentryMiddleware) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := context.WithValue(r.Context(), "-", "-")
h.handler.ServeHTTP(w, r.WithContext(ctx))
if hub := sentry.GetHubFromContext(ctx); hub != nil {
if user := GetPrincipal(r); user != nil {
hub.Scope().SetUser(sentry.User{ID: user.ID})
}
}
}