fix(outputs.prometheus_client): Expire with ticker, not add/collect (#12560)

This commit is contained in:
Joshua Powers 2023-01-31 02:38:10 -07:00 committed by GitHub
parent 52b2323dec
commit b20e490031
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 15 deletions

View File

@ -57,16 +57,29 @@ type Collector struct {
Log telegraf.Logger
sync.Mutex
fam map[string]*MetricFamily
fam map[string]*MetricFamily
expireTicker *time.Ticker
}
func NewCollector(expire time.Duration, stringsAsLabel bool, logger telegraf.Logger) *Collector {
return &Collector{
c := &Collector{
ExpirationInterval: expire,
StringAsLabel: stringsAsLabel,
Log: logger,
fam: make(map[string]*MetricFamily),
}
if c.ExpirationInterval != 0 {
c.expireTicker = time.NewTicker(c.ExpirationInterval)
go func() {
for {
<-c.expireTicker.C
c.Expire(time.Now())
}
}()
}
return c
}
func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
@ -77,10 +90,6 @@ 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 {
// Get list of all labels on MetricFamily
var labelNames []string
@ -368,22 +377,17 @@ 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
}
func (c *Collector) Expire(now time.Time, age time.Duration) {
if age == 0 {
return
}
func (c *Collector) Expire(now time.Time) {
c.Lock()
defer c.Unlock()
for name, family := range c.fam {
for key, sample := range family.Samples {
if age != 0 && now.After(sample.Expiration) {
if now.After(sample.Expiration) {
for k := range sample.Labels {
family.LabelSet[k]--
}