CloudWatch: Remove simplejson in favor of 'encoding/json' (#51062)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package cloudwatch
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
@@ -12,32 +13,60 @@ import (
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
)
|
||||
|
||||
var validMetricDataID = regexp.MustCompile(`^[a-z][a-zA-Z0-9_]*$`)
|
||||
|
||||
type QueryJson 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:"queryType,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 (e *cloudWatchExecutor) parseQueries(queries []backend.DataQuery, startTime time.Time, endTime time.Time) (map[string][]*cloudWatchQuery, error) {
|
||||
requestQueries := make(map[string][]*cloudWatchQuery)
|
||||
|
||||
migratedQueries, err := migrateLegacyQuery(queries, e.features.IsEnabled(featuremgmt.FlagCloudWatchDynamicLabels))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, query := range migratedQueries {
|
||||
model, err := simplejson.NewJson(query.JSON)
|
||||
var model QueryJson
|
||||
err := json.Unmarshal(query.JSON, &model)
|
||||
if err != nil {
|
||||
return nil, &queryError{err: err, RefID: query.RefID}
|
||||
}
|
||||
|
||||
queryType := model.Get("type").MustString()
|
||||
queryType := model.QueryType
|
||||
if queryType != "timeSeriesQuery" && queryType != "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if model.MatchExact == nil {
|
||||
trueBooleanValue := true
|
||||
model.MatchExact = &trueBooleanValue
|
||||
}
|
||||
|
||||
refID := query.RefID
|
||||
query, err := parseRequestQuery(model, refID, startTime, endTime)
|
||||
if err != nil {
|
||||
@@ -58,7 +87,8 @@ func migrateLegacyQuery(queries []backend.DataQuery, dynamicLabelsEnabled bool)
|
||||
migratedQueries := []*backend.DataQuery{}
|
||||
for _, q := range queries {
|
||||
query := q
|
||||
queryJson, err := simplejson.NewJson(query.JSON)
|
||||
var queryJson *QueryJson
|
||||
err := json.Unmarshal(query.JSON, &queryJson)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -67,12 +97,10 @@ func migrateLegacyQuery(queries []backend.DataQuery, dynamicLabelsEnabled bool)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, labelExists := queryJson.CheckGet("label")
|
||||
if !labelExists && dynamicLabelsEnabled {
|
||||
if queryJson.Label == nil && dynamicLabelsEnabled {
|
||||
migrateAliasToDynamicLabel(queryJson)
|
||||
}
|
||||
|
||||
query.JSON, err = queryJson.MarshalJSON()
|
||||
query.JSON, err = json.Marshal(queryJson)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -86,16 +114,15 @@ func migrateLegacyQuery(queries []backend.DataQuery, dynamicLabelsEnabled bool)
|
||||
// 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 *simplejson.Json) error {
|
||||
_, err := queryJson.Get("statistic").String()
|
||||
func migrateStatisticsToStatistic(queryJson *QueryJson) 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 err != nil {
|
||||
stats, err := queryJson.Get("statistics").StringArray()
|
||||
if err != nil {
|
||||
if queryJson.Statistic == nil {
|
||||
if queryJson.Statistics == nil {
|
||||
return fmt.Errorf("query must have either statistic or statistics field")
|
||||
}
|
||||
queryJson.Del("statistics")
|
||||
queryJson.Set("statistic", stats[0])
|
||||
|
||||
queryJson.Statistic = queryJson.Statistics[0]
|
||||
queryJson.Statistics = nil
|
||||
}
|
||||
|
||||
return nil
|
||||
@@ -112,10 +139,13 @@ var aliasPatterns = map[string]string{
|
||||
|
||||
var legacyAliasRegexp = regexp.MustCompile(`{{\s*(.+?)\s*}}`)
|
||||
|
||||
func migrateAliasToDynamicLabel(queryJson *simplejson.Json) {
|
||||
fullAliasField := queryJson.Get("alias").MustString()
|
||||
if fullAliasField != "" {
|
||||
matches := legacyAliasRegexp.FindAllStringSubmatch(fullAliasField, -1)
|
||||
func migrateAliasToDynamicLabel(queryJson *QueryJson) {
|
||||
fullAliasField := ""
|
||||
|
||||
if queryJson.Alias != nil && *queryJson.Alias != "" {
|
||||
matches := legacyAliasRegexp.FindAllStringSubmatch(*queryJson.Alias, -1)
|
||||
fullAliasField = *queryJson.Alias
|
||||
|
||||
for _, groups := range matches {
|
||||
fullMatch := groups[0]
|
||||
subgroup := groups[1]
|
||||
@@ -126,36 +156,36 @@ func migrateAliasToDynamicLabel(queryJson *simplejson.Json) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
queryJson.Set("label", fullAliasField)
|
||||
queryJson.Label = &fullAliasField
|
||||
}
|
||||
|
||||
func parseRequestQuery(model *simplejson.Json, refId string, startTime time.Time, endTime time.Time) (*cloudWatchQuery, error) {
|
||||
func parseRequestQuery(model QueryJson, refId string, startTime time.Time, endTime time.Time) (*cloudWatchQuery, error) {
|
||||
plog.Debug("Parsing request query", "query", model)
|
||||
cloudWatchQuery := cloudWatchQuery{
|
||||
Alias: "",
|
||||
Label: "",
|
||||
MatchExact: true,
|
||||
Statistic: "",
|
||||
ReturnData: false,
|
||||
UsedExpression: "",
|
||||
RefId: refId,
|
||||
Id: model.Id,
|
||||
Region: model.Region,
|
||||
Namespace: model.Namespace,
|
||||
MetricName: model.MetricName,
|
||||
MetricQueryType: model.MetricQueryType,
|
||||
SqlExpression: model.SqlExpression,
|
||||
TimezoneUTCOffset: model.TimezoneUTCOffset,
|
||||
Expression: model.Expression,
|
||||
}
|
||||
reNumber := regexp.MustCompile(`^\d+$`)
|
||||
region, err := model.Get("region").String()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
namespace, err := model.Get("namespace").String()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get namespace: %v", err)
|
||||
}
|
||||
metricName, err := model.Get("metricName").String()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to get metricName: %v", err)
|
||||
}
|
||||
dimensions, err := parseDimensions(model)
|
||||
dimensions, err := parseDimensions(model.Dimensions)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse dimensions: %v", err)
|
||||
}
|
||||
cloudWatchQuery.Dimensions = dimensions
|
||||
|
||||
statistic, err := model.Get("statistic").String()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse statistic: %v", err)
|
||||
}
|
||||
|
||||
p := model.Get("period").MustString("")
|
||||
p := model.Period
|
||||
var period int
|
||||
if strings.ToLower(p) == "auto" || p == "" {
|
||||
deltaInSeconds := endTime.Sub(startTime).Seconds()
|
||||
@@ -182,9 +212,9 @@ func parseRequestQuery(model *simplejson.Json, refId string, startTime time.Time
|
||||
period = int(d.Seconds())
|
||||
}
|
||||
}
|
||||
cloudWatchQuery.Period = period
|
||||
|
||||
id := model.Get("id").MustString("")
|
||||
if id == "" {
|
||||
if model.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.
|
||||
@@ -193,55 +223,48 @@ func parseRequestQuery(model *simplejson.Json, refId string, startTime time.Time
|
||||
uuid := uuid.NewString()
|
||||
suffix = strings.Replace(uuid, "-", "", -1)
|
||||
}
|
||||
id = fmt.Sprintf("query%s", suffix)
|
||||
cloudWatchQuery.Id = fmt.Sprintf("query%s", suffix)
|
||||
}
|
||||
expression := model.Get("expression").MustString("")
|
||||
sqlExpression := model.Get("sqlExpression").MustString("")
|
||||
alias := model.Get("alias").MustString()
|
||||
label := model.Get("label").MustString()
|
||||
returnData := !model.Get("hide").MustBool(false)
|
||||
queryType := model.Get("type").MustString()
|
||||
timezoneUTCOffset := model.Get("timezoneUTCOffset").MustString("")
|
||||
|
||||
if queryType == "" {
|
||||
if model.Hide != nil {
|
||||
cloudWatchQuery.ReturnData = !*model.Hide
|
||||
}
|
||||
|
||||
if model.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.
|
||||
returnData = true
|
||||
cloudWatchQuery.ReturnData = true
|
||||
}
|
||||
|
||||
matchExact := model.Get("matchExact").MustBool(true)
|
||||
metricQueryType := metricQueryType(model.Get("metricQueryType").MustInt(0))
|
||||
|
||||
var metricEditorModeValue metricEditorMode
|
||||
memv, err := model.Get("metricEditorMode").Int()
|
||||
if err != nil && len(expression) > 0 {
|
||||
if model.MetricEditorMode == nil && len(model.Expression) > 0 {
|
||||
// this should only ever happen if this is an alerting query that has not yet been migrated in the frontend
|
||||
metricEditorModeValue = MetricEditorModeRaw
|
||||
cloudWatchQuery.MetricEditorMode = MetricEditorModeRaw
|
||||
} else {
|
||||
metricEditorModeValue = metricEditorMode(memv)
|
||||
if model.MetricEditorMode != nil {
|
||||
cloudWatchQuery.MetricEditorMode = metricEditorMode(*model.MetricEditorMode)
|
||||
} else {
|
||||
cloudWatchQuery.MetricEditorMode = metricEditorMode(0)
|
||||
}
|
||||
}
|
||||
|
||||
return &cloudWatchQuery{
|
||||
RefId: refId,
|
||||
Region: region,
|
||||
Id: id,
|
||||
Namespace: namespace,
|
||||
MetricName: metricName,
|
||||
Statistic: statistic,
|
||||
Expression: expression,
|
||||
ReturnData: returnData,
|
||||
Dimensions: dimensions,
|
||||
Period: period,
|
||||
Alias: alias,
|
||||
Label: label,
|
||||
MatchExact: matchExact,
|
||||
UsedExpression: "",
|
||||
MetricQueryType: metricQueryType,
|
||||
MetricEditorMode: metricEditorModeValue,
|
||||
SqlExpression: sqlExpression,
|
||||
TimezoneUTCOffset: timezoneUTCOffset,
|
||||
}, nil
|
||||
if model.Statistic != nil {
|
||||
cloudWatchQuery.Statistic = *model.Statistic
|
||||
}
|
||||
|
||||
if model.MatchExact != nil {
|
||||
cloudWatchQuery.MatchExact = *model.MatchExact
|
||||
}
|
||||
|
||||
if model.Alias != nil {
|
||||
cloudWatchQuery.Alias = *model.Alias
|
||||
}
|
||||
|
||||
if model.Label != nil {
|
||||
cloudWatchQuery.Label = *model.Label
|
||||
}
|
||||
|
||||
return &cloudWatchQuery, nil
|
||||
}
|
||||
|
||||
func getRetainedPeriods(timeSince time.Duration) []int {
|
||||
@@ -257,9 +280,9 @@ func getRetainedPeriods(timeSince time.Duration) []int {
|
||||
}
|
||||
}
|
||||
|
||||
func parseDimensions(model *simplejson.Json) (map[string][]string, error) {
|
||||
func parseDimensions(dimensions map[string]interface{}) (map[string][]string, error) {
|
||||
parsedDimensions := make(map[string][]string)
|
||||
for k, v := range model.Get("dimensions").MustMap() {
|
||||
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}
|
||||
|
||||
Reference in New Issue
Block a user