From e8d9add2d15a743d28cc48ccff976b02c76026ee Mon Sep 17 00:00:00 2001 From: Daniel Nelson Date: Tue, 4 Feb 2020 15:12:23 -0800 Subject: [PATCH] Expire metrics on query in addition to on add (#6981) Ensures that expired metrics are removed even when no new data is sent to the output. --- plugins/outputs/prometheus_client/v2/collector.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/plugins/outputs/prometheus_client/v2/collector.go b/plugins/outputs/prometheus_client/v2/collector.go index 45e1cb7a7..9ffc6516a 100644 --- a/plugins/outputs/prometheus_client/v2/collector.go +++ b/plugins/outputs/prometheus_client/v2/collector.go @@ -65,6 +65,12 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { c.Lock() defer c.Unlock() + // Expire metrics, doing this on Collect ensure metrics are removed even if no + // new metrics are added to the output. + if c.expireDuration != 0 { + c.coll.Expire(time.Now(), c.expireDuration) + } + for _, family := range c.coll.GetProto() { for _, metric := range family.Metric { ch <- &Metric{family: family, metric: metric} @@ -80,8 +86,11 @@ func (c *Collector) Add(metrics []telegraf.Metric) error { c.coll.Add(metric) } + // Expire metrics, doing this on Add ensure metrics are removed even if no + // one is querying the data. if c.expireDuration != 0 { c.coll.Expire(time.Now(), c.expireDuration) } + return nil }