CloudWatch: Add support for AWS Metric Insights (#42487)

* add support for code editor and builder

* refactor cloudwatch migration

* Add tooltip to editor field (#56)

* add tooltip

* add old tooltips

* Bug bash feedback fixes (#58)

* make ASC the default option

* update sql preview whenever sql changes

* don't allow queries without aggregation

* set default value for aggregation

* use new input field

* cleanup

* pr feedback

* prevent unnecessary rerenders

* use frame error instead of main error

* remove not used snapshot

* Use dimension filter in schema picker  (#63)

* use dimension key filter in group by and schema labels

* add dimension filter also to code editor

* add tests

* fix build error

* fix strict error

* remove debug code

* fix annotation editor (#64)

* fix annotation editor

* fix broken test

* revert annotation backend change

* PR feedback (#67)

* pr feedback

* removed dimension filter from group by

* add spacing between common fields and rest

* do not generate deep link for metric queries (#70)

* update docs (#69)

Co-authored-by: Erik Sundell <erik.sundell87@gmail.com>

* fix lint problem caused by merge conflict

Co-authored-by: achatterjee-grafana <70489351+achatterjee-grafana@users.noreply.github.com>
This commit is contained in:
Erik Sundell
2021-11-30 10:53:31 +01:00
committed by GitHub
co-authored by Erik Sundell achatterjee-grafana
parent 2a50c029b2
commit bab78a9e64
86 changed files with 6487 additions and 1010 deletions
@@ -7,21 +7,63 @@ import (
"github.com/stretchr/testify/require"
)
func TestMetricDataQueryBuilder_buildSearchExpression(t *testing.T) {
func TestMetricDataQueryBuilder(t *testing.T) {
t.Run("buildMetricDataQuery", func(t *testing.T) {
t.Run("should use metric stat", func(t *testing.T) {
executor := newExecutor(nil, nil, newTestConfig(), fakeSessionCache{})
query := getBaseQuery()
query.MetricEditorMode = MetricEditorModeBuilder
query.MetricQueryType = MetricQueryTypeSearch
mdq, err := executor.buildMetricDataQuery(query)
require.NoError(t, err)
require.Empty(t, mdq.Expression)
assert.Equal(t, query.MetricName, *mdq.MetricStat.Metric.MetricName)
assert.Equal(t, query.Namespace, *mdq.MetricStat.Metric.Namespace)
})
t.Run("should use custom built expression", func(t *testing.T) {
executor := newExecutor(nil, nil, newTestConfig(), fakeSessionCache{})
query := getBaseQuery()
query.MetricEditorMode = MetricEditorModeBuilder
query.MetricQueryType = MetricQueryTypeSearch
query.MatchExact = false
mdq, err := executor.buildMetricDataQuery(query)
require.NoError(t, err)
require.Nil(t, mdq.MetricStat)
assert.Equal(t, `REMOVE_EMPTY(SEARCH('Namespace="AWS/EC2" MetricName="CPUUtilization" "LoadBalancer"="lb1"', '', 300))`, *mdq.Expression)
})
t.Run("should use sql expression", func(t *testing.T) {
executor := newExecutor(nil, nil, newTestConfig(), fakeSessionCache{})
query := getBaseQuery()
query.MetricEditorMode = MetricEditorModeRaw
query.MetricQueryType = MetricQueryTypeQuery
query.SqlExpression = `SELECT SUM(CPUUTilization) FROM "AWS/EC2"`
mdq, err := executor.buildMetricDataQuery(query)
require.NoError(t, err)
require.Nil(t, mdq.MetricStat)
assert.Equal(t, query.SqlExpression, *mdq.Expression)
})
t.Run("should use user defined math expression", func(t *testing.T) {
executor := newExecutor(nil, nil, newTestConfig(), fakeSessionCache{})
query := getBaseQuery()
query.MetricEditorMode = MetricEditorModeRaw
query.MetricQueryType = MetricQueryTypeSearch
query.Expression = `SUM(x+y)`
mdq, err := executor.buildMetricDataQuery(query)
require.NoError(t, err)
require.Nil(t, mdq.MetricStat)
assert.Equal(t, query.Expression, *mdq.Expression)
})
t.Run("should set period in user defined expression", func(t *testing.T) {
executor := newExecutor(nil, nil, newTestConfig(), fakeSessionCache{})
query := &cloudWatchQuery{
Namespace: "AWS/EC2",
MetricName: "CPUUtilization",
Dimensions: map[string][]string{
"LoadBalancer": {"lb1"},
},
Period: 300,
Expression: "SUM([a,b])",
MatchExact: true,
}
query := getBaseQuery()
query.MetricEditorMode = MetricEditorModeRaw
query.MetricQueryType = MetricQueryTypeSearch
query.MatchExact = false
query.Expression = `SUM([a,b])`
mdq, err := executor.buildMetricDataQuery(query)
require.NoError(t, err)
require.Nil(t, mdq.MetricStat)
@@ -235,3 +277,17 @@ func TestMetricDataQueryBuilder_buildSearchExpression(t *testing.T) {
assert.Contains(t, res, `lb4\"\"`, "Expected escape double quotes")
})
}
func getBaseQuery() *cloudWatchQuery {
query := &cloudWatchQuery{
Namespace: "AWS/EC2",
MetricName: "CPUUtilization",
Dimensions: map[string][]string{
"LoadBalancer": {"lb1"},
},
Period: 300,
Expression: "",
MatchExact: true,
}
return query
}