From f48b8504b6c5207474e3ba13618ebefe25b40080 Mon Sep 17 00:00:00 2001 From: Joshua Powers Date: Thu, 30 Nov 2023 04:02:17 -0700 Subject: [PATCH] fix(outputs.prometheus_client): Ensure v1 collector data expires promptly (#14232) --- .../outputs/prometheus_client/v1/collector.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/plugins/outputs/prometheus_client/v1/collector.go b/plugins/outputs/prometheus_client/v1/collector.go index b25a766af..c1a635d05 100644 --- a/plugins/outputs/prometheus_client/v1/collector.go +++ b/plugins/outputs/prometheus_client/v1/collector.go @@ -96,6 +96,12 @@ func (c *Collector) Describe(ch chan<- *prometheus.Desc) { } func (c *Collector) Collect(ch chan<- prometheus.Metric) { + // Expire metrics, doing this on Collect ensure metrics are removed even if no + // new metrics are added to the output. + if c.ExpirationInterval != 0 { + c.Expire(time.Now()) + } + c.Lock() defer c.Unlock() @@ -211,6 +217,18 @@ func sorted(metrics []telegraf.Metric) []telegraf.Metric { } func (c *Collector) Add(metrics []telegraf.Metric) error { + c.addMetrics(metrics) + + // Expire metrics, doing this on Add ensure metrics are removed even if no + // new metrics are added to the output. + if c.ExpirationInterval != 0 { + c.Expire(time.Now()) + } + + return nil +} + +func (c *Collector) addMetrics(metrics []telegraf.Metric) { c.Lock() defer c.Unlock() @@ -388,7 +406,6 @@ func (c *Collector) Add(metrics []telegraf.Metric) error { } } } - return nil } func (c *Collector) Expire(now time.Time) {