Commit 77628398 by Arve Knudsen Committed by GitHub

Convert CloudWatch to use dataframes (#26702)

* CloudWatch: Convert metrics queries to return data frames

Signed-off-by: Arve Knudsen <arve.knudsen@gmail.com>
parent dfc78d09
...@@ -5,10 +5,12 @@ import ( ...@@ -5,10 +5,12 @@ import (
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch" "github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
"github.com/grafana/grafana/pkg/infra/metrics" "github.com/grafana/grafana/pkg/infra/metrics"
) )
func (e *cloudWatchExecutor) executeRequest(ctx context.Context, client cloudWatchClient, metricDataInput *cloudwatch.GetMetricDataInput) ([]*cloudwatch.GetMetricDataOutput, error) { func (e *cloudWatchExecutor) executeRequest(ctx context.Context, client cloudwatchiface.CloudWatchAPI,
metricDataInput *cloudwatch.GetMetricDataInput) ([]*cloudwatch.GetMetricDataOutput, error) {
mdo := make([]*cloudwatch.GetMetricDataOutput, 0) mdo := make([]*cloudwatch.GetMetricDataOutput, 0)
nextToken := "" nextToken := ""
......
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request" "github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/cloudwatch" "github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
...@@ -14,6 +15,7 @@ import ( ...@@ -14,6 +15,7 @@ import (
var counter = 1 var counter = 1
type cloudWatchFakeClient struct { type cloudWatchFakeClient struct {
cloudwatchiface.CloudWatchAPI
} }
func (client *cloudWatchFakeClient) GetMetricDataWithContext(ctx aws.Context, input *cloudwatch.GetMetricDataInput, opts ...request.Option) (*cloudwatch.GetMetricDataOutput, error) { func (client *cloudWatchFakeClient) GetMetricDataWithContext(ctx aws.Context, input *cloudwatch.GetMetricDataInput, opts ...request.Option) (*cloudwatch.GetMetricDataOutput, error) {
......
...@@ -17,8 +17,8 @@ func logsResultsToDataframes(response *cloudwatchlogs.GetQueryResultsOutput) (*d ...@@ -17,8 +17,8 @@ func logsResultsToDataframes(response *cloudwatchlogs.GetQueryResultsOutput) (*d
} }
nonEmptyRows := make([][]*cloudwatchlogs.ResultField, 0) nonEmptyRows := make([][]*cloudwatchlogs.ResultField, 0)
// Sometimes CloudWatch can send empty rows
for _, row := range response.Results { for _, row := range response.Results {
// Sometimes CloudWatch can send empty rows
if len(row) == 0 { if len(row) == 0 {
continue continue
} }
...@@ -26,7 +26,7 @@ func logsResultsToDataframes(response *cloudwatchlogs.GetQueryResultsOutput) (*d ...@@ -26,7 +26,7 @@ func logsResultsToDataframes(response *cloudwatchlogs.GetQueryResultsOutput) (*d
if row[0].Value == nil { if row[0].Value == nil {
continue continue
} }
// Sometimes it sends row with only timestamp // Sometimes it sends rows with only timestamp
if _, err := time.Parse(cloudWatchTSFormat, *row[0].Value); err == nil { if _, err := time.Parse(cloudWatchTSFormat, *row[0].Value); err == nil {
continue continue
} }
...@@ -52,7 +52,7 @@ func logsResultsToDataframes(response *cloudwatchlogs.GetQueryResultsOutput) (*d ...@@ -52,7 +52,7 @@ func logsResultsToDataframes(response *cloudwatchlogs.GetQueryResultsOutput) (*d
if _, exists := fieldValues[*resultField.Field]; !exists { if _, exists := fieldValues[*resultField.Field]; !exists {
fieldNames = append(fieldNames, *resultField.Field) fieldNames = append(fieldNames, *resultField.Field)
// Check if field is time field // Check if it's a time field
if _, err := time.Parse(cloudWatchTSFormat, *resultField.Value); err == nil { if _, err := time.Parse(cloudWatchTSFormat, *resultField.Value); err == nil {
fieldValues[*resultField.Field] = make([]*time.Time, rowCount) fieldValues[*resultField.Field] = make([]*time.Time, rowCount)
} else if _, err := strconv.ParseFloat(*resultField.Value, 64); err == nil { } else if _, err := strconv.ParseFloat(*resultField.Value, 64); err == nil {
...@@ -81,7 +81,7 @@ func logsResultsToDataframes(response *cloudwatchlogs.GetQueryResultsOutput) (*d ...@@ -81,7 +81,7 @@ func logsResultsToDataframes(response *cloudwatchlogs.GetQueryResultsOutput) (*d
} }
} }
newFields := make([]*data.Field, 0) newFields := make([]*data.Field, 0, len(fieldNames))
for _, fieldName := range fieldNames { for _, fieldName := range fieldNames {
newFields = append(newFields, data.NewField(fieldName, nil, fieldValues[fieldName])) newFields = append(newFields, data.NewField(fieldName, nil, fieldValues[fieldName]))
......
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"sort" "sort"
"strings" "strings"
"github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana/pkg/components/simplejson" "github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/tsdb" "github.com/grafana/grafana/pkg/tsdb"
) )
...@@ -13,7 +14,9 @@ import ( ...@@ -13,7 +14,9 @@ import (
// has more than one statistic defined, one cloudwatchQuery will be created for each statistic. // has more than one statistic defined, one cloudwatchQuery will be created for each statistic.
// If the query doesn't have an Id defined by the user, we'll give it an with format `query[RefId]`. In the case // If the query doesn't have an Id defined by the user, we'll give it an with format `query[RefId]`. In the case
// the incoming query had more than one stat, it will ge an id like `query[RefId]_[StatName]`, eg queryC_Average // the incoming query had more than one stat, it will ge an id like `query[RefId]_[StatName]`, eg queryC_Average
func (e *cloudWatchExecutor) transformRequestQueriesToCloudWatchQueries(requestQueries []*requestQuery) (map[string]*cloudWatchQuery, error) { func (e *cloudWatchExecutor) transformRequestQueriesToCloudWatchQueries(requestQueries []*requestQuery) (
map[string]*cloudWatchQuery, error) {
plog.Debug("Transforming CloudWatch request queries")
cloudwatchQueries := make(map[string]*cloudWatchQuery) cloudwatchQueries := make(map[string]*cloudWatchQuery)
for _, requestQuery := range requestQueries { for _, requestQuery := range requestQueries {
for _, stat := range requestQuery.Statistics { for _, stat := range requestQuery.Statistics {
...@@ -52,17 +55,22 @@ func (e *cloudWatchExecutor) transformRequestQueriesToCloudWatchQueries(requestQ ...@@ -52,17 +55,22 @@ func (e *cloudWatchExecutor) transformRequestQueriesToCloudWatchQueries(requestQ
func (e *cloudWatchExecutor) transformQueryResponseToQueryResult(cloudwatchResponses []*cloudwatchResponse) map[string]*tsdb.QueryResult { func (e *cloudWatchExecutor) transformQueryResponseToQueryResult(cloudwatchResponses []*cloudwatchResponse) map[string]*tsdb.QueryResult {
responsesByRefID := make(map[string][]*cloudwatchResponse) responsesByRefID := make(map[string][]*cloudwatchResponse)
refIDs := sort.StringSlice{}
for _, res := range cloudwatchResponses { for _, res := range cloudwatchResponses {
refIDs = append(refIDs, res.RefId)
responsesByRefID[res.RefId] = append(responsesByRefID[res.RefId], res) responsesByRefID[res.RefId] = append(responsesByRefID[res.RefId], res)
} }
// Ensure stable results
refIDs.Sort()
results := make(map[string]*tsdb.QueryResult) results := make(map[string]*tsdb.QueryResult)
for refID, responses := range responsesByRefID { for _, refID := range refIDs {
responses := responsesByRefID[refID]
queryResult := tsdb.NewQueryResult() queryResult := tsdb.NewQueryResult()
queryResult.RefId = refID queryResult.RefId = refID
queryResult.Meta = simplejson.New() queryResult.Meta = simplejson.New()
queryResult.Series = tsdb.TimeSeriesSlice{} queryResult.Series = tsdb.TimeSeriesSlice{}
timeSeries := make(tsdb.TimeSeriesSlice, 0) frames := make(data.Frames, 0, len(responses))
requestExceededMaxLimit := false requestExceededMaxLimit := false
partialData := false partialData := false
...@@ -72,7 +80,7 @@ func (e *cloudWatchExecutor) transformQueryResponseToQueryResult(cloudwatchRespo ...@@ -72,7 +80,7 @@ func (e *cloudWatchExecutor) transformQueryResponseToQueryResult(cloudwatchRespo
}{} }{}
for _, response := range responses { for _, response := range responses {
timeSeries = append(timeSeries, *response.series...) frames = append(frames, response.DataFrames...)
requestExceededMaxLimit = requestExceededMaxLimit || response.RequestExceededMaxLimit requestExceededMaxLimit = requestExceededMaxLimit || response.RequestExceededMaxLimit
partialData = partialData || response.PartialData partialData = partialData || response.PartialData
queryMeta = append(queryMeta, struct { queryMeta = append(queryMeta, struct {
...@@ -85,8 +93,8 @@ func (e *cloudWatchExecutor) transformQueryResponseToQueryResult(cloudwatchRespo ...@@ -85,8 +93,8 @@ func (e *cloudWatchExecutor) transformQueryResponseToQueryResult(cloudwatchRespo
}) })
} }
sort.Slice(timeSeries, func(i, j int) bool { sort.Slice(frames, func(i, j int) bool {
return timeSeries[i].Name < timeSeries[j].Name return frames[i].Name < frames[j].Name
}) })
if requestExceededMaxLimit { if requestExceededMaxLimit {
...@@ -96,7 +104,7 @@ func (e *cloudWatchExecutor) transformQueryResponseToQueryResult(cloudwatchRespo ...@@ -96,7 +104,7 @@ func (e *cloudWatchExecutor) transformQueryResponseToQueryResult(cloudwatchRespo
queryResult.ErrorString = "Cloudwatch GetMetricData error: Too many datapoints requested - your search has been limited. Please try to reduce the time range" queryResult.ErrorString = "Cloudwatch GetMetricData error: Too many datapoints requested - your search has been limited. Please try to reduce the time range"
} }
queryResult.Series = append(queryResult.Series, timeSeries...) queryResult.Dataframes = tsdb.NewDecodedDataFrames(frames)
queryResult.Meta.Set("gmdMeta", queryMeta) queryResult.Meta.Set("gmdMeta", queryMeta)
results[refID] = queryResult results[refID] = queryResult
} }
......
...@@ -17,13 +17,13 @@ import ( ...@@ -17,13 +17,13 @@ import (
// Parses the json queries and returns a requestQuery. The requestQuery has a 1 to 1 mapping to a query editor row // Parses the json queries and returns a requestQuery. The requestQuery has a 1 to 1 mapping to a query editor row
func (e *cloudWatchExecutor) parseQueries(queryContext *tsdb.TsdbQuery, startTime time.Time, endTime time.Time) (map[string][]*requestQuery, error) { func (e *cloudWatchExecutor) parseQueries(queryContext *tsdb.TsdbQuery, startTime time.Time, endTime time.Time) (map[string][]*requestQuery, error) {
requestQueries := make(map[string][]*requestQuery) requestQueries := make(map[string][]*requestQuery)
for i, model := range queryContext.Queries { for i, query := range queryContext.Queries {
queryType := model.Model.Get("type").MustString() queryType := query.Model.Get("type").MustString()
if queryType != "timeSeriesQuery" && queryType != "" { if queryType != "timeSeriesQuery" && queryType != "" {
continue continue
} }
refID := queryContext.Queries[i].RefId refID := query.RefId
query, err := parseRequestQuery(queryContext.Queries[i].Model, refID, startTime, endTime) query, err := parseRequestQuery(queryContext.Queries[i].Model, refID, startTime, endTime)
if err != nil { if err != nil {
return nil, &queryError{err: err, RefID: refID} return nil, &queryError{err: err, RefID: refID}
...@@ -39,6 +39,7 @@ func (e *cloudWatchExecutor) parseQueries(queryContext *tsdb.TsdbQuery, startTim ...@@ -39,6 +39,7 @@ func (e *cloudWatchExecutor) parseQueries(queryContext *tsdb.TsdbQuery, startTim
} }
func parseRequestQuery(model *simplejson.Json, refId string, startTime time.Time, endTime time.Time) (*requestQuery, error) { func parseRequestQuery(model *simplejson.Json, refId string, startTime time.Time, endTime time.Time) (*requestQuery, error) {
plog.Debug("Parsing request query", "query", model)
reNumber := regexp.MustCompile(`^\d+$`) reNumber := regexp.MustCompile(`^\d+$`)
region, err := model.Get("region").String() region, err := model.Get("region").String()
if err != nil { if err != nil {
......
...@@ -8,11 +8,12 @@ import ( ...@@ -8,11 +8,12 @@ import (
"time" "time"
"github.com/aws/aws-sdk-go/service/cloudwatch" "github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/grafana/grafana/pkg/components/null" "github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/grafana/grafana/pkg/tsdb"
) )
func (e *cloudWatchExecutor) parseResponse(metricDataOutputs []*cloudwatch.GetMetricDataOutput, queries map[string]*cloudWatchQuery) ([]*cloudwatchResponse, error) { func (e *cloudWatchExecutor) parseResponse(metricDataOutputs []*cloudwatch.GetMetricDataOutput,
queries map[string]*cloudWatchQuery) ([]*cloudwatchResponse, error) {
plog.Debug("Parsing metric data output", "queries", queries)
// Map from result ID -> label -> result // Map from result ID -> label -> result
mdrs := make(map[string]map[string]*cloudwatch.MetricDataResult) mdrs := make(map[string]map[string]*cloudwatch.MetricDataResult)
labels := map[string][]string{} labels := map[string][]string{}
...@@ -48,14 +49,15 @@ func (e *cloudWatchExecutor) parseResponse(metricDataOutputs []*cloudwatch.GetMe ...@@ -48,14 +49,15 @@ func (e *cloudWatchExecutor) parseResponse(metricDataOutputs []*cloudwatch.GetMe
cloudWatchResponses := make([]*cloudwatchResponse, 0) cloudWatchResponses := make([]*cloudwatchResponse, 0)
for id, lr := range mdrs { for id, lr := range mdrs {
plog.Debug("Handling metric data results", "id", id, "lr", lr)
query := queries[id] query := queries[id]
series, partialData, err := parseGetMetricDataTimeSeries(lr, labels[id], query) frames, partialData, err := parseGetMetricDataTimeSeries(lr, labels[id], query)
if err != nil { if err != nil {
return nil, err return nil, err
} }
response := &cloudwatchResponse{ response := &cloudwatchResponse{
series: series, DataFrames: frames,
Period: query.Period, Period: query.Period,
Expression: query.UsedExpression, Expression: query.UsedExpression,
RefId: query.RefId, RefId: query.RefId,
...@@ -70,12 +72,15 @@ func (e *cloudWatchExecutor) parseResponse(metricDataOutputs []*cloudwatch.GetMe ...@@ -70,12 +72,15 @@ func (e *cloudWatchExecutor) parseResponse(metricDataOutputs []*cloudwatch.GetMe
} }
func parseGetMetricDataTimeSeries(metricDataResults map[string]*cloudwatch.MetricDataResult, labels []string, func parseGetMetricDataTimeSeries(metricDataResults map[string]*cloudwatch.MetricDataResult, labels []string,
query *cloudWatchQuery) (*tsdb.TimeSeriesSlice, bool, error) { query *cloudWatchQuery) (data.Frames, bool, error) {
plog.Debug("Parsing metric data results", "results", metricDataResults)
partialData := false partialData := false
result := tsdb.TimeSeriesSlice{} frames := data.Frames{}
for _, label := range labels { for _, label := range labels {
metricDataResult := metricDataResults[label] metricDataResult := metricDataResults[label]
plog.Debug("Processing metric data result", "label", label, "statusCode", metricDataResult.StatusCode)
if *metricDataResult.StatusCode != "Complete" { if *metricDataResult.StatusCode != "Complete" {
plog.Debug("Handling a partial result")
partialData = true partialData = true
} }
...@@ -85,8 +90,8 @@ func parseGetMetricDataTimeSeries(metricDataResults map[string]*cloudwatch.Metri ...@@ -85,8 +90,8 @@ func parseGetMetricDataTimeSeries(metricDataResults map[string]*cloudwatch.Metri
} }
} }
// In case a multi-valued dimension is used and the cloudwatch query yields no values, create one empty time series for each dimension value. // In case a multi-valued dimension is used and the cloudwatch query yields no values, create one empty time
// Use that dimension value to expand the alias field // series for each dimension value. Use that dimension value to expand the alias field
if len(metricDataResult.Values) == 0 && query.isMultiValuedDimensionExpression() { if len(metricDataResult.Values) == 0 && query.isMultiValuedDimensionExpression() {
series := 0 series := 0
multiValuedDimension := "" multiValuedDimension := ""
...@@ -98,62 +103,78 @@ func parseGetMetricDataTimeSeries(metricDataResults map[string]*cloudwatch.Metri ...@@ -98,62 +103,78 @@ func parseGetMetricDataTimeSeries(metricDataResults map[string]*cloudwatch.Metri
} }
for _, value := range query.Dimensions[multiValuedDimension] { for _, value := range query.Dimensions[multiValuedDimension] {
emptySeries := tsdb.TimeSeries{ tags := map[string]string{multiValuedDimension: value}
Tags: map[string]string{multiValuedDimension: value},
Points: make([]tsdb.TimePoint, 0),
}
for key, values := range query.Dimensions { for key, values := range query.Dimensions {
if key != multiValuedDimension && len(values) > 0 { if key != multiValuedDimension && len(values) > 0 {
emptySeries.Tags[key] = values[0] tags[key] = values[0]
} }
} }
emptySeries.Name = formatAlias(query, query.Stats, emptySeries.Tags, label) emptyFrame := data.Frame{
result = append(result, &emptySeries) Name: formatAlias(query, query.Stats, tags, label),
Fields: []*data.Field{
data.NewField("timestamp", nil, []float64{}),
data.NewField("value", tags, []*float64{}),
},
}
frames = append(frames, &emptyFrame)
} }
} else { } else {
keys := make([]string, 0) dims := make([]string, 0, len(query.Dimensions))
for k := range query.Dimensions { for k := range query.Dimensions {
keys = append(keys, k) dims = append(dims, k)
} }
sort.Strings(keys) sort.Strings(dims)
series := tsdb.TimeSeries{ tags := data.Labels{}
Tags: make(map[string]string), for _, dim := range dims {
Points: make([]tsdb.TimePoint, 0), plog.Debug("Handling dimension", "dimension", dim)
} values := query.Dimensions[dim]
for _, key := range keys {
values := query.Dimensions[key]
if len(values) == 1 && values[0] != "*" { if len(values) == 1 && values[0] != "*" {
series.Tags[key] = values[0] plog.Debug("Got a tag value", "tag", dim, "value", values[0])
tags[dim] = values[0]
} else { } else {
for _, value := range values { for _, value := range values {
if value == label || value == "*" { if value == label || value == "*" {
series.Tags[key] = label plog.Debug("Got a tag value", "tag", dim, "value", value, "label", label)
tags[dim] = label
} else if strings.Contains(label, value) { } else if strings.Contains(label, value) {
series.Tags[key] = value plog.Debug("Got a tag value", "tag", dim, "value", value, "label", label)
tags[dim] = value
} }
} }
} }
} }
series.Name = formatAlias(query, query.Stats, series.Tags, label) timestamps := []float64{}
points := []*float64{}
for j, t := range metricDataResult.Timestamps { for j, t := range metricDataResult.Timestamps {
if j > 0 { if j > 0 {
expectedTimestamp := metricDataResult.Timestamps[j-1].Add(time.Duration(query.Period) * time.Second) expectedTimestamp := metricDataResult.Timestamps[j-1].Add(time.Duration(query.Period) * time.Second)
if expectedTimestamp.Before(*t) { if expectedTimestamp.Before(*t) {
series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFromPtr(nil), float64(expectedTimestamp.Unix()*1000))) timestamps = append(timestamps, float64(expectedTimestamp.Unix()*1000))
points = append(points, nil)
} }
} }
series.Points = append(series.Points, tsdb.NewTimePoint(null.FloatFrom(*metricDataResult.Values[j]), val := metricDataResult.Values[j]
float64(t.Unix())*1000)) plog.Debug("Handling timestamp", "timestamp", t, "value", *val)
timestamps = append(timestamps, float64(t.Unix()*1000))
points = append(points, val)
} }
result = append(result, &series)
fields := []*data.Field{
data.NewField("timestamp", nil, timestamps),
data.NewField("value", tags, points),
}
frame := data.Frame{
Name: formatAlias(query, query.Stats, tags, label),
Fields: fields,
} }
frames = append(frames, &frame)
} }
return &result, partialData, nil }
return frames, partialData, nil
} }
func formatAlias(query *cloudWatchQuery, stat string, dimensions map[string]string, label string) string { func formatAlias(query *cloudWatchQuery, stat string, dimensions map[string]string, label string) string {
......
...@@ -6,7 +6,6 @@ import ( ...@@ -6,7 +6,6 @@ import (
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch" "github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/grafana/grafana/pkg/components/null"
. "github.com/smartystreets/goconvey/convey" . "github.com/smartystreets/goconvey/convey"
) )
...@@ -61,17 +60,17 @@ func TestCloudWatchResponseParser(t *testing.T) { ...@@ -61,17 +60,17 @@ func TestCloudWatchResponseParser(t *testing.T) {
Period: 60, Period: 60,
Alias: "{{LoadBalancer}} Expanded", Alias: "{{LoadBalancer}} Expanded",
} }
series, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query) frames, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query)
timeSeries := (*series)[0]
So(err, ShouldBeNil) So(err, ShouldBeNil)
frame1 := frames[0]
So(partialData, ShouldBeFalse) So(partialData, ShouldBeFalse)
So(timeSeries.Name, ShouldEqual, "lb1 Expanded") So(frame1.Name, ShouldEqual, "lb1 Expanded")
So(timeSeries.Tags["LoadBalancer"], ShouldEqual, "lb1") So(frame1.Fields[1].Labels["LoadBalancer"], ShouldEqual, "lb1")
timeSeries2 := (*series)[1] frame2 := frames[1]
So(timeSeries2.Name, ShouldEqual, "lb2 Expanded") So(frame2.Name, ShouldEqual, "lb2 Expanded")
So(timeSeries2.Tags["LoadBalancer"], ShouldEqual, "lb2") So(frame2.Fields[1].Labels["LoadBalancer"], ShouldEqual, "lb2")
}) })
Convey("can expand dimension value using substring", func() { Convey("can expand dimension value using substring", func() {
...@@ -123,16 +122,17 @@ func TestCloudWatchResponseParser(t *testing.T) { ...@@ -123,16 +122,17 @@ func TestCloudWatchResponseParser(t *testing.T) {
Period: 60, Period: 60,
Alias: "{{LoadBalancer}} Expanded", Alias: "{{LoadBalancer}} Expanded",
} }
series, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query) frames, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query)
timeSeries := (*series)[0]
So(err, ShouldBeNil) So(err, ShouldBeNil)
frame1 := frames[0]
So(partialData, ShouldBeFalse) So(partialData, ShouldBeFalse)
So(timeSeries.Name, ShouldEqual, "lb1 Expanded") So(frame1.Name, ShouldEqual, "lb1 Expanded")
So(timeSeries.Tags["LoadBalancer"], ShouldEqual, "lb1") So(frame1.Fields[1].Labels["LoadBalancer"], ShouldEqual, "lb1")
timeSeries2 := (*series)[1] frame2 := frames[1]
So(timeSeries2.Name, ShouldEqual, "lb2 Expanded") So(frame2.Name, ShouldEqual, "lb2 Expanded")
So(timeSeries2.Tags["LoadBalancer"], ShouldEqual, "lb2") So(frame2.Fields[1].Labels["LoadBalancer"], ShouldEqual, "lb2")
}) })
Convey("can expand dimension value using wildcard", func() { Convey("can expand dimension value using wildcard", func() {
...@@ -184,12 +184,12 @@ func TestCloudWatchResponseParser(t *testing.T) { ...@@ -184,12 +184,12 @@ func TestCloudWatchResponseParser(t *testing.T) {
Period: 60, Period: 60,
Alias: "{{LoadBalancer}} Expanded", Alias: "{{LoadBalancer}} Expanded",
} }
series, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query) frames, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(partialData, ShouldBeFalse) So(partialData, ShouldBeFalse)
So((*series)[0].Name, ShouldEqual, "lb3 Expanded") So(frames[0].Name, ShouldEqual, "lb3 Expanded")
So((*series)[1].Name, ShouldEqual, "lb4 Expanded") So(frames[1].Name, ShouldEqual, "lb4 Expanded")
}) })
Convey("can expand dimension value when no values are returned and a multi-valued template variable is used", func() { Convey("can expand dimension value when no values are returned and a multi-valued template variable is used", func() {
...@@ -221,13 +221,13 @@ func TestCloudWatchResponseParser(t *testing.T) { ...@@ -221,13 +221,13 @@ func TestCloudWatchResponseParser(t *testing.T) {
Period: 60, Period: 60,
Alias: "{{LoadBalancer}} Expanded", Alias: "{{LoadBalancer}} Expanded",
} }
series, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query) frames, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(partialData, ShouldBeFalse) So(partialData, ShouldBeFalse)
So(len(*series), ShouldEqual, 2) So(len(frames), ShouldEqual, 2)
So((*series)[0].Name, ShouldEqual, "lb1 Expanded") So(frames[0].Name, ShouldEqual, "lb1 Expanded")
So((*series)[1].Name, ShouldEqual, "lb2 Expanded") So(frames[1].Name, ShouldEqual, "lb2 Expanded")
}) })
Convey("can expand dimension value when no values are returned and a multi-valued template variable and two single-valued dimensions are used", func() { Convey("can expand dimension value when no values are returned and a multi-valued template variable and two single-valued dimensions are used", func() {
...@@ -261,13 +261,13 @@ func TestCloudWatchResponseParser(t *testing.T) { ...@@ -261,13 +261,13 @@ func TestCloudWatchResponseParser(t *testing.T) {
Period: 60, Period: 60,
Alias: "{{LoadBalancer}} Expanded {{InstanceType}} - {{Resource}}", Alias: "{{LoadBalancer}} Expanded {{InstanceType}} - {{Resource}}",
} }
series, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query) frames, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query)
So(err, ShouldBeNil) So(err, ShouldBeNil)
So(partialData, ShouldBeFalse) So(partialData, ShouldBeFalse)
So(len(*series), ShouldEqual, 2) So(len(frames), ShouldEqual, 2)
So((*series)[0].Name, ShouldEqual, "lb1 Expanded micro - res") So(frames[0].Name, ShouldEqual, "lb1 Expanded micro - res")
So((*series)[1].Name, ShouldEqual, "lb2 Expanded micro - res") So(frames[1].Name, ShouldEqual, "lb2 Expanded micro - res")
}) })
Convey("can parse cloudwatch response", func() { Convey("can parse cloudwatch response", func() {
...@@ -304,17 +304,18 @@ func TestCloudWatchResponseParser(t *testing.T) { ...@@ -304,17 +304,18 @@ func TestCloudWatchResponseParser(t *testing.T) {
Period: 60, Period: 60,
Alias: "{{namespace}}_{{metric}}_{{stat}}", Alias: "{{namespace}}_{{metric}}_{{stat}}",
} }
series, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query) frames, partialData, err := parseGetMetricDataTimeSeries(mdrs, labels, query)
timeSeries := (*series)[0]
So(err, ShouldBeNil) So(err, ShouldBeNil)
frame := frames[0]
So(partialData, ShouldBeFalse) So(partialData, ShouldBeFalse)
So(timeSeries.Name, ShouldEqual, "AWS/ApplicationELB_TargetResponseTime_Average") So(frame.Name, ShouldEqual, "AWS/ApplicationELB_TargetResponseTime_Average")
So(timeSeries.Tags["LoadBalancer"], ShouldEqual, "lb") So(frame.Fields[1].Labels["LoadBalancer"], ShouldEqual, "lb")
So(timeSeries.Points[0][0].String(), ShouldEqual, null.FloatFrom(10.0).String()) So(frame.Fields[1].Len(), ShouldEqual, 4)
So(timeSeries.Points[1][0].String(), ShouldEqual, null.FloatFrom(20.0).String()) So(*frame.Fields[1].At(0).(*float64), ShouldEqual, 10.0)
So(timeSeries.Points[2][0].String(), ShouldEqual, null.FloatFromPtr(nil).String()) So(*frame.Fields[1].At(1).(*float64), ShouldEqual, 20.0)
So(timeSeries.Points[3][0].String(), ShouldEqual, null.FloatFrom(30.0).String()) So(frame.Fields[1].At(2).(*float64), ShouldBeNil)
So(*frame.Fields[1].At(3).(*float64), ShouldEqual, 30.0)
}) })
}) })
} }
...@@ -6,17 +6,19 @@ import ( ...@@ -6,17 +6,19 @@ import (
"github.com/grafana/grafana/pkg/infra/log" "github.com/grafana/grafana/pkg/infra/log"
"github.com/grafana/grafana/pkg/tsdb" "github.com/grafana/grafana/pkg/tsdb"
"github.com/grafana/grafana/pkg/util/errutil"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
) )
func (e *cloudWatchExecutor) executeTimeSeriesQuery(ctx context.Context, queryContext *tsdb.TsdbQuery) (*tsdb.Response, error) { func (e *cloudWatchExecutor) executeTimeSeriesQuery(ctx context.Context, queryContext *tsdb.TsdbQuery) (*tsdb.Response, error) {
plog.Debug("Executing time series query")
startTime, err := queryContext.TimeRange.ParseFrom() startTime, err := queryContext.TimeRange.ParseFrom()
if err != nil { if err != nil {
return nil, err return nil, errutil.Wrap("failed to parse start time", err)
} }
endTime, err := queryContext.TimeRange.ParseTo() endTime, err := queryContext.TimeRange.ParseTo()
if err != nil { if err != nil {
return nil, err return nil, errutil.Wrap("failed to parse end time", err)
} }
if !startTime.Before(endTime) { if !startTime.Before(endTime) {
return nil, fmt.Errorf("invalid time range: start time must be before end time") return nil, fmt.Errorf("invalid time range: start time must be before end time")
......
...@@ -3,16 +3,9 @@ package cloudwatch ...@@ -3,16 +3,9 @@ package cloudwatch
import ( import (
"fmt" "fmt"
"github.com/aws/aws-sdk-go/aws" "github.com/grafana/grafana-plugin-sdk-go/data"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/grafana/grafana/pkg/tsdb"
) )
type cloudWatchClient interface {
GetMetricDataWithContext(ctx aws.Context, input *cloudwatch.GetMetricDataInput, opts ...request.Option) (*cloudwatch.GetMetricDataOutput, error)
}
type requestQuery struct { type requestQuery struct {
RefId string RefId string
Region string Region string
...@@ -31,7 +24,7 @@ type requestQuery struct { ...@@ -31,7 +24,7 @@ type requestQuery struct {
} }
type cloudwatchResponse struct { type cloudwatchResponse struct {
series *tsdb.TimeSeriesSlice DataFrames data.Frames
Id string Id string
RefId string RefId string
Expression string Expression string
......
...@@ -89,7 +89,7 @@ func NewTimeSeries(name string, points TimeSeriesPoints) *TimeSeries { ...@@ -89,7 +89,7 @@ func NewTimeSeries(name string, points TimeSeriesPoints) *TimeSeries {
} }
} }
// DataFrames interface for retrieving encoded and decoded data frames. // DataFrames is an interface for retrieving encoded and decoded data frames.
// //
// See NewDecodedDataFrames and NewEncodedDataFrames for more information. // See NewDecodedDataFrames and NewEncodedDataFrames for more information.
type DataFrames interface { type DataFrames interface {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment