diff --git a/receiver/azuremonitorreceiver/README.md b/receiver/azuremonitorreceiver/README.md index 6c4d7e4c74af..0aea8e39a4f4 100644 --- a/receiver/azuremonitorreceiver/README.md +++ b/receiver/azuremonitorreceiver/README.md @@ -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 ``` diff --git a/receiver/azuremonitorreceiver/scraper.go b/receiver/azuremonitorreceiver/scraper.go index 0c2b29b447d5..2db35f0f0935 100644 --- a/receiver/azuremonitorreceiver/scraper.go +++ b/receiver/azuremonitorreceiver/scraper.go @@ -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 { @@ -511,21 +512,17 @@ 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) @@ -533,9 +530,5 @@ func getMetricAggregations(name string, filters []string) string { } } - if len(out) == 0 { - out = aggregations - } - - return strings.Join(out, ",") + return out } diff --git a/receiver/azuremonitorreceiver/scraper_test.go b/receiver/azuremonitorreceiver/scraper_test.go index 09e9e3c23c90..44737212c2dc 100644 --- a/receiver/azuremonitorreceiver/scraper_test.go +++ b/receiver/azuremonitorreceiver/scraper_test.go @@ -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]}, }, }