diff --git a/metricproviders/datadog/datadog.go b/metricproviders/datadog/datadog.go index 48b60fc0f3..47623b6b9f 100644 --- a/metricproviders/datadog/datadog.go +++ b/metricproviders/datadog/datadog.go @@ -224,6 +224,10 @@ func (p *Provider) createRequestV2(queries map[string]string, formula string, no "formula": formula, }} } + // we cannot leave aggregator empty as it will be passed as such to datadog API and fail + if aggregator == "" { + aggregator = "last" + } attribs := datadogQueryAttributes{ // Datadog requires milliseconds for v2 api diff --git a/metricproviders/datadog/datadogV2_test.go b/metricproviders/datadog/datadogV2_test.go index f3e481160e..b8319423f3 100644 --- a/metricproviders/datadog/datadogV2_test.go +++ b/metricproviders/datadog/datadogV2_test.go @@ -52,6 +52,16 @@ func newQueryProviderInterval10m() v1alpha1.MetricProvider { } } +func newQueryProviderSumAggregator() v1alpha1.MetricProvider { + return v1alpha1.MetricProvider{ + Datadog: &v1alpha1.DatadogMetric{ + Query: "avg:kubernetes.cpu.user.total{*}", + Interval: "5m", + Aggregator: "sum", + ApiVersion: "v2", + }, + } +} func TestRunSuiteV2(t *testing.T) { const expectedApiKey = "0123456789abcdef0123456789abcdef" const expectedAppKey = "0123456789abcdef0123456789abcdef01234567" @@ -68,6 +78,7 @@ func TestRunSuiteV2(t *testing.T) { expectedValue string expectedPhase v1alpha1.AnalysisPhase expectedErrorMessage string + expectedAggregator string useEnvVarForKeys bool }{ { @@ -252,6 +263,21 @@ func TestRunSuiteV2(t *testing.T) { expectedPhase: v1alpha1.AnalysisPhaseSuccessful, useEnvVarForKeys: false, }, + + { + webServerStatus: 200, + webServerResponse: `{"data": {"attributes": {"columns": [ {"values": [0.006121378742186943]}]}}}`, + metric: v1alpha1.Metric{ + Name: "success with default and data", + SuccessCondition: "default(result, 0) < 0.05", + Provider: newQueryProviderSumAggregator(), + }, + expectedIntervalSeconds: 300, + expectedValue: "0.006121378742186943", + expectedPhase: v1alpha1.AnalysisPhaseSuccessful, + expectedAggregator: "sum", + useEnvVarForKeys: false, + }, } // Run @@ -298,6 +324,16 @@ func TestRunSuiteV2(t *testing.T) { t.Errorf("\nExpected formula but no Formulas in request: %+v", actualFormulas) } } + // Check query aggregation being set + expectedAggregator := test.expectedAggregator + if expectedAggregator == "" { + expectedAggregator = "last" + } + for _, query := range actualQueries { + if query["aggregator"] != expectedAggregator { + t.Errorf("\naggregator expected %s but got %s", expectedAggregator, query["aggregator"]) + } + } if actualFrom != (unixNow()-test.expectedIntervalSeconds)*1000 { t.Errorf("\nfrom %d expected be equal to %d", actualFrom, (unixNow()-test.expectedIntervalSeconds)*1000)