From 905f815794331834b948af30f0d7339323478f8d Mon Sep 17 00:00:00 2001 From: Kegan Dougal <7190048+kegsay@users.noreply.github.com> Date: Tue, 5 Mar 2024 11:30:05 +0000 Subject: [PATCH] bugfix: ensure sentry username/id values are correct Previously there were wrong under high concurrency due to using the global hub instead of a per-request hub. --- internal/context.go | 19 ++++++++++++------- sync2/poller.go | 5 +---- sync3/handler/handler.go | 2 +- 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/internal/context.go b/internal/context.go index 09570229..e0cc3458 100644 --- a/internal/context.go +++ b/internal/context.go @@ -48,19 +48,24 @@ func RequestContext(ctx context.Context) context.Context { } // add the user ID to this request context. Need to have called RequestContext first. -func SetRequestContextUserID(ctx context.Context, userID, deviceID string) { +func AssociateUserIDWithRequest(ctx context.Context, userID, deviceID string) context.Context { d := ctx.Value(ctxData) if d == nil { - return + return ctx } da := d.(*data) da.userID = userID da.deviceID = deviceID - if hub := sentry.GetHubFromContext(ctx); hub != nil { - sentry.ConfigureScope(func(scope *sentry.Scope) { - scope.SetUser(sentry.User{Username: userID}) - }) - } + hub := sentry.GetHubFromContext(ctx) + if hub == nil { + // Basing the sentry-wrangling on the sentry-go net/http integration, see e.g. + // https://github.com/getsentry/sentry-go/blob/02e712a638c40cd9701ad52d5d1309d65d556ef9/http/sentryhttp.go#L84 + hub = sentry.CurrentHub().Clone() + } + hub.ConfigureScope(func(scope *sentry.Scope) { + scope.SetUser(sentry.User{Username: userID, ID: deviceID}) + }) + return sentry.SetHubOnContext(ctx, hub) } func SetConnBufferInfo(ctx context.Context, bufferLen, nextLen, bufferCap int) { diff --git a/sync2/poller.go b/sync2/poller.go index 44d061ed..e3e4839b 100644 --- a/sync2/poller.go +++ b/sync2/poller.go @@ -503,10 +503,7 @@ func (p *poller) Poll(since string) { // caller and passed down? hub := sentry.CurrentHub().Clone() hub.ConfigureScope(func(scope *sentry.Scope) { - scope.SetUser(sentry.User{Username: p.userID}) - scope.SetContext(internal.SentryCtxKey, map[string]interface{}{ - "device_id": p.deviceID, - }) + scope.SetUser(sentry.User{Username: p.userID, ID: p.deviceID}) }) ctx := sentry.SetHubOnContext(context.Background(), hub) diff --git a/sync3/handler/handler.go b/sync3/handler/handler.go index 01e36b9c..9ab0db77 100644 --- a/sync3/handler/handler.go +++ b/sync3/handler/handler.go @@ -385,7 +385,7 @@ func (h *SyncLiveHandler) setupConnection(req *http.Request, cancel context.Canc Str("device", token.DeviceID). Str("conn", syncReq.ConnID). Logger() - internal.SetRequestContextUserID(req.Context(), token.UserID, token.DeviceID) + req = req.WithContext(internal.AssociateUserIDWithRequest(req.Context(), token.UserID, token.DeviceID)) internal.Logf(req.Context(), "setupConnection", "identified access token as user=%s device=%s", token.UserID, token.DeviceID) // Record the fact that we've recieved a request from this token