Skip to content

Commit 1e398cc

Browse files
committed
native histogram: Fix race between Write and addExemplar
Follow-up to 1608 Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
1 parent ef2f87e commit 1e398cc

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

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)