1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-06-03 18:35:08 +02:00

Fix race in the lastvalue aggregation where 0 could be observed (#8056)

This initializes the value of the gauge with the correct value before it
is stored in the map. We end up storing the same thing twice on the
first call, but that isn't a big deal performance-wise.

Discovered during
https://github.com/open-telemetry/opentelemetry-go/pull/8021
This commit is contained in:
David Ashpole
2026-03-16 09:30:51 -04:00
committed by GitHub
parent b201d73b24
commit 206ac291d0
3 changed files with 4 additions and 4 deletions
+1
View File
@@ -28,6 +28,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Return spec-compliant `TraceIdRatioBased` description. This is a breaking behavioral change, but it is necessary to
make the implementation [spec-compliant](https://opentelemetry.io/docs/specs/otel/trace/sdk/#traceidratiobased). (#8027)
- Fix a race condition in `go.opentelemetry.io/otel/sdk/metric` where the lastvalue aggregation could collect the value 0 even when no zero-value measurements were recorded. (#8056)
<!-- Released section -->
<!-- Don't change this section unless doing release -->
+3 -1
View File
@@ -31,10 +31,12 @@ func (s *lastValueMap[N]) measure(
droppedAttr []attribute.KeyValue,
) {
lv := s.values.LoadOrStoreAttr(fltrAttr, func(attr attribute.Set) any {
return &lastValuePoint[N]{
p := &lastValuePoint[N]{
res: s.newRes(attr),
attrs: attr,
}
p.value.Store(value)
return p
}).(*lastValuePoint[N])
lv.value.Store(value)
@@ -490,9 +490,6 @@ func validateGauge[N int64 | float64](t *testing.T, aggs []metricdata.Aggregatio
for _, v := range getConcurrentVals[N]() {
valid[v] = true
}
// TODO(dashpole): Fix a concurrency bug where a gauge can be collected with
// the value zero even when no zero-value measurements have been recorded.
valid[0] = true
for _, agg := range aggs {
s, ok := agg.(metricdata.Gauge[N])