Skip to content

Commit

Permalink
simplify metrics filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
an-mmx committed Jan 23, 2025
1 parent 330245e commit 75de370
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 68 deletions.
2 changes: 1 addition & 1 deletion receiver/azuremonitorreceiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ receivers:
auth: "default_credentials"
metrics:
- metric1 # This will scrape all known aggregations for "metric1"
- metric2/total # This will restrict scraping to "metric2" with the "Total" aggregation
- metric2/total # This will include "metric2" with the "Total" aggregation in the scraping
- metric3/average # This will include "metric3" with the "Average" aggregation in the scraping
```
Expand Down
29 changes: 11 additions & 18 deletions receiver/azuremonitorreceiver/scraper.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,15 @@ func (s *azureScraper) getResourceMetricsDefinitions(ctx context.Context, resour

for _, v := range nextResult.Value {
name := *v.Name.Value
if !isMetricMatchFilters(name, s.cfg.Metrics) {
aggregations := getMetricAggregations(name, s.cfg.Metrics)
if len(aggregations) == 0 {
continue
}

timeGrain := *v.MetricAvailabilities[0].TimeGrain
compositeKey := metricsCompositeKey{
timeGrain: timeGrain,
aggregations: getMetricAggregations(name, s.cfg.Metrics),
aggregations: strings.Join(aggregations, ","),
}

if len(v.Dimensions) > 0 {
Expand Down Expand Up @@ -511,31 +512,23 @@ func (s *azureScraper) processTimeseriesData(
}
}

func isMetricMatchFilters(name string, filters []string) bool {
name = strings.ToLower(name)
for _, filter := range filters {
filter = strings.ToLower(filter)
if filter == name || strings.HasPrefix(filter, name+"/") {
return true
}
func getMetricAggregations(name string, filters []string) []string {
if len(filters) == 0 {
return aggregations
}

return len(filters) == 0
}

func getMetricAggregations(name string, filters []string) string {
out := []string{}
for _, filter := range filters {
if strings.EqualFold(name, filter) {
return aggregations
}

for _, aggregation := range aggregations {
if strings.EqualFold(name+"/"+aggregation, filter) {
out = append(out, aggregation)
}
}
}

if len(out) == 0 {
out = aggregations
}

return strings.Join(out, ",")
return out
}
56 changes: 7 additions & 49 deletions receiver/azuremonitorreceiver/scraper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,84 +656,42 @@ func TestAzureScraperClientOptions(t *testing.T) {
}
}

func TestIsMetricMatchFilters(t *testing.T) {
testMetricName := "MetricName1"
tests := []struct {
name string
filters []string
want bool
}{
{
"filters_empty",
[]string{},
true,
},
{
"filters_include_metric",
[]string{"foo", testMetricName, "bar"},
true,
},
{
"filters_include_metric_ignore_case",
[]string{"foo", strings.ToLower(testMetricName), "bar"},
true,
},
{
"filters_include_metric_aggregation",
[]string{"foo/count", testMetricName + "/total", "bar/total"},
true,
},
{
"filters_exclude_metric",
[]string{"foo", "bar"},
false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := isMetricMatchFilters(testMetricName, tt.filters)
require.Equal(t, tt.want, got)
})
}
}

func TestGetMetricAggregations(t *testing.T) {
testMetricName := "MetricName"
tests := []struct {
name string
filters []string
want string
want []string
}{
{
"filters_empty",
[]string{},
strings.Join(aggregations, ","),
aggregations,
},
{
"filters_include_metric",
[]string{"foo", testMetricName, "bar"},
strings.Join(aggregations, ","),
aggregations,
},
{
"filters_include_metric_ignore_case",
[]string{"foo", strings.ToLower(testMetricName), "bar"},
strings.Join(aggregations, ","),
aggregations,
},
{
"filters_include_metric_aggregation",
[]string{"foo/count", testMetricName + "/" + aggregations[0], "bar/total"},
aggregations[0],
[]string{aggregations[0]},
},
{
"filters_include_metric_aggregation_ignore_case",
[]string{"foo/count", testMetricName + "/" + strings.ToLower(aggregations[0]), "bar/total"},
aggregations[0],
[]string{aggregations[0]},
},
{
"filters_include_metric_multiple_aggregations",
[]string{"foo/count", testMetricName + "/" + aggregations[0], testMetricName + "/" + aggregations[2]},
aggregations[0] + "," + aggregations[2],
[]string{aggregations[0], aggregations[2]},
},
}

Expand Down

0 comments on commit 75de370

Please sign in to comment.