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

util: Send only last metric value to Graphite #31829

Merged
merged 1 commit into from
Oct 24, 2018
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
5 changes: 5 additions & 0 deletions pkg/util/metric/graphite_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,10 @@ func (ge *GraphiteExporter) Push(ctx context.Context, endpoint string) error {
}); err != nil {
return err
}
// Regardless of whether Push() errors, clear metrics. Only latest metrics
// are pushed. If there is an error, this will cause a gap in receiver. The
// receiver associates timestamp with when metric arrived, so storing missed
// metrics has no benefit.
defer ge.pm.clearMetrics()
return b.Push()
}
11 changes: 9 additions & 2 deletions pkg/util/metric/prometheus_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ func (pm *PrometheusExporter) PrintAsText(w io.Writer) error {
if _, err := expfmt.MetricFamilyToText(w, family); err != nil {
return err
}
// Clear metrics for reuse.
family.Metric = []*prometheusgo.Metric{}
}
pm.clearMetrics()
return nil
}

Expand All @@ -108,3 +107,11 @@ func (pm *PrometheusExporter) Gather() ([]*prometheusgo.MetricFamily, error) {
}
return v, nil
}

// Clear metrics for reuse.
func (pm *PrometheusExporter) clearMetrics() {
for _, family := range pm.families {
// Set to nil to avoid allocation if the family never gets any metrics.
family.Metric = nil
}
}
30 changes: 30 additions & 0 deletions pkg/util/metric/prometheus_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,34 @@ func TestPrometheusExporter(t *testing.T) {
}
}
}

// Test Gather
families, err := pe.Gather()
if err != nil {
t.Errorf("unexpected error from Gather(): %v", err)
}
for _, fam := range families {
if len(fam.Metric) == 0 {
t.Errorf("gathered %s has no data points", fam.GetName())
}
}

// Test clearMetrics
pe.clearMetrics()
for _, fam := range pe.families {
if numPoints := len(fam.Metric); numPoints != 0 {
t.Errorf("%s has %d data points, want 0", fam.GetName(), numPoints)
}
}
// Check families returned by Gather are empty, right after calling clearMetrics
// before another call to scrape.
families, err = pe.Gather()
if err != nil {
t.Errorf("unexpected error from Gather(): %v", err)
}
for _, fam := range families {
if num := len(fam.Metric); num != 0 {
t.Errorf("gathered %s has %d data points but expect none", fam.GetName(), num)
}
}
}