Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use add time for prometheus expiration calculation #7056

Merged
merged 2 commits into from
Feb 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/outputs/prometheus_client/v2/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (c *Collector) Add(metrics []telegraf.Metric) error {
defer c.Unlock()

for _, metric := range metrics {
c.coll.Add(metric)
c.coll.Add(metric, time.Now())
}

// Expire metrics, doing this on Add ensure metrics are removed even if no
Expand Down
20 changes: 13 additions & 7 deletions plugins/serializers/prometheus/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (

const helpString = "Telegraf collected metric"

type TimeFunc func() time.Time

type MetricFamily struct {
Name string
Type telegraf.ValueType
Expand All @@ -22,6 +24,7 @@ type MetricFamily struct {
type Metric struct {
Labels []LabelPair
Time time.Time
AddTime time.Time
Scaler *Scaler
Histogram *Histogram
Summary *Summary
Expand Down Expand Up @@ -97,14 +100,14 @@ type Entry struct {
}

type Collection struct {
config FormatConfig
Entries map[MetricFamily]Entry
config FormatConfig
}

func NewCollection(config FormatConfig) *Collection {
cache := &Collection{
config: config,
Entries: make(map[MetricFamily]Entry),
config: config,
}
return cache
}
Expand Down Expand Up @@ -177,7 +180,7 @@ func (c *Collection) createLabels(metric telegraf.Metric) []LabelPair {
return labels
}

func (c *Collection) Add(metric telegraf.Metric) {
func (c *Collection) Add(metric telegraf.Metric, now time.Time) {
labels := c.createLabels(metric)
for _, field := range metric.FieldList() {
metricName := MetricName(metric.Name(), field.Key, metric.Type())
Expand Down Expand Up @@ -225,9 +228,10 @@ func (c *Collection) Add(metric telegraf.Metric) {
}

m = &Metric{
Labels: labels,
Time: metric.Time(),
Scaler: &Scaler{Value: value},
Labels: labels,
Time: metric.Time(),
AddTime: now,
Scaler: &Scaler{Value: value},
}

entry.Metrics[metricKey] = m
Expand All @@ -236,6 +240,7 @@ func (c *Collection) Add(metric telegraf.Metric) {
m = &Metric{
Labels: labels,
Time: metric.Time(),
AddTime: now,
Histogram: &Histogram{},
}
}
Expand Down Expand Up @@ -283,6 +288,7 @@ func (c *Collection) Add(metric telegraf.Metric) {
m = &Metric{
Labels: labels,
Time: metric.Time(),
AddTime: now,
Summary: &Summary{},
}
}
Expand Down Expand Up @@ -331,7 +337,7 @@ func (c *Collection) Expire(now time.Time, age time.Duration) {
expireTime := now.Add(-age)
for _, entry := range c.Entries {
for key, metric := range entry.Metrics {
if metric.Time.Before(expireTime) {
if metric.AddTime.Before(expireTime) {
delete(entry.Metrics, key)
if len(entry.Metrics) == 0 {
delete(c.Entries, entry.Family)
Expand Down
Loading