From 594fcb3fdfff04d4553e94622e5c499729f53b8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Desch=C3=AAnes?= Date: Thu, 14 Nov 2024 13:27:13 -0500 Subject: [PATCH] feat: support more specific prefixes in ?collect parameter MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Antoine DeschĂȘnes --- stackdriver_exporter.go | 8 +++++--- stackdriver_exporter_test.go | 26 ++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/stackdriver_exporter.go b/stackdriver_exporter.go index 0529f44..4fc1c0a 100644 --- a/stackdriver_exporter.go +++ b/stackdriver_exporter.go @@ -257,12 +257,14 @@ func (h *handler) filterMetricTypePrefixes(filters map[string]bool) []string { if len(filters) > 0 { filteredPrefixes = nil for _, prefix := range h.metricsPrefixes { - if filters[prefix] { - filteredPrefixes = append(filteredPrefixes, prefix) + for filter, _ := range filters { + if strings.HasPrefix(filter, prefix) { + filteredPrefixes = append(filteredPrefixes, filter) + } } } } - return filteredPrefixes + return parseMetricTypePrefixes(filteredPrefixes) } func main() { diff --git a/stackdriver_exporter_test.go b/stackdriver_exporter_test.go index 05d0e3e..ff69b57 100644 --- a/stackdriver_exporter_test.go +++ b/stackdriver_exporter_test.go @@ -35,3 +35,29 @@ func TestParseMetricTypePrefixes(t *testing.T) { t.Errorf("Metric type prefix sanitization did not produce expected output. Expected:\n%s\nGot:\n%s", expectedOutputPrefixes, outputPrefixes) } } + +func TestFilterMetricTypePrefixes(t *testing.T) { + inputFilters := map[string]bool{ + "redis.googleapis.com/stats/memory/usage": true, + "redis.googleapis.com/stats/memory/usage_ratio": true, + "redis.googleapis.com": true, + } + + inputPrefixes := []string{ + "redis.googleapis.com/stats/", + } + + h := &handler{ + metricsPrefixes: inputPrefixes, + } + + expectedOutputPrefixes := []string{ + "redis.googleapis.com/stats/memory/usage", + } + + outputPrefixes := h.filterMetricTypePrefixes(inputFilters) + + if !reflect.DeepEqual(outputPrefixes, expectedOutputPrefixes) { + t.Errorf("filterMetricTypePrefixes did not produce expected output. Expected:\n%s\nGot:\n%s", expectedOutputPrefixes, outputPrefixes) + } +}