Skip to content

Commit 05fcde9

Browse files
authored
Merge pull request #1623 from krajorama/data-race-in-histogram-write
native histogram: Fix race between Write and addExemplar
2 parents ef2f87e + 209f4c0 commit 05fcde9

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Unreleased
22

3+
* [BUGFIX] histograms: Fix possible data race when appending exemplars vs metrics gather. #1623
4+
35
## 1.20.3 / 2024-09-05
46

57
* [BUGFIX] histograms: Fix possible data race when appending exemplars. #1608

prometheus/histogram.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -844,9 +844,7 @@ func (h *histogram) Write(out *dto.Metric) error {
844844
}}
845845
}
846846

847-
// If exemplars are not configured, the cap will be 0.
848-
// So append is not needed in this case.
849-
if cap(h.nativeExemplars.exemplars) > 0 {
847+
if h.nativeExemplars.isEnabled() {
850848
h.nativeExemplars.Lock()
851849
his.Exemplars = append(his.Exemplars, h.nativeExemplars.exemplars...)
852850
h.nativeExemplars.Unlock()
@@ -1665,6 +1663,10 @@ type nativeExemplars struct {
16651663
exemplars []*dto.Exemplar
16661664
}
16671665

1666+
func (n *nativeExemplars) isEnabled() bool {
1667+
return n.ttl != -1
1668+
}
1669+
16681670
func makeNativeExemplars(ttl time.Duration, maxCount int) nativeExemplars {
16691671
if ttl == 0 {
16701672
ttl = 5 * time.Minute
@@ -1686,7 +1688,7 @@ func makeNativeExemplars(ttl time.Duration, maxCount int) nativeExemplars {
16861688
}
16871689

16881690
func (n *nativeExemplars) addExemplar(e *dto.Exemplar) {
1689-
if n.ttl == -1 {
1691+
if !n.isEnabled() {
16901692
return
16911693
}
16921694

0 commit comments

Comments
 (0)