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.
This commit is contained in:
Daniel Nelson 2020-02-04 15:12:23 -08:00 committed by GitHub
parent 8792a5fd5e
commit e8d9add2d1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 0 deletions

View File

@ -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
}