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.
This commit is contained in:
Kegan Dougal 2024-03-05 11:30:05 +00:00
parent 30f8c5b308
commit 905f815794
3 changed files with 13 additions and 11 deletions

View File

@ -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) {

View File

@ -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)

View File

@ -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