Просмотр исходного кода

Merge pull request #13571 from mtanda/cw_unit

cloudwatch: automatically set graph yaxis unit 

fixes #13575
Marcus Efraimsson 7 лет назад
Родитель
Сommit
5f69854e2a

+ 13 - 3
pkg/tsdb/cloudwatch/cloudwatch.go

@@ -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
 		})
 	}
@@ -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{
@@ -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)
 	}
 
@@ -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

+ 5 - 0
pkg/tsdb/cloudwatch/cloudwatch_test.go

@@ -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"),
 					},
 				},
 			}
@@ -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() {
@@ -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)),
@@ -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)),
@@ -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"),
 					},
 				},
 			}

+ 30 - 0
pkg/tsdb/cloudwatch/constants.go

@@ -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":     "",
+}

+ 1 - 1
public/app/plugins/datasource/cloudwatch/datasource.ts

@@ -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' });
           });
         });
       }

+ 2 - 0
public/app/plugins/datasource/cloudwatch/specs/datasource.test.ts

@@ -60,6 +60,7 @@ describe('CloudWatchDatasource', () => {
         A: {
           error: '',
           refId: 'A',
+          meta: {},
           series: [
             {
               name: 'CPUUtilization_Average',
@@ -221,6 +222,7 @@ describe('CloudWatchDatasource', () => {
         A: {
           error: '',
           refId: 'A',
+          meta: {},
           series: [
             {
               name: 'TargetResponseTime_p90.00',