@@ -573,7 +573,7 @@ func newHistogram(desc *Desc, opts HistogramOpts, labelValues ...string) Histogr
573
573
h .nativeHistogramZeroThreshold = DefNativeHistogramZeroThreshold
574
574
} // Leave h.nativeHistogramZeroThreshold at 0 otherwise.
575
575
h .nativeHistogramSchema = pickSchema (opts .NativeHistogramBucketFactor )
576
- h .nativeExemplars = makeNativeExemplars ( opts .NativeHistogramExemplarTTL , opts .NativeHistogramMaxExemplars )
576
+ makeNativeExemplars ( & h .nativeExemplars , opts .NativeHistogramExemplarTTL , opts .NativeHistogramMaxExemplars )
577
577
}
578
578
for i , upperBound := range h .upperBounds {
579
579
if i < len (h .upperBounds )- 1 {
@@ -1658,11 +1658,11 @@ func addAndResetCounts(hot, cold *histogramCounts) {
1658
1658
type nativeExemplars struct {
1659
1659
sync.Mutex
1660
1660
1661
- ttl time. Duration
1661
+ ttl atomic. Int64 // It is a duration, but also used to check concurrently if exemplars are enabled.
1662
1662
exemplars []* dto.Exemplar
1663
1663
}
1664
1664
1665
- func makeNativeExemplars (ttl time.Duration , maxCount int ) nativeExemplars {
1665
+ func makeNativeExemplars (exemplars * nativeExemplars , ttl time.Duration , maxCount int ) {
1666
1666
if ttl == 0 {
1667
1667
ttl = 5 * time .Minute
1668
1668
}
@@ -1673,16 +1673,16 @@ func makeNativeExemplars(ttl time.Duration, maxCount int) nativeExemplars {
1673
1673
1674
1674
if maxCount < 0 {
1675
1675
maxCount = 0
1676
+ ttl = - 1
1676
1677
}
1677
1678
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 )
1682
1681
}
1683
1682
1684
1683
func (n * nativeExemplars ) addExemplar (e * dto.Exemplar ) {
1685
- if cap (n .exemplars ) == 0 {
1684
+ ttl := n .ttl .Load ()
1685
+ if ttl == - 1 {
1686
1686
return
1687
1687
}
1688
1688
@@ -1754,7 +1754,7 @@ func (n *nativeExemplars) addExemplar(e *dto.Exemplar) {
1754
1754
nIdx = len (n .exemplars )
1755
1755
}
1756
1756
1757
- if otIdx != - 1 && e .Timestamp .AsTime ().Sub (ot ) > n . ttl {
1757
+ if otIdx != - 1 && e .Timestamp .AsTime ().Sub (ot ) > time . Duration ( ttl ) {
1758
1758
rIdx = otIdx
1759
1759
} else {
1760
1760
// In the previous for loop, when calculating the closest pair of exemplars,
0 commit comments