Introduce histogram purging support.

This commit is contained in:
Matt T. Proud 2013-07-21 17:34:37 +02:00
parent a10d055c32
commit 93130ba5c3
1 changed files with 31 additions and 0 deletions

View File

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