Skip to content

Commit e4dfbac

Browse files
committed
switch to atomic.Pointer[measurement]
1 parent c643a4d commit e4dfbac

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

sdk/metric/exemplar/fixed_size_reservoir_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,12 @@ func TestNewFixedSizeReservoirSamplingCorrectness(t *testing.T) {
4545
}
4646

4747
var sum float64
48-
for _, val := range r.measurements {
49-
loaded := val.Load()
50-
if loaded == nil {
48+
for i := range r.measurements {
49+
m := r.measurements[i].Load()
50+
if m == nil {
5151
continue
5252
}
53-
m := loaded.(*measurement)
54-
if m != nil {
55-
sum += m.Value.Float64()
56-
}
53+
sum += m.Value.Float64()
5754
}
5855
mean := sum / float64(sampleSize)
5956

sdk/metric/exemplar/storage.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type storage struct {
2323
}
2424

2525
func newStorage(n int) *storage {
26-
return &storage{measurements: make([]atomic.Value, n)}
26+
return &storage{measurements: make([]atomic.Pointer[measurement], n)}
2727
}
2828

2929
func (r *storage) store(idx int, m *measurement) {
@@ -39,18 +39,17 @@ func (r *storage) store(idx int, m *measurement) {
3939
func (r *storage) Collect(dest *[]Exemplar) {
4040
*dest = reset(*dest, len(r.measurements), len(r.measurements))
4141
var n int
42-
for _, val := range r.measurements {
42+
for i := range r.measurements {
4343
// For performance reasons, this iterates over measurements
4444
// concurrently with new measurements being written. This means we do
4545
// not get a point-in-time snapshot of the state of the reservoir.
4646
// This means that for sequential Offer calls, a later Offer call may
4747
// be collected and an earlier call not collected if they are written
4848
// to different indices.
49-
loaded := val.Load()
50-
if loaded == nil {
49+
m := r.measurements[i].Load()
50+
if m == nil {
5151
continue
5252
}
53-
m := loaded.(*measurement)
5453
if !m.valid {
5554
continue
5655
}

0 commit comments

Comments
 (0)