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"
|
|
|
|
"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) {
|
2020-10-06 06:45:58 -05:00
|
|
|
plog.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")
|
|
|
|
}
|
2021-03-23 10:32:12 -05:00
|
|
|
|
2021-06-10 03:23:17 -05:00
|
|
|
// 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
|
|
|
|
2021-06-10 03:23:17 -05:00
|
|
|
requestQueriesByRegion, err := e.parseQueries(req.Queries, startTime, endTime)
|
|
|
|
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 {
|
|
|
|
plog.Error("Execute Get Metric Data Query Panic", "error", err, "stack", log.Stack(1))
|
|
|
|
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
|
|
|
}()
|
|
|
|
|
|
|
|
client, err := e.getCWClient(region, req.PluginContext)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
queries, err := e.transformRequestQueriesToCloudWatchQueries(requestQueries)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
metricDataInput, err := e.buildMetricDataInput(startTime, endTime, queries)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mdo, err := e.executeRequest(ectx, client, metricDataInput)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
responses, err := e.parseResponse(mdo, queries)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := e.transformQueryResponsesToQueryResult(responses, requestQueries, startTime, endTime)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for refID, queryRes := range res {
|
|
|
|
resultChan <- &responseWrapper{
|
|
|
|
DataResponse: queryRes,
|
|
|
|
RefId: refID,
|
2019-11-14 03:59:41 -06:00
|
|
|
}
|
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 {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|