From fe1f6515a044b2eee490e691422d80482879acb8 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Mon, 14 Feb 2022 12:07:09 -0700 Subject: [PATCH] [Metricbeat] azure: move event report into loop validDim loop (#29945) (#30379) Co-authored-by: Adrian Tomalla Co-authored-by: Patrick Eichhorn (cherry picked from commit ef7a0b948c2eec6fd16c13b5d1df8b37e57d462e) Co-authored-by: ClumsyPotato --- x-pack/metricbeat/module/azure/data.go | 37 ++++++++++--------- .../module/azure/monitor/_meta/data.json | 30 ++++++--------- .../azure/monitor/monitor_integration_test.go | 17 +++++++++ 3 files changed, 48 insertions(+), 36 deletions(-) diff --git a/x-pack/metricbeat/module/azure/data.go b/x-pack/metricbeat/module/azure/data.go index eb7f1433142e..55b5d5440ee9 100644 --- a/x-pack/metricbeat/module/azure/data.go +++ b/x-pack/metricbeat/module/azure/data.go @@ -79,33 +79,34 @@ func EventsMapping(metrics []Metric, client *Client, report mb.ReporterV2) error groupByDimensions[dimKey] = append(groupByDimensions[dimKey], dimGroupValue) } for _, groupDimValues := range groupByDimensions { - event, metricList = createEvent(timestamp, defaultMetric, resource, groupDimValues) - if client.Config.AddCloudMetadata { - vm = client.GetVMForMetaData(&resource, groupDimValues) - addCloudVMMetadata(&event, vm, resource.Subscription) - } + manageAndReportEvent(client, report, event, metricList, vm, timestamp, defaultMetric, resource, groupDimValues) } } } else { - event, metricList = createEvent(timestamp, defaultMetric, resource, groupTimeValues) - if client.Config.AddCloudMetadata { - vm = client.GetVMForMetaData(&resource, groupTimeValues) - addCloudVMMetadata(&event, vm, resource.Subscription) - } + manageAndReportEvent(client, report, event, metricList, vm, timestamp, defaultMetric, resource, groupTimeValues) } - if client.Config.DefaultResourceType == "" { - event.ModuleFields.Put("metrics", metricList) - } else { - for key, metric := range metricList { - event.MetricSetFields.Put(key, metric) - } - } - report.Event(event) } } return nil } +// manageAndReportEvent function will handle event creation and report +func manageAndReportEvent(client *Client, report mb.ReporterV2, event mb.Event, metricList common.MapStr, vm VmResource, timestamp time.Time, defaultMetric Metric, resource Resource, groupedValues []MetricValue) { + event, metricList = createEvent(timestamp, defaultMetric, resource, groupedValues) + if client.Config.AddCloudMetadata { + vm = client.GetVMForMetaData(&resource, groupedValues) + addCloudVMMetadata(&event, vm, resource.Subscription) + } + if client.Config.DefaultResourceType == "" { + event.ModuleFields.Put("metrics", metricList) + } else { + for key, metric := range metricList { + event.MetricSetFields.Put(key, metric) + } + } + report.Event(event) +} + // managePropertyName function will handle metric names, there are several formats the metric names are written func managePropertyName(metric string) string { // replace spaces with underscores diff --git a/x-pack/metricbeat/module/azure/monitor/_meta/data.json b/x-pack/metricbeat/module/azure/monitor/_meta/data.json index 16d7f82b8d89..7ee2ca14be79 100644 --- a/x-pack/metricbeat/module/azure/monitor/_meta/data.json +++ b/x-pack/metricbeat/module/azure/monitor/_meta/data.json @@ -1,29 +1,23 @@ { "@timestamp": "2017-10-12T08:05:34.853Z", "azure": { + "dimensions": { + "activity_name": "secretlist" + }, "metrics": { - "data_usage": { - "total": 131072 - }, - "document_count": { - "total": 2 - }, - "document_quota": { - "total": 107374182400 + "availability": { + "avg": 100 } }, - "namespace": "Microsoft.DocumentDb/databaseAccounts", + "namespace": "Microsoft.KeyVault/vaults", "resource": { - "group": "obs-infrastructure", - "id": "/subscriptions/70bd6e64-4b1e-4835-8896-db77b8eef364/resourceGroups/obs-infrastructure/providers/Microsoft.DocumentDb/databaseAccounts/obsaccount", - "name": "obsaccount", - "tags": { - "defaultExperience": "Core (SQL)" - }, - "type": "Microsoft.DocumentDb/databaseAccounts" + "group": "some-rg", + "id": "/subscriptions/70f046a0-a299-ab73-9950-88ac8b5ac454/resourceGroups/some-rg/providers/Microsoft.KeyVault/vaults/somekeyvault", + "name": "somekeyvault", + "type": "Microsoft.KeyVault/vaults" }, "subscription_id": "70bd6e64-4b1e-4835-8896-db77b8eef364", - "timegrain": "PT5M" + "timegrain": "PT1M" }, "cloud": { "provider": "azure", @@ -41,4 +35,4 @@ "service": { "type": "azure" } -} +} \ No newline at end of file diff --git a/x-pack/metricbeat/module/azure/monitor/monitor_integration_test.go b/x-pack/metricbeat/module/azure/monitor/monitor_integration_test.go index 9383d92de6ce..3d9eef010bc8 100644 --- a/x-pack/metricbeat/module/azure/monitor/monitor_integration_test.go +++ b/x-pack/metricbeat/module/azure/monitor/monitor_integration_test.go @@ -14,6 +14,8 @@ import ( "github.com/stretchr/testify/assert" + "github.com/elastic/beats/v7/libbeat/common" + mbtest "github.com/elastic/beats/v7/metricbeat/mb/testing" ) @@ -37,3 +39,18 @@ func TestData(t *testing.T) { metricSet := mbtest.NewFetcher(t, config) metricSet.WriteEvents(t, "/") } + +func TestDataMultipleDimensions(t *testing.T) { + config := test.GetConfig(t, "monitor") + config["resources"] = []map[string]interface{}{{ + "resource_query": "resourceType eq 'Microsoft.KeyVault/vaults'", + "metrics": []map[string]interface{}{{"namespace": "Microsoft.KeyVault/vaults", + "name": []string{"Availability"}, "dimensions": []map[string]interface{}{{"name": "ActivityName", "value": "*"}}}}}} + metricSet := mbtest.NewFetcher(t, config) + metricSet.WriteEventsCond(t, "/", func(m common.MapStr) bool { + if m["azure"].(common.MapStr)["dimensions"].(common.MapStr)["activity_name"] == "secretget" { + return true + } + return false + }) +}