From 93130ba5c3bd52e920b7709c12c2a9a7cfb4af70 Mon Sep 17 00:00:00 2001 From: "Matt T. Proud" Date: Sun, 21 Jul 2013 17:34:37 +0200 Subject: [PATCH] Introduce histogram purging support. --- prometheus/histogram.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/prometheus/histogram.go b/prometheus/histogram.go index 5a806bb..3d5f9e7 100644 --- a/prometheus/histogram.go +++ b/prometheus/histogram.go @@ -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()