From a850a2dd15b50fbee5010ef125cf0ab7d0570288 Mon Sep 17 00:00:00 2001 From: Kegan Dougal Date: Tue, 23 Mar 2021 18:21:59 +0000 Subject: [PATCH] Use HTTP Sentry properly maybe --- internal/httputil/httpapi.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/internal/httputil/httpapi.go b/internal/httputil/httpapi.go index c69468e6c..f36e1c860 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,20 @@ 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) + } - 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) }