Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure safe access to global rand.Rand #5815

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Enable exemplars by default in `go.opentelemetry.io/otel/sdk/metric`. Exemplars can be disabled by setting `OTEL_METRICS_EXEMPLAR_FILTER=always_off` (#5778)

### Fixed

- Avoid panic in metric histograms under load. (#5815)
pkwarren marked this conversation as resolved.
Show resolved Hide resolved

<!-- Released section -->
<!-- Don't change this section unless doing release -->

Expand Down
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()
pkwarren marked this conversation as resolved.
Show resolved Hide resolved
r.store[idx] = newMeasurement(ctx, t, n, a)
r.advance()
}
Expand Down
22 changes: 22 additions & 0 deletions sdk/metric/internal/exemplar/rand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,25 @@ func TestRandomConcurrentSafe(t *testing.T) {

wg.Wait()
}

func TestOfferConcurrentSafe(t *testing.T) {
const goRoutines = 10

ctx := context.Background()

var startWg, doneWg sync.WaitGroup
startWg.Add(1)
for n := 0; n < goRoutines; n++ {
doneWg.Add(1)
go func() {
defer doneWg.Done()
startWg.Wait()
r := FixedSize(10)
for i := 0; i < 100; i++ {
assert.NotPanics(t, func() { r.Offer(ctx, staticTime, NewValue(random()), nil) })
}
}()
}
startWg.Done()
doneWg.Wait()
}
Loading