Skip to content

Commit

Permalink
native histogram: Fix race between Write and addExemplar
Browse files Browse the repository at this point in the history
Follow-up to 1608

Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
  • Loading branch information
krajorama committed Sep 6, 2024
1 parent ef2f87e commit 1e398cc
Showing 1 changed file with 6 additions and 4 deletions.
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 1e398cc

Please sign in to comment.