Skip to content

Commit 5123705

Browse files
committed
Use an atomic variable to check if exemplars are disabled
Instead of checking non thread safe slice capacity Signed-off-by: György Krajcsovits <[email protected]>
1 parent 8fb77a3 commit 5123705

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

prometheus/histogram.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ func newHistogram(desc *Desc, opts HistogramOpts, labelValues ...string) Histogr
573573
h.nativeHistogramZeroThreshold = DefNativeHistogramZeroThreshold
574574
} // Leave h.nativeHistogramZeroThreshold at 0 otherwise.
575575
h.nativeHistogramSchema = pickSchema(opts.NativeHistogramBucketFactor)
576-
h.nativeExemplars = makeNativeExemplars(opts.NativeHistogramExemplarTTL, opts.NativeHistogramMaxExemplars)
576+
makeNativeExemplars(&h.nativeExemplars, opts.NativeHistogramExemplarTTL, opts.NativeHistogramMaxExemplars)
577577
}
578578
for i, upperBound := range h.upperBounds {
579579
if i < len(h.upperBounds)-1 {
@@ -1658,11 +1658,11 @@ func addAndResetCounts(hot, cold *histogramCounts) {
16581658
type nativeExemplars struct {
16591659
sync.Mutex
16601660

1661-
ttl time.Duration
1661+
ttl atomic.Int64 // It is a duration, but also used to check concurrently if exemplars are enabled.
16621662
exemplars []*dto.Exemplar
16631663
}
16641664

1665-
func makeNativeExemplars(ttl time.Duration, maxCount int) nativeExemplars {
1665+
func makeNativeExemplars(exemplars *nativeExemplars, ttl time.Duration, maxCount int) {
16661666
if ttl == 0 {
16671667
ttl = 5 * time.Minute
16681668
}
@@ -1673,16 +1673,16 @@ func makeNativeExemplars(ttl time.Duration, maxCount int) nativeExemplars {
16731673

16741674
if maxCount < 0 {
16751675
maxCount = 0
1676+
ttl = -1
16761677
}
16771678

1678-
return nativeExemplars{
1679-
ttl: ttl,
1680-
exemplars: make([]*dto.Exemplar, 0, maxCount),
1681-
}
1679+
exemplars.ttl.Store(int64(ttl))
1680+
exemplars.exemplars = make([]*dto.Exemplar, 0, maxCount)
16821681
}
16831682

16841683
func (n *nativeExemplars) addExemplar(e *dto.Exemplar) {
1685-
if cap(n.exemplars) == 0 {
1684+
ttl := n.ttl.Load()
1685+
if ttl == -1 {
16861686
return
16871687
}
16881688

@@ -1754,7 +1754,7 @@ func (n *nativeExemplars) addExemplar(e *dto.Exemplar) {
17541754
nIdx = len(n.exemplars)
17551755
}
17561756

1757-
if otIdx != -1 && e.Timestamp.AsTime().Sub(ot) > n.ttl {
1757+
if otIdx != -1 && e.Timestamp.AsTime().Sub(ot) > time.Duration(ttl) {
17581758
rIdx = otIdx
17591759
} else {
17601760
// In the previous for loop, when calculating the closest pair of exemplars,

0 commit comments

Comments
 (0)