-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Restores support for multiple aggregation types for the same metric name. Adding tests for the known use cases, so we don't miss this feature again in future updates.
- Loading branch information
Showing
4 changed files
with
307 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package azure | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestGroupMetricsDefinitionsByResourceId(t *testing.T) { | ||
|
||
t.Run("Group metrics definitions by resource ID", func(t *testing.T) { | ||
metrics := []Metric{ | ||
{ | ||
ResourceId: "test", | ||
Namespace: "test", | ||
Names: []string{"name1"}, | ||
}, | ||
{ | ||
ResourceId: "test", | ||
Namespace: "test", | ||
Names: []string{"name2"}, | ||
}, | ||
{ | ||
ResourceId: "test", | ||
Namespace: "test", | ||
Names: []string{"name3"}, | ||
}, | ||
} | ||
|
||
metricsByResourceId := groupMetricsDefinitionsByResourceId(metrics) | ||
|
||
assert.Equal(t, 1, len(metricsByResourceId)) | ||
assert.Equal(t, 3, len(metricsByResourceId["test"])) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ package azure | |
|
||
import ( | ||
"errors" | ||
"github.com/Azure/azure-sdk-for-go/sdk/azcore/to" | ||
"github.com/stretchr/testify/require" | ||
"testing" | ||
"time" | ||
|
||
|
@@ -112,4 +114,154 @@ func TestGetMetricValues(t *testing.T) { | |
assert.Equal(t, len(client.ResourceConfigurations.Metrics[0].Values), 0) | ||
m.AssertExpectations(t) | ||
}) | ||
|
||
t.Run("multiple aggregation types", func(t *testing.T) { | ||
client := NewMockClient() | ||
referenceTime := time.Now().UTC() | ||
client.ResourceConfigurations = ResourceConfiguration{ | ||
Metrics: []Metric{ | ||
{ | ||
Namespace: "Microsoft.EventHub/Namespaces", | ||
Names: []string{"ActiveConnections"}, | ||
Aggregations: "Maximum,Minimum,Average", | ||
TimeGrain: "PT1M", | ||
}, | ||
}, | ||
} | ||
|
||
m := &MockService{} | ||
m.On( | ||
"GetMetricValues", | ||
mock.Anything, | ||
mock.Anything, | ||
mock.Anything, | ||
mock.Anything, | ||
mock.Anything, | ||
mock.Anything, | ||
mock.Anything, | ||
).Return( | ||
[]armmonitor.Metric{{ | ||
ID: to.Ptr("test"), | ||
Name: &armmonitor.LocalizableString{ | ||
Value: to.Ptr("ActiveConnections"), | ||
LocalizedValue: to.Ptr("ActiveConnections"), | ||
}, | ||
Timeseries: []*armmonitor.TimeSeriesElement{{ | ||
Data: []*armmonitor.MetricValue{{ | ||
Average: to.Ptr(1.0), | ||
Maximum: to.Ptr(2.0), | ||
Minimum: to.Ptr(3.0), | ||
TimeStamp: to.Ptr(time.Now()), | ||
}}, | ||
}}, | ||
Type: to.Ptr("Microsoft.Insights/metrics"), | ||
Unit: to.Ptr(armmonitor.MetricUnit("Count")), | ||
Check failure on line 158 in x-pack/metricbeat/module/azure/client_test.go GitHub Actions / lint (darwin)
|
||
DisplayDescription: to.Ptr("Total Active Connections for Microsoft.EventHub."), | ||
ErrorCode: to.Ptr("Success"), | ||
}}, | ||
"PT1M", | ||
nil, | ||
) | ||
|
||
client.AzureMonitorService = m | ||
mr := MockReporterV2{} | ||
|
||
metricValues := client.GetMetricValues(referenceTime, client.ResourceConfigurations.Metrics, &mr) | ||
|
||
require.Equal(t, len(metricValues), 1) | ||
require.Equal(t, len(metricValues[0].Values), 1) | ||
|
||
assert.Equal(t, *metricValues[0].Values[0].avg, 1.0) | ||
assert.Equal(t, *metricValues[0].Values[0].max, 2.0) | ||
assert.Equal(t, *metricValues[0].Values[0].min, 3.0) | ||
|
||
require.Equal(t, len(client.ResourceConfigurations.Metrics[0].Values), 1) | ||
|
||
m.AssertExpectations(t) | ||
}) | ||
|
||
t.Run("single aggregation types", func(t *testing.T) { | ||
client := NewMockClient() | ||
referenceTime := time.Now().UTC() | ||
timestamp := time.Now().UTC() | ||
client.ResourceConfigurations = ResourceConfiguration{ | ||
Metrics: []Metric{ | ||
{ | ||
Namespace: "Microsoft.EventHub/Namespaces", | ||
Names: []string{"ActiveConnections"}, | ||
Aggregations: "Maximum", | ||
TimeGrain: "PT1M", | ||
}, { | ||
Namespace: "Microsoft.EventHub/Namespaces", | ||
Names: []string{"ActiveConnections"}, | ||
Aggregations: "Minimum", | ||
TimeGrain: "PT1M", | ||
}, { | ||
Namespace: "Microsoft.EventHub/Namespaces", | ||
Names: []string{"ActiveConnections"}, | ||
Aggregations: "Average", | ||
TimeGrain: "PT1M", | ||
}, | ||
}, | ||
} | ||
|
||
m := &MockService{} | ||
|
||
x := []struct { | ||
aggregation string | ||
data []*armmonitor.MetricValue | ||
}{ | ||
{aggregation: "Maximum", data: []*armmonitor.MetricValue{{Maximum: to.Ptr(3.0), TimeStamp: to.Ptr(timestamp)}}}, | ||
{aggregation: "Minimum", data: []*armmonitor.MetricValue{{Minimum: to.Ptr(1.0), TimeStamp: to.Ptr(timestamp)}}}, | ||
{aggregation: "Average", data: []*armmonitor.MetricValue{{Average: to.Ptr(2.0), TimeStamp: to.Ptr(timestamp)}}}, | ||
} | ||
|
||
for _, v := range x { | ||
m.On( | ||
"GetMetricValues", | ||
mock.Anything, | ||
mock.Anything, | ||
mock.Anything, | ||
mock.Anything, | ||
mock.Anything, | ||
v.aggregation, | ||
mock.Anything, | ||
).Return( | ||
[]armmonitor.Metric{{ | ||
ID: to.Ptr("test"), | ||
Name: &armmonitor.LocalizableString{ | ||
Value: to.Ptr("ActiveConnections"), | ||
LocalizedValue: to.Ptr("ActiveConnections"), | ||
}, | ||
Timeseries: []*armmonitor.TimeSeriesElement{{ | ||
Data: v.data, | ||
}}, | ||
Type: to.Ptr("Microsoft.Insights/metrics"), | ||
Unit: to.Ptr(armmonitor.MetricUnit("Count")), | ||
Check failure on line 240 in x-pack/metricbeat/module/azure/client_test.go GitHub Actions / lint (darwin)
|
||
DisplayDescription: to.Ptr("Total Active Connections for Microsoft.EventHub."), | ||
ErrorCode: to.Ptr("Success"), | ||
}}, | ||
"PT1M", | ||
nil, | ||
).Once() | ||
} | ||
|
||
client.AzureMonitorService = m | ||
mr := MockReporterV2{} | ||
|
||
metricValues := client.GetMetricValues(referenceTime, client.ResourceConfigurations.Metrics, &mr) | ||
|
||
require.Equal(t, 3, len(metricValues)) | ||
require.Equal(t, 1, len(metricValues[0].Values)) | ||
|
||
require.NotNil(t, metricValues[0].Values[0].max, "max value is nil") | ||
require.NotNil(t, metricValues[0].Values[0].min, "min value is nil") | ||
require.NotNil(t, metricValues[0].Values[0].avg, "avg value is nil") | ||
|
||
assert.Equal(t, *metricValues[0].Values[0].max, 3.0) | ||
assert.Equal(t, *metricValues[2].Values[0].min, 1.0) | ||
assert.Equal(t, *metricValues[1].Values[0].avg, 2.0) | ||
|
||
m.AssertExpectations(t) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters