Skip to content

Commit

Permalink
feat: support more specific prefixes in ?collect parameter (#387)
Browse files Browse the repository at this point in the history
Signed-off-by: Antoine Deschênes <[email protected]>
  • Loading branch information
antoinedeschenes authored Dec 2, 2024
1 parent 5f55f7b commit 231987c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
8 changes: 5 additions & 3 deletions stackdriver_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
26 changes: 26 additions & 0 deletions stackdriver_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

0 comments on commit 231987c

Please sign in to comment.