From 9e13cd253b3bae50603992351a4b034ab705904b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20St=C3=A4ber?= Date: Fri, 7 Oct 2022 23:33:39 +0200 Subject: [PATCH 1/2] Fix race condition with Exemplar in Counter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Potential fix for #1145. Signed-off-by: Fabian Stäber --- prometheus/counter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/prometheus/counter.go b/prometheus/counter.go index de30de6da..40d1b24f3 100644 --- a/prometheus/counter.go +++ b/prometheus/counter.go @@ -140,12 +140,12 @@ func (c *counter) get() float64 { } func (c *counter) Write(out *dto.Metric) error { - val := c.get() 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) } From 037699f9c26cec6b8e2ab1116967c52dd876f205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20St=C3=A4ber?= Date: Mon, 17 Oct 2022 15:42:03 +0200 Subject: [PATCH 2/2] Fix race condition with Exemplar in Counter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabian Stäber --- prometheus/counter.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/prometheus/counter.go b/prometheus/counter.go index 40d1b24f3..3668a16b3 100644 --- a/prometheus/counter.go +++ b/prometheus/counter.go @@ -140,7 +140,8 @@ func (c *counter) get() float64 { } func (c *counter) Write(out *dto.Metric) error { - + // 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)