diff options
author | Kegsay <kegan@matrix.org> | 2021-03-24 10:25:24 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-24 10:25:24 +0000 |
commit | af41f6d4549759afd7f52f780b40abe2834ab4c0 (patch) | |
tree | c13e5d27221981ac438e5880f5c93ae8999f596b /internal/httputil | |
parent | 802f1c96f804f7a146e4e12e25b20c980a6af870 (diff) |
Add Sentry support (#1803)
* Add Sentry support
* Use HTTP Sentry properly maybe
* Capture panics
* Log fed Sentry stuff correctly
* British english linter
Diffstat (limited to 'internal/httputil')
-rw-r--r-- | internal/httputil/httpapi.go | 50 |
1 files changed, 47 insertions, 3 deletions
diff --git a/internal/httputil/httpapi.go b/internal/httputil/httpapi.go index c69468e6..704bdecb 100644 --- a/internal/httputil/httpapi.go +++ b/internal/httputil/httpapi.go @@ -16,6 +16,7 @@ package httputil import ( "context" + "fmt" "io" "net/http" "net/http/httptest" @@ -25,6 +26,7 @@ import ( "sync" "time" + "github.com/getsentry/sentry-go" "github.com/gorilla/mux" "github.com/matrix-org/dendrite/clientapi/auth" federationsenderAPI "github.com/matrix-org/dendrite/federationsender/api" @@ -59,8 +61,29 @@ func MakeAuthAPI( logger := util.GetLogger((req.Context())) logger = logger.WithField("user_id", device.UserID) req = req.WithContext(util.ContextWithLogger(req.Context(), logger)) + // add the user to Sentry, if enabled + hub := sentry.GetHubFromContext(req.Context()) + if hub != nil { + hub.Scope().SetTag("user_id", device.UserID) + hub.Scope().SetTag("device_id", device.ID) + } + defer func() { + if r := recover(); r != nil { + if hub != nil { + hub.CaptureException(fmt.Errorf("%s panicked", req.URL.Path)) + } + // re-panic to return the 500 + panic(r) + } + }() - return f(req, device) + jsonRes := f(req, device) + // do not log 4xx as errors as they are client fails, not server fails + if hub != nil && jsonRes.Code >= 500 { + hub.Scope().SetExtra("response", jsonRes) + hub.CaptureException(fmt.Errorf("%s returned HTTP %d", req.URL.Path, jsonRes.Code)) + } + return jsonRes } return MakeExternalAPI(metricsName, h) } @@ -195,13 +218,34 @@ func MakeFedAPI( if fedReq == nil { return errResp } + // add the user to Sentry, if enabled + hub := sentry.GetHubFromContext(req.Context()) + if hub != nil { + hub.Scope().SetTag("origin", string(fedReq.Origin())) + hub.Scope().SetTag("uri", fedReq.RequestURI()) + } + defer func() { + if r := recover(); r != nil { + if hub != nil { + hub.CaptureException(fmt.Errorf("%s panicked", req.URL.Path)) + } + // re-panic to return the 500 + panic(r) + } + }() go wakeup.Wakeup(req.Context(), fedReq.Origin()) vars, err := URLDecodeMapValues(mux.Vars(req)) if err != nil { - return util.ErrorResponse(err) + return util.MatrixErrorResponse(400, "M_UNRECOGNISED", "badly encoded query params") } - return f(req, fedReq, vars) + jsonRes := f(req, fedReq, vars) + // do not log 4xx as errors as they are client fails, not server fails + if hub != nil && jsonRes.Code >= 500 { + hub.Scope().SetExtra("response", jsonRes) + hub.CaptureException(fmt.Errorf("%s returned HTTP %d", req.URL.Path, jsonRes.Code)) + } + return jsonRes } return MakeExternalAPI(metricsName, h) } |