Skip to content

Commit

Permalink
Fix race condition with Exemplar in Counter (#1146)
Browse files Browse the repository at this point in the history
* Fix race condition with Exemplar in Counter

Potential fix for #1145.

Signed-off-by: Fabian Stäber <[email protected]>

* Fix race condition with Exemplar in Counter

Signed-off-by: Fabian Stäber <[email protected]>

Signed-off-by: Fabian Stäber <[email protected]>
  • Loading branch information
fstab authored and bwplotka committed Nov 1, 2022
1 parent 1f93f64 commit ddd7f0e
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions prometheus/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,13 @@ func (c *counter) get() float64 {
}

func (c *counter) Write(out *dto.Metric) error {
val := c.get()

// Read the Exemplar first and the value second. This is to avoid a race condition
// where users see an exemplar for a not-yet-existing observation.
var exemplar *dto.Exemplar
if e := c.exemplar.Load(); e != nil {
exemplar = e.(*dto.Exemplar)
}
val := c.get()

return populateMetric(CounterValue, val, c.labelPairs, exemplar, out)
}
Expand Down

0 comments on commit ddd7f0e

Please sign in to comment.