Skip to content

Commit ddd7f0e

Browse files
fstabbwplotka
authored andcommitted
Fix race condition with Exemplar in Counter (#1146)
* Fix race condition with Exemplar in Counter Potential fix for #1145. Signed-off-by: Fabian Stäber <fabian@fstab.de> * Fix race condition with Exemplar in Counter Signed-off-by: Fabian Stäber <fabian@fstab.de> Signed-off-by: Fabian Stäber <fabian@fstab.de>
1 parent 1f93f64 commit ddd7f0e

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

Diff for: prometheus/counter.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,13 @@ func (c *counter) get() float64 {
140140
}
141141

142142
func (c *counter) Write(out *dto.Metric) error {
143-
val := c.get()
144-
143+
// Read the Exemplar first and the value second. This is to avoid a race condition
144+
// where users see an exemplar for a not-yet-existing observation.
145145
var exemplar *dto.Exemplar
146146
if e := c.exemplar.Load(); e != nil {
147147
exemplar = e.(*dto.Exemplar)
148148
}
149+
val := c.get()
149150

150151
return populateMetric(CounterValue, val, c.labelPairs, exemplar, out)
151152
}

0 commit comments

Comments
 (0)