2019-11-14 03:59:41 -06:00
|
|
|
package cloudwatch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-01-17 06:22:43 -06:00
|
|
|
"fmt"
|
2019-11-14 03:59:41 -06:00
|
|
|
|
2021-03-23 10:32:12 -05:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
2019-11-14 03:59:41 -06:00
|
|
|
"github.com/grafana/grafana/pkg/infra/log"
|
2022-10-18 03:21:18 -05:00
|
|
|
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
|
|
|
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/cwlog"
|
2019-11-14 03:59:41 -06:00
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
)
|
|
|
|
|
2021-06-10 03:23:17 -05:00
|
|
|
type responseWrapper struct {
|
|
|
|
DataResponse *backend.DataResponse
|
|
|
|
RefId string
|
|
|
|
}
|
|
|
|
|
2021-03-23 10:32:12 -05:00
|
|
|
func (e *cloudWatchExecutor) executeTimeSeriesQuery(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) {
|
2022-10-18 03:21:18 -05:00
|
|
|
cwlog.Debug("Executing time series query")
|
2021-03-23 10:32:12 -05:00
|
|
|
resp := backend.NewQueryDataResponse()
|
2020-05-18 05:25:58 -05:00
|
|
|
|
2021-06-10 03:23:17 -05:00
|
|
|
if len(req.Queries) == 0 {
|
|
|
|
return nil, fmt.Errorf("request contains no queries")
|
|
|
|
}
|
|
|
|
// startTime and endTime are always the same for all queries
|
|
|
|
startTime := req.Queries[0].TimeRange.From
|
|
|
|
endTime := req.Queries[0].TimeRange.To
|
|
|
|
if !startTime.Before(endTime) {
|
|
|
|
return nil, fmt.Errorf("invalid time range: start time must be before end time")
|
|
|
|
}
|
2021-03-23 10:32:12 -05:00
|
|
|
|
2022-10-18 03:21:18 -05:00
|
|
|
requestQueriesByRegion, err := parseQueries(req.Queries, startTime, endTime, e.features.IsEnabled(featuremgmt.FlagCloudWatchDynamicLabels))
|
2021-06-10 03:23:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-23 10:32:12 -05:00
|
|
|
|
2021-06-10 03:23:17 -05:00
|
|
|
if len(requestQueriesByRegion) == 0 {
|
|
|
|
return backend.NewQueryDataResponse(), nil
|
|
|
|
}
|
2021-03-23 10:32:12 -05:00
|
|
|
|
2021-06-10 03:23:17 -05:00
|
|
|
resultChan := make(chan *responseWrapper, len(req.Queries))
|
|
|
|
eg, ectx := errgroup.WithContext(ctx)
|
|
|
|
for r, q := range requestQueriesByRegion {
|
|
|
|
requestQueries := q
|
|
|
|
region := r
|
|
|
|
eg.Go(func() error {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2022-10-18 03:21:18 -05:00
|
|
|
cwlog.Error("Execute Get Metric Data Query Panic", "error", err, "stack", log.Stack(1))
|
2021-06-10 03:23:17 -05:00
|
|
|
if theErr, ok := err.(error); ok {
|
|
|
|
resultChan <- &responseWrapper{
|
|
|
|
DataResponse: &backend.DataResponse{
|
|
|
|
Error: theErr,
|
|
|
|
},
|
2021-03-23 10:32:12 -05:00
|
|
|
}
|
2019-11-14 03:59:41 -06:00
|
|
|
}
|
|
|
|
}
|
2021-06-10 03:23:17 -05:00
|
|
|
}()
|
|
|
|
|
2022-02-10 10:15:11 -06:00
|
|
|
client, err := e.getCWClient(req.PluginContext, region)
|
2021-06-10 03:23:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-08 09:06:43 -05:00
|
|
|
metricDataInput, err := e.buildMetricDataInput(startTime, endTime, requestQueries)
|
2021-06-10 03:23:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mdo, err := e.executeRequest(ectx, client, metricDataInput)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-08 09:06:43 -05:00
|
|
|
res, err := e.parseResponse(startTime, endTime, mdo, requestQueries)
|
2021-06-10 03:23:17 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-09-08 09:06:43 -05:00
|
|
|
for _, responseWrapper := range res {
|
|
|
|
resultChan <- responseWrapper
|
2021-06-10 03:23:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
2021-03-23 10:32:12 -05:00
|
|
|
|
2021-06-10 03:23:17 -05:00
|
|
|
if err := eg.Wait(); err != nil {
|
2021-08-18 02:37:13 -05:00
|
|
|
dataResponse := backend.DataResponse{
|
|
|
|
Error: fmt.Errorf("metric request error: %q", err),
|
|
|
|
}
|
|
|
|
resultChan <- &responseWrapper{
|
|
|
|
DataResponse: &dataResponse,
|
|
|
|
}
|
2021-06-10 03:23:17 -05:00
|
|
|
}
|
|
|
|
close(resultChan)
|
2021-03-23 10:32:12 -05:00
|
|
|
|
2021-06-10 03:23:17 -05:00
|
|
|
for result := range resultChan {
|
|
|
|
resp.Responses[result.RefId] = *result.DataResponse
|
2019-11-14 03:59:41 -06:00
|
|
|
}
|
2021-03-23 10:32:12 -05:00
|
|
|
|
|
|
|
return resp, nil
|
2019-11-14 03:59:41 -06:00
|
|
|
}
|