CloudWatch: Remove simplejson in favor of 'encoding/json' (#51062)
This commit is contained in:
@@ -25,7 +25,6 @@ import (
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/backend/resource/httpadapter"
|
||||
"github.com/grafana/grafana-plugin-sdk-go/data"
|
||||
"github.com/grafana/grafana/pkg/components/simplejson"
|
||||
"github.com/grafana/grafana/pkg/infra/httpclient"
|
||||
"github.com/grafana/grafana/pkg/infra/log"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
@@ -49,6 +48,20 @@ type datasourceInfo struct {
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
type DataQueryJson struct {
|
||||
QueryType string `json:"type,omitempty"`
|
||||
QueryMode string
|
||||
PrefixMatching bool
|
||||
Region string
|
||||
Namespace string
|
||||
MetricName string
|
||||
Dimensions map[string]interface{}
|
||||
Statistic *string
|
||||
Period string
|
||||
ActionPrefix string
|
||||
AlarmNamePrefix string
|
||||
}
|
||||
|
||||
const (
|
||||
cloudWatchTSFormat = "2006-01-02 15:04:05.000"
|
||||
defaultRegion = "default"
|
||||
@@ -59,10 +72,12 @@ const (
|
||||
|
||||
alertMaxAttempts = 8
|
||||
alertPollPeriod = 1000 * time.Millisecond
|
||||
logsQueryMode = "Logs"
|
||||
)
|
||||
|
||||
var plog = log.New("tsdb.cloudwatch")
|
||||
var aliasFormat = regexp.MustCompile(`\{\{\s*(.+?)\s*\}\}`)
|
||||
var baseLimit = int64(1)
|
||||
|
||||
func ProvideService(cfg *setting.Cfg, httpClientProvider httpclient.Provider, features featuremgmt.FeatureToggles) *CloudWatchService {
|
||||
plog.Debug("initing")
|
||||
@@ -188,7 +203,12 @@ func (e *cloudWatchExecutor) checkHealthLogs(ctx context.Context, pluginCtx back
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = e.handleDescribeLogGroups(ctx, logsClient, simplejson.NewFromAny(map[string]interface{}{"limit": "1"}))
|
||||
|
||||
parameters := LogQueryJson{
|
||||
Limit: &baseLimit,
|
||||
}
|
||||
|
||||
_, err = e.handleDescribeLogGroups(ctx, logsClient, parameters)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -282,16 +302,16 @@ func (e *cloudWatchExecutor) getRGTAClient(pluginCtx backend.PluginContext, regi
|
||||
}
|
||||
|
||||
func (e *cloudWatchExecutor) alertQuery(ctx context.Context, logsClient cloudwatchlogsiface.CloudWatchLogsAPI,
|
||||
queryContext backend.DataQuery, model *simplejson.Json) (*cloudwatchlogs.GetQueryResultsOutput, error) {
|
||||
queryContext backend.DataQuery, model LogQueryJson) (*cloudwatchlogs.GetQueryResultsOutput, error) {
|
||||
startQueryOutput, err := e.executeStartQuery(ctx, logsClient, model, queryContext.TimeRange)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
requestParams := simplejson.NewFromAny(map[string]interface{}{
|
||||
"region": model.Get("region").MustString(""),
|
||||
"queryId": *startQueryOutput.QueryId,
|
||||
})
|
||||
requestParams := LogQueryJson{
|
||||
Region: model.Region,
|
||||
QueryId: *startQueryOutput.QueryId,
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(alertPollPeriod)
|
||||
defer ticker.Stop()
|
||||
@@ -324,18 +344,19 @@ func (e *cloudWatchExecutor) QueryData(ctx context.Context, req *backend.QueryDa
|
||||
frontend, but because alerts are executed on the backend the logic needs to be reimplemented here.
|
||||
*/
|
||||
q := req.Queries[0]
|
||||
model, err := simplejson.NewJson(q.JSON)
|
||||
var model DataQueryJson
|
||||
err := json.Unmarshal(q.JSON, &model)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, fromAlert := req.Headers["FromAlert"]
|
||||
isLogAlertQuery := fromAlert && model.Get("queryMode").MustString("") == "Logs"
|
||||
isLogAlertQuery := fromAlert && model.QueryMode == logsQueryMode
|
||||
|
||||
if isLogAlertQuery {
|
||||
return e.executeLogAlertQuery(ctx, req)
|
||||
}
|
||||
|
||||
queryType := model.Get("type").MustString("")
|
||||
queryType := model.QueryType
|
||||
|
||||
var result *backend.QueryDataResponse
|
||||
switch queryType {
|
||||
@@ -356,21 +377,22 @@ func (e *cloudWatchExecutor) executeLogAlertQuery(ctx context.Context, req *back
|
||||
resp := backend.NewQueryDataResponse()
|
||||
|
||||
for _, q := range req.Queries {
|
||||
model, err := simplejson.NewJson(q.JSON)
|
||||
var model LogQueryJson
|
||||
err := json.Unmarshal(q.JSON, &model)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
model.Set("subtype", "StartQuery")
|
||||
model.Set("queryString", model.Get("expression").MustString(""))
|
||||
model.Subtype = "StartQuery"
|
||||
model.QueryString = model.Expression
|
||||
|
||||
region := model.Get("region").MustString(defaultRegion)
|
||||
if region == defaultRegion {
|
||||
region := model.Region
|
||||
if model.Region == "" || region == defaultRegion {
|
||||
dsInfo, err := e.getDSInfo(req.PluginContext)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
model.Set("region", dsInfo.region)
|
||||
model.Region = dsInfo.region
|
||||
}
|
||||
|
||||
logsClient, err := e.getCWLogsClient(req.PluginContext, region)
|
||||
@@ -389,10 +411,8 @@ func (e *cloudWatchExecutor) executeLogAlertQuery(ctx context.Context, req *back
|
||||
}
|
||||
|
||||
var frames []*data.Frame
|
||||
|
||||
statsGroups := model.Get("statsGroups").MustStringArray()
|
||||
if len(statsGroups) > 0 && len(dataframe.Fields) > 0 {
|
||||
frames, err = groupResults(dataframe, statsGroups)
|
||||
if len(model.StatsGroups) > 0 && len(dataframe.Fields) > 0 {
|
||||
frames, err = groupResults(dataframe, model.StatsGroups)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user