From f500d859fa052698987c3a1210abb3a7239e6e8a Mon Sep 17 00:00:00 2001 From: Maurizio Branca Date: Tue, 16 Jan 2024 17:26:30 +0100 Subject: [PATCH] Fix value mapping for min and max aggregation type in Azure Monitor (#37643) * Fix value mapping for min and max aggregation type Metric values for `min` and `max` aggregation types were picked up from the wrong place. --------- Co-authored-by: Andrew Gizas (cherry picked from commit 1554d7d3cd919ccf209e544ddef1789192173d27) --- CHANGELOG.next.asciidoc | 1 + x-pack/metricbeat/module/azure/data.go | 4 +- x-pack/metricbeat/module/azure/data_test.go | 116 ++++++++++++++++++++ 3 files changed, 119 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.next.asciidoc b/CHANGELOG.next.asciidoc index ace3eeec5dcf..f07844d43fa5 100644 --- a/CHANGELOG.next.asciidoc +++ b/CHANGELOG.next.asciidoc @@ -127,6 +127,7 @@ https://github.com/elastic/beats/compare/v8.8.1\...main[Check the HEAD diff] - Fix the "api-version query parameter (?api-version=) is required for all requests" error in Azure Billing. {pull}37158[37158] - Add memory hard limit from container metadata and remove usage percentage in AWS Fargate. {pull}37194[37194] - Fix the reference time rounding on Azure Metrics {issue}37204[37204] {pull}37365[37365] +- Fix Azure Resource Metrics missing metrics (min and max aggregations) after upgrade to 8.11.3 {issue}37642[37642] {pull}37643[37643] *Osquerybeat* diff --git a/x-pack/metricbeat/module/azure/data.go b/x-pack/metricbeat/module/azure/data.go index b39a99d480da..c46aee9da246 100644 --- a/x-pack/metricbeat/module/azure/data.go +++ b/x-pack/metricbeat/module/azure/data.go @@ -142,10 +142,10 @@ func mapToKeyValuePoints(metrics []Metric) []KeyValuePoint { switch { case value.min != nil: point.Key = fmt.Sprintf("%s.%s", metricName, "min") - point.Value = value.avg + point.Value = value.min case value.max != nil: point.Key = fmt.Sprintf("%s.%s", metricName, "max") - point.Value = value.avg + point.Value = value.max case value.avg != nil: point.Key = fmt.Sprintf("%s.%s", metricName, "avg") point.Value = value.avg diff --git a/x-pack/metricbeat/module/azure/data_test.go b/x-pack/metricbeat/module/azure/data_test.go index 8ea3144a0a71..85b781ed64ec 100644 --- a/x-pack/metricbeat/module/azure/data_test.go +++ b/x-pack/metricbeat/module/azure/data_test.go @@ -5,7 +5,9 @@ package azure import ( + "fmt" "testing" + "time" "github.com/stretchr/testify/assert" ) @@ -46,3 +48,117 @@ func TestManagePropertyName(t *testing.T) { result = managePropertyName("Percentage CPU") assert.Equal(t, result, "percentage_cpu") } + +func TestMapToKeyValuePoints(t *testing.T) { + timestamp := time.Now().UTC() + metricName := "test" + minValue := 4.0 + maxValue := 42.0 + avgValue := 13.0 + totalValue := 46.0 + countValue := 2.0 + namespace := "test" + resourceId := "test" + resourceSubId := "test" + timeGrain := "PT1M" + + t.Run("test aggregation types", func(t *testing.T) { + + metrics := []Metric{{ + Namespace: namespace, + Names: []string{"test"}, + Aggregations: "min", + Values: []MetricValue{{name: metricName, min: &minValue, timestamp: timestamp}}, + TimeGrain: timeGrain, + ResourceId: resourceId, + ResourceSubId: resourceSubId, + }, { + Namespace: namespace, + Names: []string{"test"}, + Aggregations: "max", + Values: []MetricValue{{name: metricName, max: &maxValue, timestamp: timestamp}}, + TimeGrain: timeGrain, + ResourceId: resourceId, + ResourceSubId: resourceSubId, + }, { + Namespace: namespace, + Names: []string{"test"}, + Aggregations: "avg", + Values: []MetricValue{{name: metricName, avg: &avgValue, timestamp: timestamp}}, + TimeGrain: timeGrain, + ResourceId: resourceId, + ResourceSubId: resourceSubId, + }, { + Namespace: namespace, + Names: []string{"test"}, + Aggregations: "total", + Values: []MetricValue{{name: metricName, total: &totalValue, timestamp: timestamp}}, + TimeGrain: timeGrain, + ResourceId: resourceId, + ResourceSubId: resourceSubId, + }, { + Namespace: namespace, + Names: []string{"test"}, + Aggregations: "count", + Values: []MetricValue{{name: metricName, count: &countValue, timestamp: timestamp}}, + TimeGrain: timeGrain, + ResourceId: resourceId, + ResourceSubId: resourceSubId, + }} + + actual := mapToKeyValuePoints(metrics) + + expected := []KeyValuePoint{ + { + Key: fmt.Sprintf("%s.%s", metricName, "min"), + Value: &minValue, + Namespace: namespace, + TimeGrain: timeGrain, + Timestamp: timestamp, + ResourceId: resourceId, + ResourceSubId: resourceSubId, + Dimensions: map[string]interface{}{}, + }, { + Key: fmt.Sprintf("%s.%s", metricName, "max"), + Value: &maxValue, + Namespace: namespace, + TimeGrain: timeGrain, + Timestamp: timestamp, + ResourceId: resourceId, + ResourceSubId: resourceSubId, + Dimensions: map[string]interface{}{}, + }, { + Key: fmt.Sprintf("%s.%s", metricName, "avg"), + Value: &avgValue, + Namespace: namespace, + TimeGrain: timeGrain, + Timestamp: timestamp, + ResourceId: resourceId, + ResourceSubId: resourceSubId, + Dimensions: map[string]interface{}{}, + }, + { + Key: fmt.Sprintf("%s.%s", metricName, "total"), + Value: &totalValue, + Namespace: namespace, + TimeGrain: timeGrain, + Timestamp: timestamp, + ResourceId: resourceId, + ResourceSubId: resourceSubId, + Dimensions: map[string]interface{}{}, + }, + { + Key: fmt.Sprintf("%s.%s", metricName, "count"), + Value: &countValue, + Namespace: namespace, + TimeGrain: timeGrain, + Timestamp: timestamp, + ResourceId: resourceId, + ResourceSubId: resourceSubId, + Dimensions: map[string]interface{}{}, + }, + } + + assert.Equal(t, expected, actual) + }) +}