fix(outputs.prometheus_client): Ensure v1 collector data expires promptly (#14232)

This commit is contained in:
Joshua Powers 2023-11-30 04:02:17 -07:00 committed by GitHub
parent 6814d7af8a
commit f48b8504b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 18 additions and 1 deletions

View File

@ -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) {