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..b0e65f0 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) { + metricPrefixes := []string{ + "redis.googleapis.com/stats/", + } + + h := &handler{ + metricsPrefixes: metricPrefixes, + } + + inputFilters := map[string]bool{ + "redis.googleapis.com/stats/memory/usage": true, + "redis.googleapis.com/stats/memory/usage_ratio": true, + "redis.googleapis.com": true, + } + + 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) + } +}