From 1e398ccb1259d20836e3003885bdd949cb21e635 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gy=C3=B6rgy=20Krajcsovits?= Date: Fri, 6 Sep 2024 11:01:04 +0200 Subject: [PATCH 1/2] native histogram: Fix race between Write and addExemplar MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Follow-up to 1608 Signed-off-by: György Krajcsovits --- prometheus/histogram.go | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/prometheus/histogram.go b/prometheus/histogram.go index 8a4f49a..519db34 100644 --- a/prometheus/histogram.go +++ b/prometheus/histogram.go @@ -844,9 +844,7 @@ func (h *histogram) Write(out *dto.Metric) error { }} } - // If exemplars are not configured, the cap will be 0. - // So append is not needed in this case. - if cap(h.nativeExemplars.exemplars) > 0 { + if h.nativeExemplars.isEnabled() { h.nativeExemplars.Lock() his.Exemplars = append(his.Exemplars, h.nativeExemplars.exemplars...) h.nativeExemplars.Unlock() @@ -1665,6 +1663,10 @@ type nativeExemplars struct { exemplars []*dto.Exemplar } +func (n *nativeExemplars) isEnabled() bool { + return n.ttl != -1 +} + func makeNativeExemplars(ttl time.Duration, maxCount int) nativeExemplars { if ttl == 0 { ttl = 5 * time.Minute @@ -1686,7 +1688,7 @@ func makeNativeExemplars(ttl time.Duration, maxCount int) nativeExemplars { } func (n *nativeExemplars) addExemplar(e *dto.Exemplar) { - if n.ttl == -1 { + if !n.isEnabled() { return } From 209f4c041ed1764866f44dd053a8d94aa051c610 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gy=C3=B6rgy=20Krajcsovits?= Date: Fri, 6 Sep 2024 12:41:51 +0200 Subject: [PATCH 2/2] Add changelog MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: György Krajcsovits --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c55343..f390ed8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## Unreleased +* [BUGFIX] histograms: Fix possible data race when appending exemplars vs metrics gather. #1623 + ## 1.20.3 / 2024-09-05 * [BUGFIX] histograms: Fix possible data race when appending exemplars. #1608