From e1fe15e0949164ad1379bad8678c4be1fdd889fb Mon Sep 17 00:00:00 2001 From: Mitsuhiro Tanda Date: Tue, 26 Sep 2017 15:45:52 +0900 Subject: [PATCH] fix annotation query --- pkg/tsdb/cloudwatch/annotation_query.go | 32 +++++++++++-------- .../datasource/cloudwatch/datasource.js | 4 ++- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/pkg/tsdb/cloudwatch/annotation_query.go b/pkg/tsdb/cloudwatch/annotation_query.go index e9680e9ff84..2283e12b64e 100644 --- a/pkg/tsdb/cloudwatch/annotation_query.go +++ b/pkg/tsdb/cloudwatch/annotation_query.go @@ -20,16 +20,16 @@ func (e *CloudWatchExecutor) executeAnnotationQuery(ctx context.Context, queryCo queryResult := &tsdb.QueryResult{Meta: simplejson.New(), RefId: firstQuery.RefId} parameters := firstQuery.Model - usePrefixMatch := parameters.Get("prefixMatching").MustBool() + usePrefixMatch := parameters.Get("prefixMatching").MustBool(false) region := parameters.Get("region").MustString("") namespace := parameters.Get("namespace").MustString("") metricName := parameters.Get("metricName").MustString("") dimensions := parameters.Get("dimensions").MustMap() statistics := parameters.Get("statistics").MustStringArray() extendedStatistics := parameters.Get("extendedStatistics").MustStringArray() - period := int64(300) - if usePrefixMatch { - period = int64(parameters.Get("period").MustInt(0)) + period := int64(parameters.Get("period").MustInt(0)) + if period == 0 && !usePrefixMatch { + period = 300 } actionPrefix := parameters.Get("actionPrefix").MustString("") alarmNamePrefix := parameters.Get("alarmNamePrefix").MustString("") @@ -75,9 +75,9 @@ func (e *CloudWatchExecutor) executeAnnotationQuery(ctx context.Context, queryCo params := &cloudwatch.DescribeAlarmsForMetricInput{ Namespace: aws.String(namespace), MetricName: aws.String(metricName), - Period: aws.Int64(int64(period)), Dimensions: qd, Statistic: aws.String(s), + Period: aws.Int64(int64(period)), } resp, err := svc.DescribeAlarmsForMetric(params) if err != nil { @@ -91,9 +91,9 @@ func (e *CloudWatchExecutor) executeAnnotationQuery(ctx context.Context, queryCo params := &cloudwatch.DescribeAlarmsForMetricInput{ Namespace: aws.String(namespace), MetricName: aws.String(metricName), - Period: aws.Int64(int64(period)), Dimensions: qd, ExtendedStatistic: aws.String(s), + Period: aws.Int64(int64(period)), } resp, err := svc.DescribeAlarmsForMetric(params) if err != nil { @@ -109,7 +109,6 @@ func (e *CloudWatchExecutor) executeAnnotationQuery(ctx context.Context, queryCo if err != nil { return nil, err } - endTime, err := queryContext.TimeRange.ParseTo() if err != nil { return nil, err @@ -175,17 +174,20 @@ func filterAlarms(alarms *cloudwatch.DescribeAlarmsOutput, namespace string, met } match := true - for _, d := range alarm.Dimensions { - if _, ok := dimensions[*d.Name]; !ok { - match = false + if len(dimensions) == 0 { + // all match + } else if len(alarm.Dimensions) != len(dimensions) { + match = false + } else { + for _, d := range alarm.Dimensions { + if _, ok := dimensions[*d.Name]; !ok { + match = false + } } } if !match { continue } - if period != 0 && *alarm.Period != period { - continue - } if len(statistics) != 0 { found := false @@ -211,6 +213,10 @@ func filterAlarms(alarms *cloudwatch.DescribeAlarmsOutput, namespace string, met } } + if period != 0 && *alarm.Period != period { + continue + } + alarmNames = append(alarmNames, alarm.AlarmName) } diff --git a/public/app/plugins/datasource/cloudwatch/datasource.js b/public/app/plugins/datasource/cloudwatch/datasource.js index de369429b4f..61222216b2a 100644 --- a/public/app/plugins/datasource/cloudwatch/datasource.js +++ b/public/app/plugins/datasource/cloudwatch/datasource.js @@ -259,6 +259,7 @@ function (angular, _, moment, dateMath, kbn, templatingVariable) { this.annotationQuery = function (options) { var annotation = options.annotation; + var statistics = _.map(annotation.statistics, function (s) { return templateSrv.replace(s); }); var defaultPeriod = annotation.prefixMatching ? '' : '300'; var period = annotation.period || defaultPeriod; period = parseInt(period, 10); @@ -268,7 +269,8 @@ function (angular, _, moment, dateMath, kbn, templatingVariable) { namespace: templateSrv.replace(annotation.namespace), metricName: templateSrv.replace(annotation.metricName), dimensions: this.convertDimensionFormat(annotation.dimensions, {}), - statistics: _.map(annotation.statistics, function (s) { return templateSrv.replace(s); }), + statistics: _.filter(statistics, function (s) { return _.includes(self.standardStatistics, s); }), + extendedStatistics: _.filter(statistics, function (s) { return !_.includes(self.standardStatistics, s); }), period: period, actionPrefix: annotation.actionPrefix || '', alarmNamePrefix: annotation.alarmNamePrefix || ''