Skip to content

Commit

Permalink
Use an atomic variable to check if exemplars are disabled
Browse files Browse the repository at this point in the history
Instead of  checking non thread safe slice capacity

Signed-off-by: György Krajcsovits <gyorgy.krajcsovits@grafana.com>
  • Loading branch information
krajorama committed Aug 31, 2024
1 parent 8fb77a3 commit 5123705
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions prometheus/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ func newHistogram(desc *Desc, opts HistogramOpts, labelValues ...string) Histogr
h.nativeHistogramZeroThreshold = DefNativeHistogramZeroThreshold
} // Leave h.nativeHistogramZeroThreshold at 0 otherwise.
h.nativeHistogramSchema = pickSchema(opts.NativeHistogramBucketFactor)
h.nativeExemplars = makeNativeExemplars(opts.NativeHistogramExemplarTTL, opts.NativeHistogramMaxExemplars)
makeNativeExemplars(&h.nativeExemplars, opts.NativeHistogramExemplarTTL, opts.NativeHistogramMaxExemplars)
}
for i, upperBound := range h.upperBounds {
if i < len(h.upperBounds)-1 {
Expand Down Expand Up @@ -1658,11 +1658,11 @@ func addAndResetCounts(hot, cold *histogramCounts) {
type nativeExemplars struct {
sync.Mutex

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

func makeNativeExemplars(ttl time.Duration, maxCount int) nativeExemplars {
func makeNativeExemplars(exemplars *nativeExemplars, ttl time.Duration, maxCount int) {
if ttl == 0 {
ttl = 5 * time.Minute
}
Expand All @@ -1673,16 +1673,16 @@ func makeNativeExemplars(ttl time.Duration, maxCount int) nativeExemplars {

if maxCount < 0 {
maxCount = 0
ttl = -1
}

return nativeExemplars{
ttl: ttl,
exemplars: make([]*dto.Exemplar, 0, maxCount),
}
exemplars.ttl.Store(int64(ttl))
exemplars.exemplars = make([]*dto.Exemplar, 0, maxCount)
}

func (n *nativeExemplars) addExemplar(e *dto.Exemplar) {
if cap(n.exemplars) == 0 {
ttl := n.ttl.Load()
if ttl == -1 {
return
}

Expand Down Expand Up @@ -1754,7 +1754,7 @@ func (n *nativeExemplars) addExemplar(e *dto.Exemplar) {
nIdx = len(n.exemplars)
}

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

0 comments on commit 5123705

Please sign in to comment.