From cd0a7cd52f8be2225db9160722e4e15517656eb1 Mon Sep 17 00:00:00 2001 From: rentiansheng Date: Wed, 24 Nov 2021 06:05:23 +0800 Subject: [PATCH] fix: input plugin statsd bug (#10116) --- config/config.go | 3 ++- plugins/inputs/statsd/statsd.go | 6 ++++++ plugins/inputs/statsd/statsd_test.go | 27 +++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/config/config.go b/config/config.go index 9333e32ab..97f9c35b3 100644 --- a/config/config.go +++ b/config/config.go @@ -165,7 +165,8 @@ type AgentConfig struct { // TODO(cam): Remove UTC and parameter, they are no longer // valid for the agent config. Leaving them here for now for backwards- // compatibility - UTC bool `toml:"utc"` // deprecated in 1.0.0; has no effect + // Deprecated: 1.0.0 after, has no effect + UTC bool `toml:"utc"` // Debug is the option for running in debug mode Debug bool `toml:"debug"` diff --git a/plugins/inputs/statsd/statsd.go b/plugins/inputs/statsd/statsd.go index d23a79225..861d2561a 100644 --- a/plugins/inputs/statsd/statsd.go +++ b/plugins/inputs/statsd/statsd.go @@ -797,6 +797,12 @@ func parseKeyValue(keyValue string) (key string, val string) { val = split[1] } else if len(split) == 1 { val = split[0] + } else if len(split) > 2 { + // fix: https://github.com/influxdata/telegraf/issues/10113 + // fix: value has "=" parse error + // uri=/service/endpoint?sampleParam={paramValue} parse value key="uri", val="/service/endpoint?sampleParam\={paramValue}" + key = split[0] + val = strings.Join(split[1:], "=") } return key, val diff --git a/plugins/inputs/statsd/statsd_test.go b/plugins/inputs/statsd/statsd_test.go index 48889aa43..5121f06b6 100644 --- a/plugins/inputs/statsd/statsd_test.go +++ b/plugins/inputs/statsd/statsd_test.go @@ -1674,3 +1674,30 @@ func TestParse_Ints(t *testing.T) { require.NoError(t, s.Gather(acc)) require.Equal(t, s.Percentiles, []Number{90.0}) } + +func TestParse_KeyValue(t *testing.T) { + type output struct { + key string + val string + } + + validLines := []struct { + input string + output output + }{ + {"", output{"", ""}}, + {"only value", output{"", "only value"}}, + {"key=value", output{"key", "value"}}, + {"url=/api/querystring?key1=val1&key2=value", output{"url", "/api/querystring?key1=val1&key2=value"}}, + } + + for _, line := range validLines { + key, val := parseKeyValue(line.input) + if key != line.output.key { + t.Errorf("line: %s, key expected %s, actual %s", line, line.output.key, key) + } + if val != line.output.val { + t.Errorf("line: %s, val expected %s, actual %s", line, line.output.val, val) + } + } +}