CloudWatch: Add labels for Metric Query type queries (#85766)

* CloudWatch: Fix metric query with group by not being labelled in alerts

* just use one key for the labels

* not needed

* unused function

* add tests

* pr comments

* fetch dimensions to build labels for MetricQuery type queries

* pr comments

* group cache related tests and use fresh cache for non-cache related tests

* don't cache empty values
This commit is contained in:
Kevin Yu
2024-04-30 09:06:16 -07:00
committed by GitHub
parent b0f6913ef6
commit a54df47976
7 changed files with 317 additions and 105 deletions
+16 -3
View File
@@ -114,17 +114,28 @@ func parseLabels(cloudwatchLabel string, query *models.CloudWatchQuery) (string,
return splitLabels[0], labels
}
func getLabels(cloudwatchLabel string, query *models.CloudWatchQuery) data.Labels {
func getLabels(cloudwatchLabel string, query *models.CloudWatchQuery, addSeriesLabelAsFallback bool) data.Labels {
dims := make([]string, 0, len(query.Dimensions))
for k := range query.Dimensions {
dims = append(dims, k)
}
sort.Strings(dims)
labels := data.Labels{}
if addSeriesLabelAsFallback {
labels["Series"] = cloudwatchLabel
}
for _, dim := range dims {
values := query.Dimensions[dim]
if len(values) == 1 && values[0] != "*" {
labels[dim] = values[0]
} else if len(values) == 0 {
// Metric Insights metrics might not have a value for a dimension specified in the `GROUP BY` clause for Metric Query type queries. When this happens, CloudWatch returns "Other" in the label for the dimension so `len(values)` would be 0.
// We manually add "Other" as the value for the dimension to match what CloudWatch returns in the label.
// See the note under `GROUP BY` in https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-metrics-insights-querylanguage.html
labels[dim] = "Other"
continue
} else {
for _, value := range values {
if value == cloudwatchLabel || value == "*" {
@@ -195,10 +206,12 @@ func buildDataFrames(ctx context.Context, startTime time.Time, endTime time.Time
name := label
var labels data.Labels
if features.IsEnabled(ctx, features.FlagCloudWatchNewLabelParsing) {
if query.GetGetMetricDataAPIMode() == models.GMDApiModeSQLExpression {
labels = getLabels(label, query, true)
} else if features.IsEnabled(ctx, features.FlagCloudWatchNewLabelParsing) {
name, labels = parseLabels(label, query)
} else {
labels = getLabels(label, query)
labels = getLabels(label, query, false)
}
timestamps := []*time.Time{}
points := []*float64{}