grafana/pkg/tsdb/cloudwatch/annotation_query.go

185 lines
5.1 KiB
Go
Raw Normal View History

2017-09-25 04:16:40 -05:00
package cloudwatch
import (
"context"
"errors"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/grafana/grafana-plugin-sdk-go/backend"
"github.com/grafana/grafana-plugin-sdk-go/data"
2017-09-25 04:16:40 -05:00
"github.com/grafana/grafana/pkg/components/simplejson"
"github.com/grafana/grafana/pkg/util/errutil"
2017-09-25 04:16:40 -05:00
)
func (e *cloudWatchExecutor) executeAnnotationQuery(ctx context.Context, model *simplejson.Json, query backend.DataQuery, pluginCtx backend.PluginContext) (*backend.QueryDataResponse, error) {
result := backend.NewQueryDataResponse()
usePrefixMatch := model.Get("prefixMatching").MustBool(false)
region := model.Get("region").MustString("")
namespace := model.Get("namespace").MustString("")
metricName := model.Get("metricName").MustString("")
dimensions := model.Get("dimensions").MustMap()
statistics := parseStatistics(model)
period := int64(model.Get("period").MustInt(0))
2017-09-26 01:45:52 -05:00
if period == 0 && !usePrefixMatch {
period = 300
2017-09-25 04:16:40 -05:00
}
actionPrefix := model.Get("actionPrefix").MustString("")
alarmNamePrefix := model.Get("alarmNamePrefix").MustString("")
2017-09-25 04:16:40 -05:00
cli, err := e.getCWClient(region, pluginCtx)
2017-09-25 04:16:40 -05:00
if err != nil {
return nil, err
2017-09-25 04:16:40 -05:00
}
var alarmNames []*string
if usePrefixMatch {
params := &cloudwatch.DescribeAlarmsInput{
MaxRecords: aws.Int64(100),
ActionPrefix: aws.String(actionPrefix),
AlarmNamePrefix: aws.String(alarmNamePrefix),
}
resp, err := cli.DescribeAlarms(params)
2017-09-25 04:16:40 -05:00
if err != nil {
return nil, errutil.Wrap("failed to call cloudwatch:DescribeAlarms", err)
2017-09-25 04:16:40 -05:00
}
alarmNames = filterAlarms(resp, namespace, metricName, dimensions, statistics, period)
2017-09-25 04:16:40 -05:00
} else {
if region == "" || namespace == "" || metricName == "" || len(statistics) == 0 {
return result, errors.New("invalid annotations query")
2017-09-25 04:16:40 -05:00
}
var qd []*cloudwatch.Dimension
for k, v := range dimensions {
if vv, ok := v.([]interface{}); ok {
for _, vvv := range vv {
if vvvv, ok := vvv.(string); ok {
qd = append(qd, &cloudwatch.Dimension{
Name: aws.String(k),
Value: aws.String(vvvv),
})
}
}
2017-09-25 04:16:40 -05:00
}
}
for _, s := range statistics {
params := &cloudwatch.DescribeAlarmsForMetricInput{
Namespace: aws.String(namespace),
MetricName: aws.String(metricName),
Dimensions: qd,
Statistic: aws.String(s),
2018-04-16 13:04:58 -05:00
Period: aws.Int64(period),
2017-09-25 04:16:40 -05:00
}
resp, err := cli.DescribeAlarmsForMetric(params)
2017-09-25 04:16:40 -05:00
if err != nil {
return nil, errutil.Wrap("failed to call cloudwatch:DescribeAlarmsForMetric", err)
2017-09-25 04:16:40 -05:00
}
for _, alarm := range resp.MetricAlarms {
alarmNames = append(alarmNames, alarm.AlarmName)
}
}
}
annotations := make([]map[string]string, 0)
for _, alarmName := range alarmNames {
params := &cloudwatch.DescribeAlarmHistoryInput{
2017-09-26 10:00:38 -05:00
AlarmName: alarmName,
StartDate: aws.Time(query.TimeRange.From),
EndDate: aws.Time(query.TimeRange.To),
2017-09-26 10:00:38 -05:00
MaxRecords: aws.Int64(100),
2017-09-25 04:16:40 -05:00
}
resp, err := cli.DescribeAlarmHistory(params)
2017-09-25 04:16:40 -05:00
if err != nil {
return nil, errutil.Wrap("failed to call cloudwatch:DescribeAlarmHistory", err)
2017-09-25 04:16:40 -05:00
}
for _, history := range resp.AlarmHistoryItems {
annotation := make(map[string]string)
annotation["time"] = history.Timestamp.UTC().Format(time.RFC3339)
annotation["title"] = *history.AlarmName
annotation["tags"] = *history.HistoryItemType
annotation["text"] = *history.HistorySummary
annotations = append(annotations, annotation)
}
}
respD := result.Responses[query.RefID]
respD.Frames = append(respD.Frames, transformAnnotationToTable(annotations, query))
result.Responses[query.RefID] = respD
return result, err
2017-09-25 04:16:40 -05:00
}
func transformAnnotationToTable(annotations []map[string]string, query backend.DataQuery) *data.Frame {
frame := data.NewFrame(query.RefID,
data.NewField("time", nil, []string{}),
data.NewField("title", nil, []string{}),
data.NewField("tags", nil, []string{}),
data.NewField("text", nil, []string{}),
)
for _, a := range annotations {
frame.AppendRow(a["time"], a["title"], a["tags"], a["text"])
2017-09-25 04:16:40 -05:00
}
frame.Meta = &data.FrameMeta{
Custom: map[string]interface{}{
"rowCount": len(annotations),
},
2017-09-25 04:16:40 -05:00
}
return frame
2017-09-25 04:16:40 -05:00
}
func filterAlarms(alarms *cloudwatch.DescribeAlarmsOutput, namespace string, metricName string,
dimensions map[string]interface{}, statistics []string, period int64) []*string {
2017-09-25 04:16:40 -05:00
alarmNames := make([]*string, 0)
for _, alarm := range alarms.MetricAlarms {
if namespace != "" && *alarm.Namespace != namespace {
continue
}
if metricName != "" && *alarm.MetricName != metricName {
continue
}
match := true
if len(dimensions) != 0 {
if len(alarm.Dimensions) != len(dimensions) {
match = false
} else {
for _, d := range alarm.Dimensions {
if _, ok := dimensions[*d.Name]; !ok {
match = false
}
2017-09-26 01:45:52 -05:00
}
2017-09-25 04:16:40 -05:00
}
}
if !match {
continue
}
if len(statistics) != 0 {
found := false
for _, s := range statistics {
if *alarm.Statistic == s {
found = true
break
2017-09-25 04:16:40 -05:00
}
}
if !found {
continue
}
}
2017-09-26 01:45:52 -05:00
if period != 0 && *alarm.Period != period {
continue
}
2017-09-25 04:16:40 -05:00
alarmNames = append(alarmNames, alarm.AlarmName)
}
return alarmNames
}