CloudWatch: move QueryData input parsing types to separate package (#57165)
* CloudWatch: move parse request types separate package * Move metric query constants, unexport metricDataQuery json decoding type * Unexport isSearchExpression
This commit is contained in:
@@ -0,0 +1,184 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/cwlog"
|
||||
)
|
||||
|
||||
type (
|
||||
MetricEditorMode uint32
|
||||
MetricQueryType uint32
|
||||
GMDApiMode uint32
|
||||
)
|
||||
|
||||
const (
|
||||
MetricEditorModeBuilder MetricEditorMode = iota
|
||||
MetricEditorModeRaw
|
||||
)
|
||||
|
||||
const (
|
||||
MetricQueryTypeSearch MetricQueryType = iota
|
||||
MetricQueryTypeQuery
|
||||
)
|
||||
|
||||
const (
|
||||
GMDApiModeMetricStat GMDApiMode = iota
|
||||
GMDApiModeInferredSearchExpression
|
||||
GMDApiModeMathExpression
|
||||
GMDApiModeSQLExpression
|
||||
)
|
||||
|
||||
type CloudWatchQuery struct {
|
||||
RefId string
|
||||
Region string
|
||||
Id string
|
||||
Namespace string
|
||||
MetricName string
|
||||
Statistic string
|
||||
Expression string
|
||||
SqlExpression string
|
||||
ReturnData bool
|
||||
Dimensions map[string][]string
|
||||
Period int
|
||||
Alias string
|
||||
Label string
|
||||
MatchExact bool
|
||||
UsedExpression string
|
||||
TimezoneUTCOffset string
|
||||
MetricQueryType MetricQueryType
|
||||
MetricEditorMode MetricEditorMode
|
||||
}
|
||||
|
||||
func (q *CloudWatchQuery) GetGMDAPIMode() GMDApiMode {
|
||||
if q.MetricQueryType == MetricQueryTypeSearch && q.MetricEditorMode == MetricEditorModeBuilder {
|
||||
if q.IsInferredSearchExpression() {
|
||||
return GMDApiModeInferredSearchExpression
|
||||
}
|
||||
return GMDApiModeMetricStat
|
||||
} else if q.MetricQueryType == MetricQueryTypeSearch && q.MetricEditorMode == MetricEditorModeRaw {
|
||||
return GMDApiModeMathExpression
|
||||
} else if q.MetricQueryType == MetricQueryTypeQuery {
|
||||
return GMDApiModeSQLExpression
|
||||
}
|
||||
|
||||
cwlog.Warn("could not resolve CloudWatch metric query type. Falling back to metric stat.", "query", q)
|
||||
return GMDApiModeMetricStat
|
||||
}
|
||||
|
||||
func (q *CloudWatchQuery) IsMathExpression() bool {
|
||||
return q.MetricQueryType == MetricQueryTypeSearch && q.MetricEditorMode == MetricEditorModeRaw && !q.IsUserDefinedSearchExpression()
|
||||
}
|
||||
|
||||
func (q *CloudWatchQuery) isSearchExpression() bool {
|
||||
return q.MetricQueryType == MetricQueryTypeSearch && (q.IsUserDefinedSearchExpression() || q.IsInferredSearchExpression())
|
||||
}
|
||||
|
||||
func (q *CloudWatchQuery) IsUserDefinedSearchExpression() bool {
|
||||
return q.MetricQueryType == MetricQueryTypeSearch && q.MetricEditorMode == MetricEditorModeRaw && strings.Contains(q.Expression, "SEARCH(")
|
||||
}
|
||||
|
||||
func (q *CloudWatchQuery) IsInferredSearchExpression() bool {
|
||||
if q.MetricQueryType != MetricQueryTypeSearch || q.MetricEditorMode != MetricEditorModeBuilder {
|
||||
return false
|
||||
}
|
||||
|
||||
if len(q.Dimensions) == 0 {
|
||||
return !q.MatchExact
|
||||
}
|
||||
if !q.MatchExact {
|
||||
return true
|
||||
}
|
||||
|
||||
for _, values := range q.Dimensions {
|
||||
if len(values) > 1 {
|
||||
return true
|
||||
}
|
||||
for _, v := range values {
|
||||
if v == "*" {
|
||||
return true
|
||||
}
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (q *CloudWatchQuery) IsMultiValuedDimensionExpression() bool {
|
||||
if q.MetricQueryType != MetricQueryTypeSearch || q.MetricEditorMode != MetricEditorModeBuilder {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, values := range q.Dimensions {
|
||||
for _, v := range values {
|
||||
if v == "*" {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
if len(values) > 1 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func (q *CloudWatchQuery) BuildDeepLink(startTime time.Time, endTime time.Time, dynamicLabelEnabled bool) (string, error) {
|
||||
if q.IsMathExpression() || q.MetricQueryType == MetricQueryTypeQuery {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
link := &cloudWatchLink{
|
||||
Title: q.RefId,
|
||||
View: "timeSeries",
|
||||
Stacked: false,
|
||||
Region: q.Region,
|
||||
Start: startTime.UTC().Format(time.RFC3339),
|
||||
End: endTime.UTC().Format(time.RFC3339),
|
||||
}
|
||||
|
||||
if q.isSearchExpression() {
|
||||
metricExpressions := &metricExpression{Expression: q.UsedExpression}
|
||||
if dynamicLabelEnabled {
|
||||
metricExpressions.Label = q.Label
|
||||
}
|
||||
link.Metrics = []interface{}{metricExpressions}
|
||||
} else {
|
||||
metricStat := []interface{}{q.Namespace, q.MetricName}
|
||||
for dimensionKey, dimensionValues := range q.Dimensions {
|
||||
metricStat = append(metricStat, dimensionKey, dimensionValues[0])
|
||||
}
|
||||
metricStatMeta := &metricStatMeta{
|
||||
Stat: q.Statistic,
|
||||
Period: q.Period,
|
||||
}
|
||||
if dynamicLabelEnabled {
|
||||
metricStatMeta.Label = q.Label
|
||||
}
|
||||
metricStat = append(metricStat, metricStatMeta)
|
||||
link.Metrics = []interface{}{metricStat}
|
||||
}
|
||||
|
||||
linkProps, err := json.Marshal(link)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("could not marshal link: %w", err)
|
||||
}
|
||||
|
||||
url, err := url.Parse(fmt.Sprintf(`https://%s.console.aws.amazon.com/cloudwatch/deeplink.js`, q.Region))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("unable to parse CloudWatch console deep link")
|
||||
}
|
||||
|
||||
fragment := url.Query()
|
||||
fragment.Set("graph", string(linkProps))
|
||||
|
||||
query := url.Query()
|
||||
query.Set("region", q.Region)
|
||||
url.RawQuery = query.Encode()
|
||||
|
||||
return fmt.Sprintf(`%s#metricsV2:%s`, url.String(), fragment.Encode()), nil
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestCloudWatchQuery(t *testing.T) {
|
||||
t.Run("Deeplink", func(t *testing.T) {
|
||||
t.Run("is not generated for MetricQueryTypeQuery", func(t *testing.T) {
|
||||
startTime := time.Now()
|
||||
endTime := startTime.Add(2 * time.Hour)
|
||||
query := &CloudWatchQuery{
|
||||
RefId: "A",
|
||||
Region: "us-east-1",
|
||||
Expression: "",
|
||||
Statistic: "Average",
|
||||
Period: 300,
|
||||
Id: "id1",
|
||||
MatchExact: true,
|
||||
Dimensions: map[string][]string{
|
||||
"InstanceId": {"i-12345678"},
|
||||
},
|
||||
MetricQueryType: MetricQueryTypeQuery,
|
||||
MetricEditorMode: MetricEditorModeBuilder,
|
||||
}
|
||||
|
||||
deepLink, err := query.BuildDeepLink(startTime, endTime, false)
|
||||
require.NoError(t, err)
|
||||
assert.Empty(t, deepLink)
|
||||
})
|
||||
|
||||
t.Run("does not include label in case dynamic label is diabled", func(t *testing.T) {
|
||||
startTime := time.Now()
|
||||
endTime := startTime.Add(2 * time.Hour)
|
||||
query := &CloudWatchQuery{
|
||||
RefId: "A",
|
||||
Region: "us-east-1",
|
||||
Expression: "",
|
||||
Statistic: "Average",
|
||||
Period: 300,
|
||||
Id: "id1",
|
||||
MatchExact: true,
|
||||
Label: "${PROP('Namespace')}",
|
||||
Dimensions: map[string][]string{
|
||||
"InstanceId": {"i-12345678"},
|
||||
},
|
||||
MetricQueryType: MetricQueryTypeSearch,
|
||||
MetricEditorMode: MetricEditorModeBuilder,
|
||||
}
|
||||
|
||||
deepLink, err := query.BuildDeepLink(startTime, endTime, false)
|
||||
require.NoError(t, err)
|
||||
assert.NotContains(t, deepLink, "label")
|
||||
})
|
||||
|
||||
t.Run("includes label in case dynamic label is enabled and it's a metric stat query", func(t *testing.T) {
|
||||
startTime := time.Now()
|
||||
endTime := startTime.Add(2 * time.Hour)
|
||||
query := &CloudWatchQuery{
|
||||
RefId: "A",
|
||||
Region: "us-east-1",
|
||||
Expression: "",
|
||||
Statistic: "Average",
|
||||
Period: 300,
|
||||
Id: "id1",
|
||||
MatchExact: true,
|
||||
Label: "${PROP('Namespace')}",
|
||||
Dimensions: map[string][]string{
|
||||
"InstanceId": {"i-12345678"},
|
||||
},
|
||||
MetricQueryType: MetricQueryTypeSearch,
|
||||
MetricEditorMode: MetricEditorModeBuilder,
|
||||
}
|
||||
|
||||
deepLink, err := query.BuildDeepLink(startTime, endTime, false)
|
||||
require.NoError(t, err)
|
||||
assert.NotContains(t, deepLink, "label")
|
||||
})
|
||||
|
||||
t.Run("includes label in case dynamic label is enabled and it's a math expression query", func(t *testing.T) {
|
||||
startTime := time.Now()
|
||||
endTime := startTime.Add(2 * time.Hour)
|
||||
query := &CloudWatchQuery{
|
||||
RefId: "A",
|
||||
Region: "us-east-1",
|
||||
Statistic: "Average",
|
||||
Expression: "SEARCH(someexpression)",
|
||||
Period: 300,
|
||||
Id: "id1",
|
||||
MatchExact: true,
|
||||
Label: "${PROP('Namespace')}",
|
||||
MetricQueryType: MetricQueryTypeSearch,
|
||||
MetricEditorMode: MetricEditorModeRaw,
|
||||
}
|
||||
|
||||
deepLink, err := query.BuildDeepLink(startTime, endTime, false)
|
||||
require.NoError(t, err)
|
||||
assert.NotContains(t, deepLink, "label")
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("SEARCH(someexpression) was specified in the query editor", func(t *testing.T) {
|
||||
query := &CloudWatchQuery{
|
||||
RefId: "A",
|
||||
Region: "us-east-1",
|
||||
Expression: "SEARCH(someexpression)",
|
||||
Statistic: "Average",
|
||||
Period: 300,
|
||||
Id: "id1",
|
||||
}
|
||||
|
||||
assert.True(t, query.isSearchExpression(), "Expected a search expression")
|
||||
assert.False(t, query.IsMathExpression(), "Expected not math expression")
|
||||
})
|
||||
|
||||
t.Run("No expression, no multi dimension key values and no * was used", func(t *testing.T) {
|
||||
query := &CloudWatchQuery{
|
||||
RefId: "A",
|
||||
Region: "us-east-1",
|
||||
Expression: "",
|
||||
Statistic: "Average",
|
||||
Period: 300,
|
||||
Id: "id1",
|
||||
MatchExact: true,
|
||||
Dimensions: map[string][]string{
|
||||
"InstanceId": {"i-12345678"},
|
||||
},
|
||||
}
|
||||
|
||||
assert.False(t, query.isSearchExpression(), "Expected not a search expression")
|
||||
assert.False(t, query.IsMathExpression(), "Expected not math expressions")
|
||||
})
|
||||
|
||||
t.Run("No expression but multi dimension key values exist", func(t *testing.T) {
|
||||
query := &CloudWatchQuery{
|
||||
RefId: "A",
|
||||
Region: "us-east-1",
|
||||
Expression: "",
|
||||
Statistic: "Average",
|
||||
Period: 300,
|
||||
Id: "id1",
|
||||
Dimensions: map[string][]string{
|
||||
"InstanceId": {"i-12345678", "i-34562312"},
|
||||
},
|
||||
}
|
||||
|
||||
assert.True(t, query.isSearchExpression(), "Expected a search expression")
|
||||
assert.False(t, query.IsMathExpression(), "Expected not math expressions")
|
||||
})
|
||||
|
||||
t.Run("No expression but dimension values has *", func(t *testing.T) {
|
||||
query := &CloudWatchQuery{
|
||||
RefId: "A",
|
||||
Region: "us-east-1",
|
||||
Expression: "",
|
||||
Statistic: "Average",
|
||||
Period: 300,
|
||||
Id: "id1",
|
||||
Dimensions: map[string][]string{
|
||||
"InstanceId": {"i-12345678", "*"},
|
||||
"InstanceType": {"abc", "def"},
|
||||
},
|
||||
}
|
||||
|
||||
assert.True(t, query.isSearchExpression(), "Expected a search expression")
|
||||
assert.False(t, query.IsMathExpression(), "Expected not math expression")
|
||||
})
|
||||
|
||||
t.Run("Query has a multi-valued dimension", func(t *testing.T) {
|
||||
query := &CloudWatchQuery{
|
||||
RefId: "A",
|
||||
Region: "us-east-1",
|
||||
Expression: "",
|
||||
Statistic: "Average",
|
||||
Period: 300,
|
||||
Id: "id1",
|
||||
Dimensions: map[string][]string{
|
||||
"InstanceId": {"i-12345678", "i-12345679"},
|
||||
"InstanceType": {"abc"},
|
||||
},
|
||||
}
|
||||
|
||||
assert.True(t, query.isSearchExpression(), "Expected a search expression")
|
||||
assert.True(t, query.IsMultiValuedDimensionExpression(), "Expected a multi-valued dimension expression")
|
||||
})
|
||||
|
||||
t.Run("No dimensions were added", func(t *testing.T) {
|
||||
query := &CloudWatchQuery{
|
||||
RefId: "A",
|
||||
Region: "us-east-1",
|
||||
Expression: "",
|
||||
Statistic: "Average",
|
||||
Period: 300,
|
||||
Id: "id1",
|
||||
MatchExact: false,
|
||||
Dimensions: make(map[string][]string),
|
||||
}
|
||||
t.Run("Match exact is false", func(t *testing.T) {
|
||||
query.MatchExact = false
|
||||
assert.True(t, query.isSearchExpression(), "Expected a search expression")
|
||||
assert.False(t, query.IsMathExpression(), "Expected not math expression")
|
||||
})
|
||||
|
||||
t.Run("Match exact is true", func(t *testing.T) {
|
||||
query.MatchExact = true
|
||||
assert.False(t, query.isSearchExpression(), "Exxpected not search expression")
|
||||
assert.False(t, query.IsMathExpression(), "Expected not math expression")
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Match exact is", func(t *testing.T) {
|
||||
query := &CloudWatchQuery{
|
||||
RefId: "A",
|
||||
Region: "us-east-1",
|
||||
Expression: "",
|
||||
Statistic: "Average",
|
||||
Period: 300,
|
||||
Id: "id1",
|
||||
MatchExact: false,
|
||||
Dimensions: map[string][]string{
|
||||
"InstanceId": {"i-12345678"},
|
||||
},
|
||||
}
|
||||
|
||||
assert.True(t, query.isSearchExpression(), "Expected search expression")
|
||||
assert.False(t, query.IsMathExpression(), "Expected not math expression")
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package models
|
||||
|
||||
import "fmt"
|
||||
|
||||
type QueryError struct {
|
||||
Err error
|
||||
RefID string
|
||||
}
|
||||
|
||||
func (e *QueryError) Error() string {
|
||||
return fmt.Sprintf("error parsing query %q, %s", e.RefID, e.Err)
|
||||
}
|
||||
@@ -0,0 +1,312 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"regexp"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
|
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/cwlog"
|
||||
)
|
||||
|
||||
const timeSeriesQuery = "timeSeriesQuery"
|
||||
|
||||
var validMetricDataID = regexp.MustCompile(`^[a-z][a-zA-Z0-9_]*$`)
|
||||
|
||||
type metricsDataQuery struct {
|
||||
Datasource map[string]string `json:"datasource,omitempty"`
|
||||
Dimensions map[string]interface{} `json:"dimensions,omitempty"`
|
||||
Expression string `json:"expression,omitempty"`
|
||||
Id string `json:"id,omitempty"`
|
||||
Label *string `json:"label,omitempty"`
|
||||
MatchExact *bool `json:"matchExact,omitempty"`
|
||||
MaxDataPoints int `json:"maxDataPoints,omitempty"`
|
||||
MetricEditorMode *int `json:"metricEditorMode,omitempty"`
|
||||
MetricName string `json:"metricName,omitempty"`
|
||||
MetricQueryType MetricQueryType `json:"metricQueryType,omitempty"`
|
||||
Namespace string `json:"namespace,omitempty"`
|
||||
Period string `json:"period,omitempty"`
|
||||
RefId string `json:"refId,omitempty"`
|
||||
Region string `json:"region,omitempty"`
|
||||
SqlExpression string `json:"sqlExpression,omitempty"`
|
||||
Statistic *string `json:"statistic,omitempty"`
|
||||
Statistics []*string `json:"statistics,omitempty"`
|
||||
TimezoneUTCOffset string `json:"timezoneUTCOffset,omitempty"`
|
||||
QueryType string `json:"type,omitempty"`
|
||||
Hide *bool `json:"hide,omitempty"`
|
||||
Alias string `json:"alias,omitempty"`
|
||||
}
|
||||
|
||||
// ParseQueries parses the json queries and returns a map of cloudWatchQueries by region. The cloudWatchQuery has a 1 to 1 mapping to a query editor row
|
||||
func ParseQueries(queries []backend.DataQuery, startTime time.Time, endTime time.Time, dynamicLabelsEnabled bool) (map[string][]*CloudWatchQuery, error) {
|
||||
result := make(map[string][]*CloudWatchQuery)
|
||||
migratedQueries, err := migrateLegacyQuery(queries, dynamicLabelsEnabled)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, query := range migratedQueries {
|
||||
var metricsDataQuery metricsDataQuery
|
||||
err := json.Unmarshal(query.JSON, &metricsDataQuery)
|
||||
if err != nil {
|
||||
return nil, &QueryError{Err: err, RefID: query.RefID}
|
||||
}
|
||||
|
||||
queryType := metricsDataQuery.QueryType
|
||||
if queryType != timeSeriesQuery && queryType != "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if metricsDataQuery.MatchExact == nil {
|
||||
trueBooleanValue := true
|
||||
metricsDataQuery.MatchExact = &trueBooleanValue
|
||||
}
|
||||
|
||||
refID := query.RefID
|
||||
query, err := parseRequestQuery(metricsDataQuery, refID, startTime, endTime)
|
||||
if err != nil {
|
||||
return nil, &QueryError{Err: err, RefID: refID}
|
||||
}
|
||||
|
||||
if _, exist := result[query.Region]; !exist {
|
||||
result[query.Region] = []*CloudWatchQuery{}
|
||||
}
|
||||
result[query.Region] = append(result[query.Region], query)
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// migrateLegacyQuery is also done in the frontend, so this should only ever be needed for alerting queries
|
||||
func migrateLegacyQuery(queries []backend.DataQuery, dynamicLabelsEnabled bool) ([]*backend.DataQuery, error) {
|
||||
migratedQueries := []*backend.DataQuery{}
|
||||
for _, q := range queries {
|
||||
query := q
|
||||
var queryJson *metricsDataQuery
|
||||
err := json.Unmarshal(query.JSON, &queryJson)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := migrateStatisticsToStatistic(queryJson); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if queryJson.Label == nil && dynamicLabelsEnabled {
|
||||
migrateAliasToDynamicLabel(queryJson)
|
||||
}
|
||||
query.JSON, err = json.Marshal(queryJson)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
migratedQueries = append(migratedQueries, &query)
|
||||
}
|
||||
|
||||
return migratedQueries, nil
|
||||
}
|
||||
|
||||
// migrateStatisticsToStatistic migrates queries that has a `statistics` field to use the `statistic` field instead.
|
||||
// In case the query used more than one stat, the first stat in the slice will be used in the statistic field
|
||||
// Read more here https://github.com/grafana/grafana/issues/30629
|
||||
func migrateStatisticsToStatistic(queryJson *metricsDataQuery) error {
|
||||
// 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 queryJson.Statistic == nil {
|
||||
if queryJson.Statistics == nil {
|
||||
return fmt.Errorf("query must have either statistic or statistics field")
|
||||
}
|
||||
|
||||
queryJson.Statistic = queryJson.Statistics[0]
|
||||
queryJson.Statistics = nil
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var aliasPatterns = map[string]string{
|
||||
"metric": `${PROP('MetricName')}`,
|
||||
"namespace": `${PROP('Namespace')}`,
|
||||
"period": `${PROP('Period')}`,
|
||||
"region": `${PROP('Region')}`,
|
||||
"stat": `${PROP('Stat')}`,
|
||||
"label": `${LABEL}`,
|
||||
}
|
||||
|
||||
var legacyAliasRegexp = regexp.MustCompile(`{{\s*(.+?)\s*}}`)
|
||||
|
||||
func migrateAliasToDynamicLabel(queryJson *metricsDataQuery) {
|
||||
fullAliasField := queryJson.Alias
|
||||
|
||||
if fullAliasField != "" {
|
||||
matches := legacyAliasRegexp.FindAllStringSubmatch(fullAliasField, -1)
|
||||
|
||||
for _, groups := range matches {
|
||||
fullMatch := groups[0]
|
||||
subgroup := groups[1]
|
||||
if dynamicLabel, ok := aliasPatterns[subgroup]; ok {
|
||||
fullAliasField = strings.ReplaceAll(fullAliasField, fullMatch, dynamicLabel)
|
||||
} else {
|
||||
fullAliasField = strings.ReplaceAll(fullAliasField, fullMatch, fmt.Sprintf(`${PROP('Dim.%s')}`, subgroup))
|
||||
}
|
||||
}
|
||||
}
|
||||
queryJson.Label = &fullAliasField
|
||||
}
|
||||
|
||||
func parseRequestQuery(dataQuery metricsDataQuery, refId string, startTime time.Time, endTime time.Time) (*CloudWatchQuery, error) {
|
||||
cwlog.Debug("Parsing request query", "query", dataQuery)
|
||||
result := CloudWatchQuery{
|
||||
Alias: dataQuery.Alias,
|
||||
Label: "",
|
||||
MatchExact: true,
|
||||
Statistic: "",
|
||||
ReturnData: true,
|
||||
UsedExpression: "",
|
||||
RefId: refId,
|
||||
Id: dataQuery.Id,
|
||||
Region: dataQuery.Region,
|
||||
Namespace: dataQuery.Namespace,
|
||||
MetricName: dataQuery.MetricName,
|
||||
MetricQueryType: dataQuery.MetricQueryType,
|
||||
SqlExpression: dataQuery.SqlExpression,
|
||||
TimezoneUTCOffset: dataQuery.TimezoneUTCOffset,
|
||||
Expression: dataQuery.Expression,
|
||||
}
|
||||
reNumber := regexp.MustCompile(`^\d+$`)
|
||||
dimensions, err := parseDimensions(dataQuery.Dimensions)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse dimensions: %v", err)
|
||||
}
|
||||
result.Dimensions = dimensions
|
||||
|
||||
p := dataQuery.Period
|
||||
var period int
|
||||
if strings.ToLower(p) == "auto" || p == "" {
|
||||
deltaInSeconds := endTime.Sub(startTime).Seconds()
|
||||
periods := getRetainedPeriods(time.Since(startTime))
|
||||
datapoints := int(math.Ceil(deltaInSeconds / 2000))
|
||||
period = periods[len(periods)-1]
|
||||
for _, value := range periods {
|
||||
if datapoints <= value {
|
||||
period = value
|
||||
break
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if reNumber.Match([]byte(p)) {
|
||||
period, err = strconv.Atoi(p)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse period as integer: %v", err)
|
||||
}
|
||||
} else {
|
||||
d, err := time.ParseDuration(p)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse period as duration: %v", err)
|
||||
}
|
||||
period = int(d.Seconds())
|
||||
}
|
||||
}
|
||||
result.Period = period
|
||||
|
||||
if dataQuery.Id == "" {
|
||||
// Why not just use refId if id is not specified in the frontend? When specifying an id in the editor,
|
||||
// and alphabetical must be used. The id must be unique, so if an id like for example a, b or c would be used,
|
||||
// it would likely collide with some ref id. That's why the `query` prefix is used.
|
||||
suffix := refId
|
||||
if !validMetricDataID.MatchString(suffix) {
|
||||
newUUID := uuid.NewString()
|
||||
suffix = strings.Replace(newUUID, "-", "", -1)
|
||||
}
|
||||
result.Id = fmt.Sprintf("query%s", suffix)
|
||||
}
|
||||
|
||||
if dataQuery.Hide != nil {
|
||||
result.ReturnData = !*dataQuery.Hide
|
||||
}
|
||||
|
||||
if dataQuery.QueryType == "" {
|
||||
// If no type is provided we assume we are called by alerting service, which requires to return data!
|
||||
// Note, this is sort of a hack, but the official Grafana interfaces do not carry the information
|
||||
// who (which service) called the TsdbQueryEndpoint.Query(...) function.
|
||||
result.ReturnData = true
|
||||
}
|
||||
|
||||
if dataQuery.MetricEditorMode == nil && len(dataQuery.Expression) > 0 {
|
||||
// this should only ever happen if this is an alerting query that has not yet been migrated in the frontend
|
||||
result.MetricEditorMode = MetricEditorModeRaw
|
||||
} else {
|
||||
if dataQuery.MetricEditorMode != nil {
|
||||
result.MetricEditorMode = MetricEditorMode(*dataQuery.MetricEditorMode)
|
||||
} else {
|
||||
result.MetricEditorMode = MetricEditorMode(0)
|
||||
}
|
||||
}
|
||||
|
||||
if dataQuery.Statistic != nil {
|
||||
result.Statistic = *dataQuery.Statistic
|
||||
}
|
||||
|
||||
if dataQuery.MatchExact != nil {
|
||||
result.MatchExact = *dataQuery.MatchExact
|
||||
}
|
||||
|
||||
if dataQuery.Label != nil {
|
||||
result.Label = *dataQuery.Label
|
||||
}
|
||||
|
||||
return &result, nil
|
||||
}
|
||||
|
||||
func getRetainedPeriods(timeSince time.Duration) []int {
|
||||
// See https://aws.amazon.com/about-aws/whats-new/2016/11/cloudwatch-extends-metrics-retention-and-new-user-interface/
|
||||
if timeSince > time.Duration(455)*24*time.Hour {
|
||||
return []int{21600, 86400}
|
||||
} else if timeSince > time.Duration(63)*24*time.Hour {
|
||||
return []int{3600, 21600, 86400}
|
||||
} else if timeSince > time.Duration(15)*24*time.Hour {
|
||||
return []int{300, 900, 3600, 21600, 86400}
|
||||
} else {
|
||||
return []int{60, 300, 900, 3600, 21600, 86400}
|
||||
}
|
||||
}
|
||||
|
||||
func parseDimensions(dimensions map[string]interface{}) (map[string][]string, error) {
|
||||
parsedDimensions := make(map[string][]string)
|
||||
for k, v := range dimensions {
|
||||
// This is for backwards compatibility. Before 6.5 dimensions values were stored as strings and not arrays
|
||||
if value, ok := v.(string); ok {
|
||||
parsedDimensions[k] = []string{value}
|
||||
} else if values, ok := v.([]interface{}); ok {
|
||||
for _, value := range values {
|
||||
parsedDimensions[k] = append(parsedDimensions[k], value.(string))
|
||||
}
|
||||
} else {
|
||||
return nil, errors.New("unknown type as dimension value")
|
||||
}
|
||||
}
|
||||
|
||||
sortedDimensions := sortDimensions(parsedDimensions)
|
||||
return sortedDimensions, nil
|
||||
}
|
||||
|
||||
func sortDimensions(dimensions map[string][]string) map[string][]string {
|
||||
sortedDimensions := make(map[string][]string)
|
||||
var keys []string
|
||||
for k := range dimensions {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
|
||||
for _, k := range keys {
|
||||
sortedDimensions[k] = dimensions[k]
|
||||
}
|
||||
return sortedDimensions
|
||||
}
|
||||
@@ -0,0 +1,603 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestQueryJSON(t *testing.T) {
|
||||
jsonString := []byte(`{
|
||||
"type": "timeSeriesQuery"
|
||||
}`)
|
||||
var res metricsDataQuery
|
||||
err := json.Unmarshal(jsonString, &res)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "timeSeriesQuery", res.QueryType)
|
||||
}
|
||||
|
||||
func TestRequestParser(t *testing.T) {
|
||||
average := "Average"
|
||||
false := false
|
||||
t.Run("Query migration ", func(t *testing.T) {
|
||||
t.Run("legacy statistics field is migrated", func(t *testing.T) {
|
||||
oldQuery := &backend.DataQuery{
|
||||
MaxDataPoints: 0,
|
||||
QueryType: "timeSeriesQuery",
|
||||
Interval: 0,
|
||||
}
|
||||
oldQuery.RefID = "A"
|
||||
oldQuery.JSON = []byte(`{
|
||||
"region": "us-east-1",
|
||||
"namespace": "ec2",
|
||||
"metricName": "CPUUtilization",
|
||||
"dimensions": {
|
||||
"InstanceId": ["test"]
|
||||
},
|
||||
"statistics": ["Average", "Sum"],
|
||||
"period": "600",
|
||||
"hide": false
|
||||
}`)
|
||||
migratedQueries, err := migrateLegacyQuery([]backend.DataQuery{*oldQuery}, false)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 1, len(migratedQueries))
|
||||
|
||||
migratedQuery := migratedQueries[0]
|
||||
assert.Equal(t, "A", migratedQuery.RefID)
|
||||
var model metricsDataQuery
|
||||
err = json.Unmarshal(migratedQuery.JSON, &model)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "Average", *model.Statistic)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("New dimensions structure", func(t *testing.T) {
|
||||
query := metricsDataQuery{
|
||||
RefId: "ref1",
|
||||
Region: "us-east-1",
|
||||
Namespace: "ec2",
|
||||
MetricName: "CPUUtilization",
|
||||
Id: "",
|
||||
Expression: "",
|
||||
Dimensions: map[string]interface{}{
|
||||
"InstanceId": []interface{}{"test"},
|
||||
"InstanceType": []interface{}{"test2", "test3"},
|
||||
},
|
||||
Statistic: &average,
|
||||
Period: "600",
|
||||
Hide: &false,
|
||||
}
|
||||
|
||||
res, err := parseRequestQuery(query, "ref1", time.Now().Add(-2*time.Hour), time.Now().Add(-time.Hour))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "us-east-1", res.Region)
|
||||
assert.Equal(t, "ref1", res.RefId)
|
||||
assert.Equal(t, "ec2", res.Namespace)
|
||||
assert.Equal(t, "CPUUtilization", res.MetricName)
|
||||
assert.Equal(t, "queryref1", res.Id)
|
||||
assert.Empty(t, res.Expression)
|
||||
assert.Equal(t, 600, res.Period)
|
||||
assert.True(t, res.ReturnData)
|
||||
assert.Len(t, res.Dimensions, 2)
|
||||
assert.Len(t, res.Dimensions["InstanceId"], 1)
|
||||
assert.Len(t, res.Dimensions["InstanceType"], 2)
|
||||
assert.Equal(t, "test3", res.Dimensions["InstanceType"][1])
|
||||
assert.Equal(t, "Average", res.Statistic)
|
||||
})
|
||||
|
||||
t.Run("Old dimensions structure (backwards compatibility)", func(t *testing.T) {
|
||||
query := metricsDataQuery{
|
||||
RefId: "ref1",
|
||||
Region: "us-east-1",
|
||||
Namespace: "ec2",
|
||||
MetricName: "CPUUtilization",
|
||||
Id: "",
|
||||
Expression: "",
|
||||
Dimensions: map[string]interface{}{
|
||||
"InstanceId": []interface{}{"test"},
|
||||
"InstanceType": []interface{}{"test2"},
|
||||
},
|
||||
Statistic: &average,
|
||||
Period: "600",
|
||||
Hide: &false,
|
||||
}
|
||||
|
||||
res, err := parseRequestQuery(query, "ref1", time.Now().Add(-2*time.Hour), time.Now().Add(-time.Hour))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "us-east-1", res.Region)
|
||||
assert.Equal(t, "ref1", res.RefId)
|
||||
assert.Equal(t, "ec2", res.Namespace)
|
||||
assert.Equal(t, "CPUUtilization", res.MetricName)
|
||||
assert.Equal(t, "queryref1", res.Id)
|
||||
assert.Empty(t, res.Expression)
|
||||
assert.Equal(t, 600, res.Period)
|
||||
assert.True(t, res.ReturnData)
|
||||
assert.Len(t, res.Dimensions, 2)
|
||||
assert.Len(t, res.Dimensions["InstanceId"], 1)
|
||||
assert.Len(t, res.Dimensions["InstanceType"], 1)
|
||||
assert.Equal(t, "test2", res.Dimensions["InstanceType"][0])
|
||||
assert.Equal(t, "Average", res.Statistic)
|
||||
})
|
||||
|
||||
t.Run("Period defined in the editor by the user is being used when time range is short", func(t *testing.T) {
|
||||
query := metricsDataQuery{
|
||||
RefId: "ref1",
|
||||
Region: "us-east-1",
|
||||
Namespace: "ec2",
|
||||
MetricName: "CPUUtilization",
|
||||
Id: "",
|
||||
Expression: "",
|
||||
Dimensions: map[string]interface{}{
|
||||
"InstanceId": []interface{}{"test"},
|
||||
"InstanceType": []interface{}{"test2"},
|
||||
},
|
||||
Statistic: &average,
|
||||
Period: "900",
|
||||
Hide: &false,
|
||||
}
|
||||
|
||||
res, err := parseRequestQuery(query, "ref1", time.Now().Add(-2*time.Hour), time.Now().Add(-time.Hour))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 900, res.Period)
|
||||
})
|
||||
|
||||
t.Run("Period is parsed correctly if not defined by user", func(t *testing.T) {
|
||||
query := metricsDataQuery{
|
||||
RefId: "ref1",
|
||||
Region: "us-east-1",
|
||||
Namespace: "ec2",
|
||||
MetricName: "CPUUtilization",
|
||||
Id: "",
|
||||
Expression: "",
|
||||
Dimensions: map[string]interface{}{
|
||||
"InstanceId": []interface{}{"test"},
|
||||
"InstanceType": []interface{}{"test2"},
|
||||
},
|
||||
Statistic: &average,
|
||||
Hide: &false,
|
||||
Period: "auto",
|
||||
}
|
||||
|
||||
t.Run("Time range is 5 minutes", func(t *testing.T) {
|
||||
query.Period = "auto"
|
||||
to := time.Now()
|
||||
from := to.Local().Add(time.Minute * time.Duration(5))
|
||||
|
||||
res, err := parseRequestQuery(query, "ref1", from, to)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 60, res.Period)
|
||||
})
|
||||
|
||||
t.Run("Time range is 1 day", func(t *testing.T) {
|
||||
query.Period = "auto"
|
||||
to := time.Now()
|
||||
from := to.AddDate(0, 0, -1)
|
||||
|
||||
res, err := parseRequestQuery(query, "ref1", from, to)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 60, res.Period)
|
||||
})
|
||||
|
||||
t.Run("Time range is 2 days", func(t *testing.T) {
|
||||
query.Period = "auto"
|
||||
to := time.Now()
|
||||
from := to.AddDate(0, 0, -2)
|
||||
res, err := parseRequestQuery(query, "ref1", from, to)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 300, res.Period)
|
||||
})
|
||||
|
||||
t.Run("Time range is 7 days", func(t *testing.T) {
|
||||
query.Period = "auto"
|
||||
to := time.Now()
|
||||
from := to.AddDate(0, 0, -7)
|
||||
|
||||
res, err := parseRequestQuery(query, "ref1", from, to)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 900, res.Period)
|
||||
})
|
||||
|
||||
t.Run("Time range is 30 days", func(t *testing.T) {
|
||||
query.Period = "auto"
|
||||
to := time.Now()
|
||||
from := to.AddDate(0, 0, -30)
|
||||
|
||||
res, err := parseRequestQuery(query, "ref1", from, to)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 3600, res.Period)
|
||||
})
|
||||
|
||||
t.Run("Time range is 90 days", func(t *testing.T) {
|
||||
query.Period = "auto"
|
||||
to := time.Now()
|
||||
from := to.AddDate(0, 0, -90)
|
||||
|
||||
res, err := parseRequestQuery(query, "ref1", from, to)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 21600, res.Period)
|
||||
})
|
||||
|
||||
t.Run("Time range is 1 year", func(t *testing.T) {
|
||||
query.Period = "auto"
|
||||
to := time.Now()
|
||||
from := to.AddDate(-1, 0, 0)
|
||||
|
||||
res, err := parseRequestQuery(query, "ref1", from, to)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, 21600, res.Period)
|
||||
})
|
||||
|
||||
t.Run("Time range is 2 years", func(t *testing.T) {
|
||||
query.Period = "auto"
|
||||
to := time.Now()
|
||||
from := to.AddDate(-2, 0, 0)
|
||||
|
||||
res, err := parseRequestQuery(query, "ref1", from, to)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 86400, res.Period)
|
||||
})
|
||||
|
||||
t.Run("Time range is 2 days, but 16 days ago", func(t *testing.T) {
|
||||
query.Period = "auto"
|
||||
to := time.Now().AddDate(0, 0, -14)
|
||||
from := to.AddDate(0, 0, -2)
|
||||
res, err := parseRequestQuery(query, "ref1", from, to)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 300, res.Period)
|
||||
})
|
||||
|
||||
t.Run("Time range is 2 days, but 90 days ago", func(t *testing.T) {
|
||||
query.Period = "auto"
|
||||
to := time.Now().AddDate(0, 0, -88)
|
||||
from := to.AddDate(0, 0, -2)
|
||||
res, err := parseRequestQuery(query, "ref1", from, to)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 3600, res.Period)
|
||||
})
|
||||
|
||||
t.Run("Time range is 2 days, but 456 days ago", func(t *testing.T) {
|
||||
query.Period = "auto"
|
||||
to := time.Now().AddDate(0, 0, -454)
|
||||
from := to.AddDate(0, 0, -2)
|
||||
res, err := parseRequestQuery(query, "ref1", from, to)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, 21600, res.Period)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("Metric query type, metric editor mode and query api mode", func(t *testing.T) {
|
||||
t.Run("when metric query type and metric editor mode is not specified", func(t *testing.T) {
|
||||
t.Run("it should be metric search builder", func(t *testing.T) {
|
||||
query := getBaseJsonQuery()
|
||||
res, err := parseRequestQuery(query, "ref1", time.Now().Add(-2*time.Hour), time.Now().Add(-time.Hour))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, MetricQueryTypeSearch, res.MetricQueryType)
|
||||
assert.Equal(t, MetricEditorModeBuilder, res.MetricEditorMode)
|
||||
assert.Equal(t, GMDApiModeMetricStat, res.GetGMDAPIMode())
|
||||
})
|
||||
|
||||
t.Run("and an expression is specified it should be metric search builder", func(t *testing.T) {
|
||||
query := getBaseJsonQuery()
|
||||
query.Expression = "SUM(a)"
|
||||
res, err := parseRequestQuery(query, "ref1", time.Now().Add(-2*time.Hour), time.Now().Add(-time.Hour))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, MetricQueryTypeSearch, res.MetricQueryType)
|
||||
assert.Equal(t, MetricEditorModeRaw, res.MetricEditorMode)
|
||||
assert.Equal(t, GMDApiModeMathExpression, res.GetGMDAPIMode())
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("and an expression is specified it should be metric search builder", func(t *testing.T) {
|
||||
query := getBaseJsonQuery()
|
||||
query.Expression = "SUM(a)"
|
||||
res, err := parseRequestQuery(query, "ref1", time.Now().Add(-2*time.Hour), time.Now().Add(-time.Hour))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, MetricQueryTypeSearch, res.MetricQueryType)
|
||||
assert.Equal(t, MetricEditorModeRaw, res.MetricEditorMode)
|
||||
assert.Equal(t, GMDApiModeMathExpression, res.GetGMDAPIMode())
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("hide and returnData", func(t *testing.T) {
|
||||
t.Run("default", func(t *testing.T) {
|
||||
query := getBaseJsonQuery()
|
||||
query.QueryType = "timeSeriesQuery"
|
||||
res, err := parseRequestQuery(query, "ref1", time.Now().Add(-2*time.Hour), time.Now().Add(-time.Hour))
|
||||
require.NoError(t, err)
|
||||
require.True(t, res.ReturnData)
|
||||
})
|
||||
t.Run("hide is true", func(t *testing.T) {
|
||||
query := getBaseJsonQuery()
|
||||
query.QueryType = "timeSeriesQuery"
|
||||
true := true
|
||||
query.Hide = &true
|
||||
res, err := parseRequestQuery(query, "ref1", time.Now().Add(-2*time.Hour), time.Now().Add(-time.Hour))
|
||||
require.NoError(t, err)
|
||||
require.False(t, res.ReturnData)
|
||||
})
|
||||
t.Run("hide is false", func(t *testing.T) {
|
||||
query := getBaseJsonQuery()
|
||||
query.QueryType = "timeSeriesQuery"
|
||||
false := false
|
||||
query.Hide = &false
|
||||
res, err := parseRequestQuery(query, "ref1", time.Now().Add(-2*time.Hour), time.Now().Add(-time.Hour))
|
||||
require.NoError(t, err)
|
||||
require.True(t, res.ReturnData)
|
||||
})
|
||||
})
|
||||
|
||||
t.Run("ID is the string `query` appended with refId if refId is a valid MetricData ID", func(t *testing.T) {
|
||||
query := getBaseJsonQuery()
|
||||
res, err := parseRequestQuery(query, "ref1", time.Now().Add(-2*time.Hour), time.Now().Add(-time.Hour))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "ref1", res.RefId)
|
||||
assert.Equal(t, "queryref1", res.Id)
|
||||
})
|
||||
|
||||
t.Run("Valid id is generated if ID is not provided and refId is not a valid MetricData ID", func(t *testing.T) {
|
||||
query := getBaseJsonQuery()
|
||||
query.RefId = "$$"
|
||||
res, err := parseRequestQuery(query, "$$", time.Now().Add(-2*time.Hour), time.Now().Add(-time.Hour))
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "$$", res.RefId)
|
||||
assert.Regexp(t, validMetricDataID, res.Id)
|
||||
})
|
||||
|
||||
t.Run("parseRequestQuery sets label when label is present in json query", func(t *testing.T) {
|
||||
query := getBaseJsonQuery()
|
||||
query.Alias = "some alias"
|
||||
|
||||
label := "some label"
|
||||
query.Label = &label
|
||||
|
||||
res, err := parseRequestQuery(query, "ref1", time.Now().Add(-2*time.Hour), time.Now().Add(-time.Hour))
|
||||
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, "some alias", res.Alias) // alias is unmodified
|
||||
assert.Equal(t, "some label", res.Label)
|
||||
})
|
||||
}
|
||||
|
||||
func getBaseJsonQuery() metricsDataQuery {
|
||||
average := "Average"
|
||||
return metricsDataQuery{
|
||||
RefId: "ref1",
|
||||
Region: "us-east-1",
|
||||
Namespace: "ec2",
|
||||
MetricName: "CPUUtilization",
|
||||
Statistic: &average,
|
||||
Period: "900",
|
||||
}
|
||||
}
|
||||
|
||||
func Test_migrateAliasToDynamicLabel_single_query_preserves_old_alias_and_creates_new_label(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
inputAlias string
|
||||
expectedLabel string
|
||||
}{
|
||||
"one known alias pattern: metric": {inputAlias: "{{metric}}", expectedLabel: "${PROP('MetricName')}"},
|
||||
"one known alias pattern: namespace": {inputAlias: "{{namespace}}", expectedLabel: "${PROP('Namespace')}"},
|
||||
"one known alias pattern: period": {inputAlias: "{{period}}", expectedLabel: "${PROP('Period')}"},
|
||||
"one known alias pattern: region": {inputAlias: "{{region}}", expectedLabel: "${PROP('Region')}"},
|
||||
"one known alias pattern: stat": {inputAlias: "{{stat}}", expectedLabel: "${PROP('Stat')}"},
|
||||
"one known alias pattern: label": {inputAlias: "{{label}}", expectedLabel: "${LABEL}"},
|
||||
"one unknown alias pattern becomes dimension": {inputAlias: "{{any_other_word}}", expectedLabel: "${PROP('Dim.any_other_word')}"},
|
||||
"one known alias pattern with spaces": {inputAlias: "{{ metric }}", expectedLabel: "${PROP('MetricName')}"},
|
||||
"multiple alias patterns": {inputAlias: "some {{combination }}{{ label}} and {{metric}}", expectedLabel: "some ${PROP('Dim.combination')}${LABEL} and ${PROP('MetricName')}"},
|
||||
"empty alias still migrates to empty label": {inputAlias: "", expectedLabel: ""},
|
||||
}
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
average := "Average"
|
||||
false := false
|
||||
|
||||
queryToMigrate := metricsDataQuery{
|
||||
Region: "us-east-1",
|
||||
Namespace: "ec2",
|
||||
MetricName: "CPUUtilization",
|
||||
Alias: tc.inputAlias,
|
||||
Dimensions: map[string]interface{}{
|
||||
"InstanceId": []interface{}{"test"},
|
||||
},
|
||||
Statistic: &average,
|
||||
Period: "600",
|
||||
Hide: &false,
|
||||
}
|
||||
|
||||
migrateAliasToDynamicLabel(&queryToMigrate)
|
||||
|
||||
expected := metricsDataQuery{
|
||||
Alias: tc.inputAlias,
|
||||
Dimensions: map[string]interface{}{
|
||||
"InstanceId": []interface{}{"test"},
|
||||
},
|
||||
Hide: &false,
|
||||
Label: &tc.expectedLabel,
|
||||
MetricName: "CPUUtilization",
|
||||
Namespace: "ec2",
|
||||
Period: "600",
|
||||
Region: "us-east-1",
|
||||
Statistic: &average,
|
||||
}
|
||||
|
||||
assert.Equal(t, expected, queryToMigrate)
|
||||
})
|
||||
}
|
||||
}
|
||||
func Test_Test_migrateLegacyQuery(t *testing.T) {
|
||||
t.Run("migrates alias to label when label does not already exist and feature toggle enabled", func(t *testing.T) {
|
||||
migratedQueries, err := migrateLegacyQuery(
|
||||
[]backend.DataQuery{
|
||||
{
|
||||
RefID: "A",
|
||||
QueryType: "timeSeriesQuery",
|
||||
JSON: []byte(`{
|
||||
"region": "us-east-1",
|
||||
"namespace": "ec2",
|
||||
"metricName": "CPUUtilization",
|
||||
"alias": "{{period}} {{any_other_word}}",
|
||||
"dimensions": {
|
||||
"InstanceId": ["test"]
|
||||
},
|
||||
"statistic": "Average",
|
||||
"period": "600",
|
||||
"hide": false
|
||||
}`)},
|
||||
}, true)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, len(migratedQueries))
|
||||
|
||||
assert.JSONEq(t, `{
|
||||
"alias":"{{period}} {{any_other_word}}",
|
||||
"label":"${PROP('Period')} ${PROP('Dim.any_other_word')}",
|
||||
"dimensions":{
|
||||
"InstanceId":[
|
||||
"test"
|
||||
]
|
||||
},
|
||||
"hide":false,
|
||||
"metricName":"CPUUtilization",
|
||||
"namespace":"ec2",
|
||||
"period":"600",
|
||||
"region":"us-east-1",
|
||||
"statistic":"Average"
|
||||
}`,
|
||||
string(migratedQueries[0].JSON))
|
||||
})
|
||||
|
||||
t.Run("successfully migrates alias to dynamic label for multiple queries", func(t *testing.T) {
|
||||
migratedQueries, err := migrateLegacyQuery(
|
||||
[]backend.DataQuery{
|
||||
{
|
||||
RefID: "A",
|
||||
QueryType: "timeSeriesQuery",
|
||||
JSON: []byte(`{
|
||||
"region": "us-east-1",
|
||||
"namespace": "ec2",
|
||||
"metricName": "CPUUtilization",
|
||||
"alias": "{{period}} {{any_other_word}}",
|
||||
"dimensions": {
|
||||
"InstanceId": ["test"]
|
||||
},
|
||||
"statistic": "Average",
|
||||
"period": "600",
|
||||
"hide": false
|
||||
}`),
|
||||
},
|
||||
{
|
||||
RefID: "B",
|
||||
QueryType: "timeSeriesQuery",
|
||||
JSON: []byte(`{
|
||||
"region": "us-east-1",
|
||||
"namespace": "ec2",
|
||||
"metricName": "CPUUtilization",
|
||||
"alias": "{{ label }}",
|
||||
"dimensions": {
|
||||
"InstanceId": ["test"]
|
||||
},
|
||||
"statistic": "Average",
|
||||
"period": "600",
|
||||
"hide": false
|
||||
}`),
|
||||
},
|
||||
}, true)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 2, len(migratedQueries))
|
||||
|
||||
assert.JSONEq(t,
|
||||
`{
|
||||
"alias": "{{period}} {{any_other_word}}",
|
||||
"label":"${PROP('Period')} ${PROP('Dim.any_other_word')}",
|
||||
"dimensions":{
|
||||
"InstanceId":[
|
||||
"test"
|
||||
]
|
||||
},
|
||||
"hide":false,
|
||||
"metricName":"CPUUtilization",
|
||||
"namespace":"ec2",
|
||||
"period":"600",
|
||||
"region":"us-east-1",
|
||||
"statistic":"Average"
|
||||
}`,
|
||||
string(migratedQueries[0].JSON))
|
||||
|
||||
assert.JSONEq(t,
|
||||
`{
|
||||
"alias": "{{ label }}",
|
||||
"label":"${LABEL}",
|
||||
"dimensions":{
|
||||
"InstanceId":[
|
||||
"test"
|
||||
]
|
||||
},
|
||||
"hide":false,
|
||||
"metricName":"CPUUtilization",
|
||||
"namespace":"ec2",
|
||||
"period":"600",
|
||||
"region":"us-east-1",
|
||||
"statistic":"Average"
|
||||
}`,
|
||||
string(migratedQueries[1].JSON))
|
||||
})
|
||||
|
||||
t.Run("does not migrate alias to label", func(t *testing.T) {
|
||||
testCases := map[string]struct {
|
||||
labelJson string
|
||||
dynamicLabelsFeatureToggleEnabled bool
|
||||
}{
|
||||
"when label already exists, feature toggle enabled": {labelJson: `"label":"some label",`, dynamicLabelsFeatureToggleEnabled: true},
|
||||
"when label does not exist, feature toggle is disabled": {dynamicLabelsFeatureToggleEnabled: false},
|
||||
"when label already exists, feature toggle is disabled": {labelJson: `"label":"some label",`, dynamicLabelsFeatureToggleEnabled: false},
|
||||
}
|
||||
|
||||
for name, tc := range testCases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
migratedQueries, err := migrateLegacyQuery(
|
||||
[]backend.DataQuery{
|
||||
{
|
||||
RefID: "A",
|
||||
QueryType: "timeSeriesQuery",
|
||||
JSON: []byte(fmt.Sprintf(`{
|
||||
"region": "us-east-1",
|
||||
"namespace": "ec2",
|
||||
"metricName": "CPUUtilization",
|
||||
"alias": "{{period}} {{any_other_word}}",
|
||||
%s
|
||||
"dimensions": {
|
||||
"InstanceId": ["test"]
|
||||
},
|
||||
"statistic": "Average",
|
||||
"period": "600",
|
||||
"hide": false
|
||||
}`, tc.labelJson))},
|
||||
}, tc.dynamicLabelsFeatureToggleEnabled)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, 1, len(migratedQueries))
|
||||
|
||||
assert.JSONEq(t,
|
||||
fmt.Sprintf(`{
|
||||
"alias":"{{period}} {{any_other_word}}",
|
||||
%s
|
||||
"dimensions":{
|
||||
"InstanceId":[
|
||||
"test"
|
||||
]
|
||||
},
|
||||
"hide":false,
|
||||
"metricName":"CPUUtilization",
|
||||
"namespace":"ec2",
|
||||
"period":"600",
|
||||
"region":"us-east-1",
|
||||
"statistic":"Average"
|
||||
}`, tc.labelJson),
|
||||
string(migratedQueries[0].JSON))
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package models
|
||||
|
||||
type cloudWatchLink struct {
|
||||
View string `json:"view"`
|
||||
Stacked bool `json:"stacked"`
|
||||
Title string `json:"title"`
|
||||
Start string `json:"start"`
|
||||
End string `json:"end"`
|
||||
Region string `json:"region"`
|
||||
Metrics []interface{} `json:"metrics"`
|
||||
}
|
||||
|
||||
type metricExpression struct {
|
||||
Expression string `json:"expression"`
|
||||
Label string `json:"label,omitempty"`
|
||||
}
|
||||
|
||||
type metricStatMeta struct {
|
||||
Stat string `json:"stat"`
|
||||
Period int `json:"period"`
|
||||
Label string `json:"label,omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user