Introduce histogram purging support.
This commit is contained in:
parent
a10d055c32
commit
93130ba5c3
|
@ -13,6 +13,7 @@ import (
|
|||
"math"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
dto "github.com/prometheus/client_model/go"
|
||||
|
||||
|
@ -54,6 +55,7 @@ type HistogramSpecification struct {
|
|||
BucketBuilder BucketBuilder
|
||||
ReportablePercentiles []float64
|
||||
Starts []float64
|
||||
PurgeInterval time.Duration
|
||||
}
|
||||
|
||||
type Histogram interface {
|
||||
|
@ -82,6 +84,9 @@ type histogram struct {
|
|||
values map[uint64]*histogramVector
|
||||
// These are the percentile values that will be reported on marshalling.
|
||||
reportablePercentiles []float64
|
||||
|
||||
purgeInterval time.Duration
|
||||
lastPurge time.Time
|
||||
}
|
||||
|
||||
type histogramVector struct {
|
||||
|
@ -249,6 +254,8 @@ func formatFloat(value float64) string {
|
|||
}
|
||||
|
||||
func (h *histogram) MarshalJSON() ([]byte, error) {
|
||||
h.Purge()
|
||||
|
||||
h.mutex.RLock()
|
||||
defer h.mutex.RUnlock()
|
||||
|
||||
|
@ -274,10 +281,29 @@ func (h *histogram) MarshalJSON() ([]byte, error) {
|
|||
})
|
||||
}
|
||||
|
||||
func (h *histogram) Purge() {
|
||||
if h.purgeInterval == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
h.mutex.Lock()
|
||||
defer h.mutex.Unlock()
|
||||
if time.Since(h.lastPurge) < h.purgeInterval {
|
||||
return
|
||||
}
|
||||
|
||||
h.resetAll()
|
||||
h.lastPurge = time.Now()
|
||||
}
|
||||
|
||||
func (h *histogram) ResetAll() {
|
||||
h.mutex.Lock()
|
||||
defer h.mutex.Unlock()
|
||||
|
||||
h.resetAll()
|
||||
}
|
||||
|
||||
func (h *histogram) resetAll() {
|
||||
for signature, value := range h.values {
|
||||
for _, bucket := range value.buckets {
|
||||
bucket.Reset()
|
||||
|
@ -294,6 +320,8 @@ func NewHistogram(specification *HistogramSpecification) Histogram {
|
|||
bucketStarts: specification.Starts,
|
||||
reportablePercentiles: specification.ReportablePercentiles,
|
||||
values: map[uint64]*histogramVector{},
|
||||
lastPurge: time.Now(),
|
||||
purgeInterval: specification.PurgeInterval,
|
||||
}
|
||||
|
||||
return metric
|
||||
|
@ -307,11 +335,14 @@ func NewDefaultHistogram() Histogram {
|
|||
Starts: LogarithmicSizedBucketsFor(0, 4096),
|
||||
BucketBuilder: AccumulatingBucketBuilder(EvictAndReplaceWith(10, AverageReducer), 50),
|
||||
ReportablePercentiles: []float64{0.01, 0.05, 0.5, 0.90, 0.99},
|
||||
PurgeInterval: 15 * time.Minute,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func (metric *histogram) dumpChildren(f *dto.MetricFamily) {
|
||||
metric.Purge()
|
||||
|
||||
metric.mutex.RLock()
|
||||
defer metric.mutex.RUnlock()
|
||||
|
||||
|
|
Loading…
Reference in New Issue