From 0b5fe611227ec24e4a8b9be9bc399fb26ffafb9b Mon Sep 17 00:00:00 2001 From: Paul Banks Date: Tue, 24 May 2022 21:30:43 +0100 Subject: [PATCH] Refactor Shutdown into a separate interface to revert breaking change. --- inmem.go | 4 ---- metrics.go | 6 ++++-- prometheus/prometheus.go | 4 ---- sink.go | 9 +++++++-- 4 files changed, 11 insertions(+), 12 deletions(-) diff --git a/inmem.go b/inmem.go index a52d0ff..7c427ac 100644 --- a/inmem.go +++ b/inmem.go @@ -230,10 +230,6 @@ func (i *InmemSink) AddSampleWithLabels(key []string, val float32, labels []Labe agg.Ingest(float64(val), i.rateDenom) } -func (i *InmemSink) Shutdown() { - // Do nothing. InmemSink does not have cleanup associated with shutdown. -} - // Data is used to retrieve all the aggregated metrics // Intervals may be in use, and a read lock should be acquired func (i *InmemSink) Data() []*IntervalMetrics { diff --git a/metrics.go b/metrics.go index 047657b..36642a4 100644 --- a/metrics.go +++ b/metrics.go @@ -5,7 +5,7 @@ import ( "strings" "time" - "github.com/hashicorp/go-immutable-radix" + iradix "github.com/hashicorp/go-immutable-radix" ) type Label struct { @@ -173,7 +173,9 @@ func (m *Metrics) UpdateFilterAndLabels(allow, block, allowedLabels, blockedLabe } func (m *Metrics) Shutdown() { - m.sink.Shutdown() + if ss, ok := m.sink.(ShutdownSink); ok { + ss.Shutdown() + } } // labelIsAllowed return true if a should be included in metric diff --git a/prometheus/prometheus.go b/prometheus/prometheus.go index f89cfd9..d8221fb 100644 --- a/prometheus/prometheus.go +++ b/prometheus/prometheus.go @@ -394,10 +394,6 @@ func (p *PrometheusSink) IncrCounterWithLabels(parts []string, val float32, labe } } -// Shutdown is not implemented. PrometheusSink is in memory storage. -func (p *PrometheusSink) Shutdown() { -} - // PrometheusPushSink wraps a normal prometheus sink and provides an address and facilities to export it to an address // on an interval. type PrometheusPushSink struct { diff --git a/sink.go b/sink.go index b839844..6f4108f 100644 --- a/sink.go +++ b/sink.go @@ -22,6 +22,10 @@ type MetricSink interface { // Samples are for timing information, where quantiles are used AddSample(key []string, val float32) AddSampleWithLabels(key []string, val float32, labels []Label) +} + +type ShutdownSink interface { + MetricSink // Shutdown the metric sink, flush metrics to storage, and cleanup resources. // Called immediately prior to application exit. Implementations must block @@ -39,7 +43,6 @@ func (*BlackholeSink) IncrCounter(key []string, val float32) func (*BlackholeSink) IncrCounterWithLabels(key []string, val float32, labels []Label) {} func (*BlackholeSink) AddSample(key []string, val float32) {} func (*BlackholeSink) AddSampleWithLabels(key []string, val float32, labels []Label) {} -func (*BlackholeSink) Shutdown() {} // FanoutSink is used to sink to fanout values to multiple sinks type FanoutSink []MetricSink @@ -82,7 +85,9 @@ func (fh FanoutSink) AddSampleWithLabels(key []string, val float32, labels []Lab func (fh FanoutSink) Shutdown() { for _, s := range fh { - s.Shutdown() + if ss, ok := s.(ShutdownSink); ok { + ss.Shutdown() + } } }