Skip to content

Commit d490cc4

Browse files
dashpoleMrAlias
andauthored
Update sdk/metric/internal/aggregate/sum.go
Co-authored-by: Tyler Yahn <[email protected]>
1 parent c1a0674 commit d490cc4

File tree

1 file changed

+5
-4
lines changed
  • sdk/metric/internal/aggregate

1 file changed

+5
-4
lines changed

sdk/metric/internal/aggregate/sum.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ func (s *valueMap[N]) measure(ctx context.Context, value N, fltrAttr attribute.S
5757
}
5858
v, ok := s.values[hotIdx].Load(fltrAttr.Equivalent())
5959
if !ok {
60-
// It is possible to exceed the attribute limit if it races with other
61-
// new attribute sets. This is an accepted tradeoff to avoid locking
62-
// for writes.
63-
if s.aggLimit > 0 && s.len[hotIdx].Load() >= int64(s.aggLimit-1) {
60+
// Pre-increment, reserve a slot first.
61+
newLen := s.len[hotIdx].Add(1)
62+
if s.aggLimit > 0 && newLen >= int64(s.aggLimit) {
63+
// We exceeded the limit, decrement back and use overflow
64+
s.len[hotIdx].Add(-1)
6465
fltrAttr = overflowSet
6566
}
6667
var loaded bool

0 commit comments

Comments
 (0)