CloudWatch: Fix segfault when migrating legacy queries (#93543)

This commit is contained in:
Isabella Siu 2024-09-20 10:54:34 -04:00 committed by GitHub
parent 216b63549d
commit 2ad558d046
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 1 deletions

View File

@ -385,7 +385,11 @@ func (q *CloudWatchQuery) validateAndSetDefaults(refId string, metricsDataQuery
func getStatistic(query metricsDataQuery) string {
// If there's not a statistic property in the json, we know it's the legacy format and then it has to be migrated
if query.Statistic == nil {
return query.Statistics[0]
if len(query.Statistics) > 0 {
return query.Statistics[0]
}
// if there isn't a statistic property in the legacy format fall back to Average
return "Average"
}
return *query.Statistic
}

View File

@ -296,6 +296,37 @@ func TestRequestParser(t *testing.T) {
assert.Equal(t, "Average", migratedQuery.Statistic)
})
t.Run("legacy statistics field is migrated: if no stat, uses Average", func(t *testing.T) {
oldQuery := []backend.DataQuery{
{
MaxDataPoints: 0,
QueryType: "timeSeriesQuery",
Interval: 0,
RefID: "A",
JSON: json.RawMessage(`{
"region":"us-east-1",
"namespace":"ec2",
"metricName":"CPUUtilization",
"dimensions":{
"InstanceId": ["test"]
},
"statistics":[],
"period":"600",
"hide":false
}`),
},
}
migratedQueries, err := ParseMetricDataQueries(oldQuery, time.Now(), time.Now(), "us-east-2", logger, false)
assert.NoError(t, err)
require.Len(t, migratedQueries, 1)
require.NotNil(t, migratedQueries[0])
migratedQuery := migratedQueries[0]
assert.Equal(t, "A", migratedQuery.RefId)
assert.Equal(t, "Average", migratedQuery.Statistic)
})
t.Run("New dimensions structure", func(t *testing.T) {
query := []backend.DataQuery{
{