Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set unit for CloudWatch GetMetricStatistics result (Fixes #13575) #13571

Merged
merged 4 commits into from
Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions pkg/tsdb/cloudwatch/cloudwatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,13 @@ func (e *CloudWatchExecutor) executeTimeSeriesQuery(ctx context.Context, queryCo
if ae, ok := err.(awserr.Error); ok && ae.Code() == "500" {
return err
}
result.Results[queryRes.RefId] = queryRes
if err != nil {
result.Results[queryRes.RefId].Error = err
result.Results[query.RefId] = &tsdb.QueryResult{
Error: err,
}
return nil
}
result.Results[queryRes.RefId] = queryRes
return nil
})
}
Expand Down Expand Up @@ -269,7 +272,7 @@ func (e *CloudWatchExecutor) executeGetMetricDataQuery(ctx context.Context, regi
for _, query := range queries {
// 1 minutes resolution metrics is stored for 15 days, 15 * 24 * 60 = 21600
if query.HighResolution && (((endTime.Unix() - startTime.Unix()) / int64(query.Period)) > 21600) {
return nil, errors.New("too long query period")
return queryResponses, errors.New("too long query period")
}

mdq := &cloudwatch.MetricDataQuery{
Expand Down Expand Up @@ -362,6 +365,7 @@ func (e *CloudWatchExecutor) executeGetMetricDataQuery(ctx context.Context, regi
}

queryRes.Series = append(queryRes.Series, &series)
queryRes.Meta = simplejson.New()
queryResponses = append(queryResponses, queryRes)
}

Expand Down Expand Up @@ -565,6 +569,12 @@ func parseResponse(resp *cloudwatch.GetMetricStatisticsOutput, query *CloudWatch
}

queryRes.Series = append(queryRes.Series, &series)
queryRes.Meta = simplejson.New()
if len(resp.Datapoints) > 0 && resp.Datapoints[0].Unit != nil {
if unit, ok := cloudwatchUnitMappings[*resp.Datapoints[0].Unit]; ok {
queryRes.Meta.Set("unit", unit)
}
}
}

return queryRes, nil
Expand Down
5 changes: 5 additions & 0 deletions pkg/tsdb/cloudwatch/cloudwatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func TestCloudWatch(t *testing.T) {
"p50.00": aws.Float64(30.0),
"p90.00": aws.Float64(40.0),
},
Unit: aws.String("Seconds"),
},
},
}
Expand Down Expand Up @@ -103,6 +104,7 @@ func TestCloudWatch(t *testing.T) {
So(queryRes.Series[1].Points[0][0].String(), ShouldEqual, null.FloatFrom(20.0).String())
So(queryRes.Series[2].Points[0][0].String(), ShouldEqual, null.FloatFrom(30.0).String())
So(queryRes.Series[3].Points[0][0].String(), ShouldEqual, null.FloatFrom(40.0).String())
So(queryRes.Meta.Get("unit").MustString(), ShouldEqual, "s")
})

Convey("terminate gap of data points", func() {
Expand All @@ -118,6 +120,7 @@ func TestCloudWatch(t *testing.T) {
"p50.00": aws.Float64(30.0),
"p90.00": aws.Float64(40.0),
},
Unit: aws.String("Seconds"),
},
{
Timestamp: aws.Time(timestamp.Add(60 * time.Second)),
Expand All @@ -127,6 +130,7 @@ func TestCloudWatch(t *testing.T) {
"p50.00": aws.Float64(40.0),
"p90.00": aws.Float64(50.0),
},
Unit: aws.String("Seconds"),
},
{
Timestamp: aws.Time(timestamp.Add(180 * time.Second)),
Expand All @@ -136,6 +140,7 @@ func TestCloudWatch(t *testing.T) {
"p50.00": aws.Float64(50.0),
"p90.00": aws.Float64(60.0),
},
Unit: aws.String("Seconds"),
},
},
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/tsdb/cloudwatch/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cloudwatch

var cloudwatchUnitMappings = map[string]string{
"Seconds": "s",
"Microseconds": "µs",
"Milliseconds": "ms",
"Bytes": "bytes",
"Kilobytes": "kbytes",
"Megabytes": "mbytes",
"Gigabytes": "gbytes",
//"Terabytes": "",
"Bits": "bits",
//"Kilobits": "",
//"Megabits": "",
//"Gigabits": "",
//"Terabits": "",
"Percent": "percent",
//"Count": "",
"Bytes/Second": "Bps",
"Kilobytes/Second": "KBs",
"Megabytes/Second": "MBs",
"Gigabytes/Second": "GBs",
//"Terabytes/Second": "",
"Bits/Second": "bps",
"Kilobits/Second": "Kbits",
"Megabits/Second": "Mbits",
"Gigabits/Second": "Gbits",
//"Terabits/Second": "",
//"Count/Second": "",
}
2 changes: 1 addition & 1 deletion public/app/plugins/datasource/cloudwatch/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ export default class CloudWatchDatasource {
if (res.results) {
_.forEach(res.results, queryRes => {
_.forEach(queryRes.series, series => {
data.push({ target: series.name, datapoints: series.points });
data.push({ target: series.name, datapoints: series.points, unit: queryRes.meta.unit || 'none' });
});
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ describe('CloudWatchDatasource', () => {
A: {
error: '',
refId: 'A',
meta: {},
series: [
{
name: 'CPUUtilization_Average',
Expand Down Expand Up @@ -221,6 +222,7 @@ describe('CloudWatchDatasource', () => {
A: {
error: '',
refId: 'A',
meta: {},
series: [
{
name: 'TargetResponseTime_p90.00',
Expand Down