Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(analysis): explicitly set datadog aggregator to last only on v2 #3730

Merged
merged 2 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions metricproviders/datadog/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 36 additions & 0 deletions metricproviders/datadog/datadogV2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -68,6 +78,7 @@ func TestRunSuiteV2(t *testing.T) {
expectedValue string
expectedPhase v1alpha1.AnalysisPhase
expectedErrorMessage string
expectedAggregator string
useEnvVarForKeys bool
}{
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Loading