Skip to content

Commit

Permalink
Ensure safe access to global rand.Rand
Browse files Browse the repository at this point in the history
Update rand.Rand usage to add locking around a global rand.Rand
instance, which is not safe for concurrent access by multiple
goroutines.

Fixes open-telemetry#5814.
  • Loading branch information
pkwarren committed Sep 12, 2024
1 parent a475ee2 commit e48fe3b
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions sdk/metric/internal/exemplar/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ func (r *randRes) Offer(ctx context.Context, t time.Time, n Value, a []attribute
} else {
if r.count == r.next {
// Overwrite a random existing measurement with the one offered.
rngMu.Lock()
idx := int(rng.Int63n(int64(cap(r.store))))
rngMu.Unlock()
r.store[idx] = newMeasurement(ctx, t, n, a)
r.advance()
}
Expand Down

0 comments on commit e48fe3b

Please sign in to comment.