From 0c4a66e6a18bc915ff71926c5c7ab7efdc5b1a9d Mon Sep 17 00:00:00 2001 From: glightfoot Date: Sun, 7 Feb 2021 11:13:35 -0500 Subject: [PATCH 1/2] rework metricLineRE to support matching segments that start with a number Signed-off-by: glightfoot --- pkg/mapper/mapper.go | 10 +++++++--- pkg/mapper/mapper_test.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/pkg/mapper/mapper.go b/pkg/mapper/mapper.go index 680952b..29bc0ac 100644 --- a/pkg/mapper/mapper.go +++ b/pkg/mapper/mapper.go @@ -28,10 +28,14 @@ import ( ) var ( - statsdMetricRE = `[a-zA-Z_](-?[a-zA-Z0-9_])*` - templateReplaceRE = `(\$\{?\d+\}?)` + // The first segment of a match cannot start with a number + statsdMetricRE = `[a-zA-Z_](-?[a-zA-Z0-9_])*` + // The subsequent segments of a match can start with a number + // See https://github.com/prometheus/statsd_exporter/issues/328 + statsdMetricSubsequentRE = `[a-zA-Z0-9_](-?[a-zA-Z0-9_])*` + templateReplaceRE = `(\$\{?\d+\}?)` - metricLineRE = regexp.MustCompile(`^(\*\.|` + statsdMetricRE + `\.)+(\*|` + statsdMetricRE + `)$`) + metricLineRE = regexp.MustCompile(`^(\*|` + statsdMetricRE + `)(\.\*|\.` + statsdMetricSubsequentRE + `)*$`) metricNameRE = regexp.MustCompile(`^([a-zA-Z_]|` + templateReplaceRE + `)([a-zA-Z0-9_]|` + templateReplaceRE + `)*$`) labelNameRE = regexp.MustCompile(`^[a-zA-Z_][a-zA-Z0-9_]+$`) ) diff --git a/pkg/mapper/mapper_test.go b/pkg/mapper/mapper_test.go index ad7e32e..d1ecf13 100644 --- a/pkg/mapper/mapper_test.go +++ b/pkg/mapper/mapper_test.go @@ -337,6 +337,39 @@ mappings: }, }, }, + { + testName: "Match metric segment starting with a number", + config: ` +mappings: +- match: test.99.myapp.* + name: "name" + labels: + label: "${1}_foo" + `, + mappings: mappings{ + { + statsdMetric: "test.99.myapp.create", + name: "name", + labels: map[string]string{ + "label": "create_foo", + }, + }, + }, + }, + { + testName: "Match metric segment starting with a number #328 example", + config: ` +mappings: +- match: kafka.server.FetcherStats.brokerHost.hostname.brokerPort.9092.clientId.ReplicaFetcherThread-0-1.BytesPerSec.1MinuteRate.gauge + name: "example_name" + `, + mappings: mappings{ + { + statsdMetric: "kafka.server.FetcherStats.brokerHost.hostname.brokerPort.9092.clientId.ReplicaFetcherThread-0-1.BytesPerSec.1MinuteRate.gauge", + name: "example_name", + }, + }, + }, { testName: "Config with bad metric line", config: `--- From 5793e05795ae637e65f1a03e8f68d97aa6b0de2b Mon Sep 17 00:00:00 2001 From: Matthias Rampke Date: Fri, 26 Mar 2021 16:54:29 +0000 Subject: [PATCH 2/2] Add additional tests for #365 testing more edge cases. Signed-off-by: Matthias Rampke --- pkg/mapper/mapper_test.go | 42 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/pkg/mapper/mapper_test.go b/pkg/mapper/mapper_test.go index d1ecf13..a9d2d9f 100644 --- a/pkg/mapper/mapper_test.go +++ b/pkg/mapper/mapper_test.go @@ -338,7 +338,7 @@ mappings: }, }, { - testName: "Match metric segment starting with a number", + testName: "Match metric segment with a number", config: ` mappings: - match: test.99.myapp.* @@ -357,7 +357,26 @@ mappings: }, }, { - testName: "Match metric segment starting with a number #328 example", + testName: "Match metric segment starting with a number", + config: ` +mappings: +- match: test.99test.myapp.* + name: "name" + labels: + label: "${1}_foo" + `, + mappings: mappings{ + { + statsdMetric: "test.99test.myapp.create", + name: "name", + labels: map[string]string{ + "label": "create_foo", + }, + }, + }, + }, + { + testName: "Match metric segment with a number #328 example", config: ` mappings: - match: kafka.server.FetcherStats.brokerHost.hostname.brokerPort.9092.clientId.ReplicaFetcherThread-0-1.BytesPerSec.1MinuteRate.gauge @@ -370,6 +389,25 @@ mappings: }, }, }, + { + testName: "Single segment match", + config: ` +mappings: +- match: '*' + name: single_segment + labels: + label: "${1}" +`, + mappings: mappings{ + { + statsdMetric: "test", + name: "single_segment", + labels: map[string]string{ + "label": "test", + }, + }, + }, + }, { testName: "Config with bad metric line", config: `---