From 2af134ca080c2a8da9ea77c6fd5bdd9f50513ec3 Mon Sep 17 00:00:00 2001 From: Marcin Bunsch Date: Thu, 28 Jan 2016 14:49:28 +0100 Subject: [PATCH] Use a single measurement with fields for timings in statsd plugin. closes #603 --- CHANGELOG.md | 1 + plugins/inputs/statsd/statsd.go | 16 ++++++----- plugins/inputs/statsd/statsd_test.go | 43 +++++++--------------------- 3 files changed, 20 insertions(+), 40 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d6fda00d..42ff2964c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Features - [#564](https://github.com/influxdata/telegraf/issues/564): features for plugin writing simplification. Internal metric data type. +- [#603](https://github.com/influxdata/telegraf/pull/603): Aggregate statsd timing measurements into fields. Thanks @marcinbunsch! ### Bugfixes - [#599](https://github.com/influxdata/telegraf/issues/599): datadog plugin tags not working. diff --git a/plugins/inputs/statsd/statsd.go b/plugins/inputs/statsd/statsd.go index 7bccd846e..a54ec86ec 100644 --- a/plugins/inputs/statsd/statsd.go +++ b/plugins/inputs/statsd/statsd.go @@ -162,15 +162,17 @@ func (s *Statsd) Gather(acc telegraf.Accumulator) error { defer s.Unlock() for _, metric := range s.timings { - acc.Add(metric.name+"_mean", metric.stats.Mean(), metric.tags) - acc.Add(metric.name+"_stddev", metric.stats.Stddev(), metric.tags) - acc.Add(metric.name+"_upper", metric.stats.Upper(), metric.tags) - acc.Add(metric.name+"_lower", metric.stats.Lower(), metric.tags) - acc.Add(metric.name+"_count", metric.stats.Count(), metric.tags) + fields := make(map[string]interface{}) + fields["mean"] = metric.stats.Mean() + fields["stddev"] = metric.stats.Stddev() + fields["upper"] = metric.stats.Upper() + fields["lower"] = metric.stats.Lower() + fields["count"] = metric.stats.Count() for _, percentile := range s.Percentiles { - name := fmt.Sprintf("%s_percentile_%v", metric.name, percentile) - acc.Add(name, metric.stats.Percentile(percentile), metric.tags) + name := fmt.Sprintf("%v_percentile", percentile) + fields[name] = metric.stats.Percentile(percentile) } + acc.AddFields(metric.name, fields, metric.tags) } if s.DeleteTimings { s.timings = make(map[string]cachedtimings) diff --git a/plugins/inputs/statsd/statsd_test.go b/plugins/inputs/statsd/statsd_test.go index 6fc1f6933..3a5917ab6 100644 --- a/plugins/inputs/statsd/statsd_test.go +++ b/plugins/inputs/statsd/statsd_test.go @@ -710,7 +710,7 @@ func TestParse_Timings(t *testing.T) { // Test that counters work valid_lines := []string{ "test.timing:1|ms", - "test.timing:1|ms", + "test.timing:11|ms", "test.timing:1|ms", "test.timing:1|ms", "test.timing:1|ms", @@ -725,40 +725,17 @@ func TestParse_Timings(t *testing.T) { s.Gather(acc) - tests := []struct { - name string - value interface{} - }{ - { - "test_timing_mean", - float64(1), - }, - { - "test_timing_stddev", - float64(0), - }, - { - "test_timing_upper", - float64(1), - }, - { - "test_timing_lower", - float64(1), - }, - { - "test_timing_count", - int64(5), - }, - { - "test_timing_percentile_90", - float64(1), - }, + valid := map[string]interface{}{ + "90_percentile": float64(11), + "count": int64(5), + "lower": float64(1), + "mean": float64(3), + "stddev": float64(4), + "upper": float64(11), } - for _, test := range tests { - acc.AssertContainsFields(t, test.name, - map[string]interface{}{"value": test.value}) - } + acc.AssertContainsFields(t, "test_timing", valid) + } func TestParse_Timings_Delete(t *testing.T) {