Skip to content

Commit

Permalink
Merge pull request #1623 from krajorama/data-race-in-histogram-write
Browse files Browse the repository at this point in the history
native histogram: Fix race between Write and addExemplar
  • Loading branch information
krajorama committed Sep 7, 2024
2 parents ef2f87e + 209f4c0 commit 05fcde9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 6 additions & 4 deletions prometheus/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand All @@ -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
}

Expand Down

0 comments on commit 05fcde9

Please sign in to comment.