Skip to content

Commit 231987c

Browse files
feat: support more specific prefixes in ?collect parameter (#387)
Signed-off-by: Antoine Deschênes <[email protected]>
1 parent 5f55f7b commit 231987c

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

Diff for: stackdriver_exporter.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -257,12 +257,14 @@ func (h *handler) filterMetricTypePrefixes(filters map[string]bool) []string {
257257
if len(filters) > 0 {
258258
filteredPrefixes = nil
259259
for _, prefix := range h.metricsPrefixes {
260-
if filters[prefix] {
261-
filteredPrefixes = append(filteredPrefixes, prefix)
260+
for filter, _ := range filters {
261+
if strings.HasPrefix(filter, prefix) {
262+
filteredPrefixes = append(filteredPrefixes, filter)
263+
}
262264
}
263265
}
264266
}
265-
return filteredPrefixes
267+
return parseMetricTypePrefixes(filteredPrefixes)
266268
}
267269

268270
func main() {

Diff for: stackdriver_exporter_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,29 @@ func TestParseMetricTypePrefixes(t *testing.T) {
3535
t.Errorf("Metric type prefix sanitization did not produce expected output. Expected:\n%s\nGot:\n%s", expectedOutputPrefixes, outputPrefixes)
3636
}
3737
}
38+
39+
func TestFilterMetricTypePrefixes(t *testing.T) {
40+
metricPrefixes := []string{
41+
"redis.googleapis.com/stats/",
42+
}
43+
44+
h := &handler{
45+
metricsPrefixes: metricPrefixes,
46+
}
47+
48+
inputFilters := map[string]bool{
49+
"redis.googleapis.com/stats/memory/usage": true,
50+
"redis.googleapis.com/stats/memory/usage_ratio": true,
51+
"redis.googleapis.com": true,
52+
}
53+
54+
expectedOutputPrefixes := []string{
55+
"redis.googleapis.com/stats/memory/usage",
56+
}
57+
58+
outputPrefixes := h.filterMetricTypePrefixes(inputFilters)
59+
60+
if !reflect.DeepEqual(outputPrefixes, expectedOutputPrefixes) {
61+
t.Errorf("filterMetricTypePrefixes did not produce expected output. Expected:\n%s\nGot:\n%s", expectedOutputPrefixes, outputPrefixes)
62+
}
63+
}

0 commit comments

Comments
 (0)