From a1de125ace7ff43f8e145149c15ce55adb870c2f Mon Sep 17 00:00:00 2001 From: Joshua Powers Date: Mon, 7 Nov 2022 11:36:34 -0700 Subject: [PATCH] fix(outputs.prometheus): expire during add (#12160) fixes https://github.com/influxdata/telegraf/issues/9821 --- plugins/outputs/prometheus_client/v1/collector.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/plugins/outputs/prometheus_client/v1/collector.go b/plugins/outputs/prometheus_client/v1/collector.go index 95ce62504..673102793 100644 --- a/plugins/outputs/prometheus_client/v1/collector.go +++ b/plugins/outputs/prometheus_client/v1/collector.go @@ -77,6 +77,8 @@ 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. c.Expire(time.Now(), c.ExpirationInterval) for name, family := range c.fam { @@ -366,6 +368,10 @@ func (c *Collector) Add(metrics []telegraf.Metric) error { c.addMetricFamily(point, sample, mname, sampleID) } } + + // Expire metrics, doing this on Add ensure metrics are removed even if no + // one is querying the data. + c.Expire(time.Now(), c.ExpirationInterval) } return nil }