Commit 9668c172 by ying-jeanne Committed by GitHub

CloudWatch: namespace in search expression should be quoted if match exact is…

CloudWatch: namespace in search expression should be quoted if match exact is enabled #29109 (#29563)
parent d0f8adae
...@@ -88,12 +88,11 @@ func buildSearchExpression(query *cloudWatchQuery, stat string) string { ...@@ -88,12 +88,11 @@ func buildSearchExpression(query *cloudWatchQuery, stat string) string {
} }
if query.MatchExact { if query.MatchExact {
schema := query.Namespace schema := fmt.Sprintf("%q", query.Namespace)
if len(dimensionNames) > 0 { if len(dimensionNames) > 0 {
sort.Strings(dimensionNames) sort.Strings(dimensionNames)
schema += fmt.Sprintf(",%s", join(dimensionNames, ",", `"`, `"`)) schema += fmt.Sprintf(",%s", join(dimensionNames, ",", `"`, `"`))
} }
return fmt.Sprintf("REMOVE_EMPTY(SEARCH('{%s} %s', '%s', %s))", schema, searchTerm, stat, strconv.Itoa(query.Period)) return fmt.Sprintf("REMOVE_EMPTY(SEARCH('{%s} %s', '%s', %s))", schema, searchTerm, stat, strconv.Itoa(query.Period))
} }
......
...@@ -23,7 +23,7 @@ func TestMetricDataQueryBuilder_buildSearchExpression(t *testing.T) { ...@@ -23,7 +23,7 @@ func TestMetricDataQueryBuilder_buildSearchExpression(t *testing.T) {
} }
res := buildSearchExpression(query, "Average") res := buildSearchExpression(query, "Average")
assert.Equal(t, `REMOVE_EMPTY(SEARCH('{AWS/EC2,"LoadBalancer"} MetricName="CPUUtilization" "LoadBalancer"=("lb1" OR "lb2" OR "lb3")', 'Average', 300))`, res) assert.Equal(t, `REMOVE_EMPTY(SEARCH('{"AWS/EC2","LoadBalancer"} MetricName="CPUUtilization" "LoadBalancer"=("lb1" OR "lb2" OR "lb3")', 'Average', 300))`, res)
}) })
t.Run("Query has three dimension values for two given dimension keys", func(t *testing.T) { t.Run("Query has three dimension values for two given dimension keys", func(t *testing.T) {
...@@ -40,7 +40,7 @@ func TestMetricDataQueryBuilder_buildSearchExpression(t *testing.T) { ...@@ -40,7 +40,7 @@ func TestMetricDataQueryBuilder_buildSearchExpression(t *testing.T) {
} }
res := buildSearchExpression(query, "Average") res := buildSearchExpression(query, "Average")
assert.Equal(t, `REMOVE_EMPTY(SEARCH('{AWS/EC2,"InstanceId","LoadBalancer"} MetricName="CPUUtilization" "InstanceId"=("i-123" OR "i-456" OR "i-789") "LoadBalancer"=("lb1" OR "lb2" OR "lb3")', 'Average', 300))`, res) assert.Equal(t, `REMOVE_EMPTY(SEARCH('{"AWS/EC2","InstanceId","LoadBalancer"} MetricName="CPUUtilization" "InstanceId"=("i-123" OR "i-456" OR "i-789") "LoadBalancer"=("lb1" OR "lb2" OR "lb3")', 'Average', 300))`, res)
}) })
t.Run("No OR operator was added if a star was used for dimension value", func(t *testing.T) { t.Run("No OR operator was added if a star was used for dimension value", func(t *testing.T) {
...@@ -72,7 +72,7 @@ func TestMetricDataQueryBuilder_buildSearchExpression(t *testing.T) { ...@@ -72,7 +72,7 @@ func TestMetricDataQueryBuilder_buildSearchExpression(t *testing.T) {
} }
res := buildSearchExpression(query, "Average") res := buildSearchExpression(query, "Average")
assert.Equal(t, `REMOVE_EMPTY(SEARCH('{AWS/EC2,"LoadBalancer"} MetricName="CPUUtilization"', 'Average', 300))`, res) assert.Equal(t, `REMOVE_EMPTY(SEARCH('{"AWS/EC2","LoadBalancer"} MetricName="CPUUtilization"', 'Average', 300))`, res)
}) })
t.Run("Query has three dimension values for two given dimension keys, and one value is a star", func(t *testing.T) { t.Run("Query has three dimension values for two given dimension keys, and one value is a star", func(t *testing.T) {
...@@ -89,7 +89,7 @@ func TestMetricDataQueryBuilder_buildSearchExpression(t *testing.T) { ...@@ -89,7 +89,7 @@ func TestMetricDataQueryBuilder_buildSearchExpression(t *testing.T) {
} }
res := buildSearchExpression(query, "Average") res := buildSearchExpression(query, "Average")
assert.Equal(t, `REMOVE_EMPTY(SEARCH('{AWS/EC2,"InstanceId","LoadBalancer"} MetricName="CPUUtilization" "LoadBalancer"=("lb1" OR "lb2" OR "lb3")', 'Average', 300))`, res) assert.Equal(t, `REMOVE_EMPTY(SEARCH('{"AWS/EC2","InstanceId","LoadBalancer"} MetricName="CPUUtilization" "LoadBalancer"=("lb1" OR "lb2" OR "lb3")', 'Average', 300))`, res)
}) })
t.Run("Query has a dimension key with a space", func(t *testing.T) { t.Run("Query has a dimension key with a space", func(t *testing.T) {
...@@ -105,7 +105,24 @@ func TestMetricDataQueryBuilder_buildSearchExpression(t *testing.T) { ...@@ -105,7 +105,24 @@ func TestMetricDataQueryBuilder_buildSearchExpression(t *testing.T) {
} }
res := buildSearchExpression(query, "Average") res := buildSearchExpression(query, "Average")
assert.Equal(t, `REMOVE_EMPTY(SEARCH('{AWS/Kafka,"Cluster Name"} MetricName="CpuUser" "Cluster Name"="dev-cluster"', 'Average', 300))`, res) assert.Equal(t, `REMOVE_EMPTY(SEARCH('{"AWS/Kafka","Cluster Name"} MetricName="CpuUser" "Cluster Name"="dev-cluster"', 'Average', 300))`, res)
})
t.Run("Query has a custom namespace contains spaces", func(t *testing.T) {
query := &cloudWatchQuery{
Namespace: "Test-API Cache by Minute",
MetricName: "CpuUser",
Dimensions: map[string][]string{
"LoadBalancer": {"lb1", "lb2", "lb3"},
"InstanceId": {"i-123", "*", "i-789"},
},
Period: 300,
Expression: "",
MatchExact: matchExact,
}
res := buildSearchExpression(query, "Average")
assert.Equal(t, `REMOVE_EMPTY(SEARCH('{"Test-API Cache by Minute","InstanceId","LoadBalancer"} MetricName="CpuUser" "LoadBalancer"=("lb1" OR "lb2" OR "lb3")', 'Average', 300))`, res)
}) })
}) })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment