Merge pull request #1623 from krajorama/data-race-in-histogram-write

native histogram: Fix race between Write and addExemplar
This commit is contained in:
George Krajcsovits 2024-09-07 07:57:39 +02:00 committed by GitHub
commit 05fcde9fe4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 4 deletions

View File

@ -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

View File

@ -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
}