Skip to content

Commit

Permalink
bugfix: ensure sentry username/id values are correct
Browse files Browse the repository at this point in the history
Previously there were wrong under high concurrency due to
using the global hub instead of a per-request hub.
  • Loading branch information
kegsay committed Mar 5, 2024
1 parent 30f8c5b commit 905f815
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
19 changes: 12 additions & 7 deletions internal/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 1 addition & 4 deletions sync2/poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion sync3/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 905f815

Please sign in to comment.