fix(outputs.prometheus_client): Expire with ticker, not add/collect (#12560)
This commit is contained in:
parent
52b2323dec
commit
b20e490031
|
|
@ -57,16 +57,29 @@ type Collector struct {
|
||||||
Log telegraf.Logger
|
Log telegraf.Logger
|
||||||
|
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
fam map[string]*MetricFamily
|
fam map[string]*MetricFamily
|
||||||
|
expireTicker *time.Ticker
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewCollector(expire time.Duration, stringsAsLabel bool, logger telegraf.Logger) *Collector {
|
func NewCollector(expire time.Duration, stringsAsLabel bool, logger telegraf.Logger) *Collector {
|
||||||
return &Collector{
|
c := &Collector{
|
||||||
ExpirationInterval: expire,
|
ExpirationInterval: expire,
|
||||||
StringAsLabel: stringsAsLabel,
|
StringAsLabel: stringsAsLabel,
|
||||||
Log: logger,
|
Log: logger,
|
||||||
fam: make(map[string]*MetricFamily),
|
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) {
|
func (c *Collector) Describe(ch chan<- *prometheus.Desc) {
|
||||||
|
|
@ -77,10 +90,6 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) {
|
||||||
c.Lock()
|
c.Lock()
|
||||||
defer c.Unlock()
|
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 {
|
for name, family := range c.fam {
|
||||||
// Get list of all labels on MetricFamily
|
// Get list of all labels on MetricFamily
|
||||||
var labelNames []string
|
var labelNames []string
|
||||||
|
|
@ -368,22 +377,17 @@ func (c *Collector) Add(metrics []telegraf.Metric) error {
|
||||||
c.addMetricFamily(point, sample, mname, sampleID)
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Collector) Expire(now time.Time, age time.Duration) {
|
func (c *Collector) Expire(now time.Time) {
|
||||||
if age == 0 {
|
c.Lock()
|
||||||
return
|
defer c.Unlock()
|
||||||
}
|
|
||||||
|
|
||||||
for name, family := range c.fam {
|
for name, family := range c.fam {
|
||||||
for key, sample := range family.Samples {
|
for key, sample := range family.Samples {
|
||||||
if age != 0 && now.After(sample.Expiration) {
|
if now.After(sample.Expiration) {
|
||||||
for k := range sample.Labels {
|
for k := range sample.Labels {
|
||||||
family.LabelSet[k]--
|
family.LabelSet[k]--
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue