grafana/pkg/tsdb/cloudwatch/annotation_query.go

188 lines
4.7 KiB
Go
Raw Normal View History

2017-09-25 04:16:40 -05:00
package cloudwatch
import (
"errors"
"fmt"
"strconv"
2017-09-25 04:16:40 -05:00
"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
)
type annotationEvent struct {
Title string
Time time.Time
Tags string
Text string
}
func (e *cloudWatchExecutor) executeAnnotationQuery(pluginCtx backend.PluginContext, model DataQueryJson, query backend.DataQuery) (*backend.QueryDataResponse, error) {
result := backend.NewQueryDataResponse()
statistic := ""
if model.Statistic != nil {
statistic = *model.Statistic
}
var period int64
if model.Period != "" {
p, err := strconv.ParseInt(model.Period, 10, 64)
if err != nil {
return nil, err
}
period = p
}
if period == 0 && !model.PrefixMatching {
2017-09-26 01:45:52 -05:00
period = 300
2017-09-25 04:16:40 -05:00
}
actionPrefix := model.ActionPrefix
alarmNamePrefix := model.AlarmNamePrefix
cli, err := e.getCWClient(pluginCtx, model.Region)
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 model.PrefixMatching {
2017-09-25 04:16:40 -05:00
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, fmt.Errorf("%v: %w", "failed to call cloudwatch:DescribeAlarms", err)
2017-09-25 04:16:40 -05:00
}
alarmNames = filterAlarms(resp, model.Namespace, model.MetricName, model.Dimensions, statistic, period)
2017-09-25 04:16:40 -05:00
} else {
if model.Region == "" || model.Namespace == "" || model.MetricName == "" || statistic == "" {
return result, errors.New("invalid annotations query")
2017-09-25 04:16:40 -05:00
}
var qd []*cloudwatch.Dimension
for k, v := range model.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
}
}
params := &cloudwatch.DescribeAlarmsForMetricInput{
Namespace: aws.String(model.Namespace),
MetricName: aws.String(model.MetricName),
Dimensions: qd,
Statistic: aws.String(statistic),
Period: aws.Int64(period),
}
resp, err := cli.DescribeAlarmsForMetric(params)
if err != nil {
return nil, fmt.Errorf("%v: %w", "failed to call cloudwatch:DescribeAlarmsForMetric", err)
}
for _, alarm := range resp.MetricAlarms {
alarmNames = append(alarmNames, alarm.AlarmName)
2017-09-25 04:16:40 -05:00
}
}
annotations := make([]*annotationEvent, 0)
2017-09-25 04:16:40 -05:00
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, fmt.Errorf("%v: %w", "failed to call cloudwatch:DescribeAlarmHistory", err)
2017-09-25 04:16:40 -05:00
}
for _, history := range resp.AlarmHistoryItems {
annotations = append(annotations, &annotationEvent{
Time: *history.Timestamp,
Title: *history.AlarmName,
Tags: *history.HistoryItemType,
Text: *history.HistorySummary,
})
2017-09-25 04:16:40 -05:00
}
}
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 []*annotationEvent, query backend.DataQuery) *data.Frame {
frame := data.NewFrame(query.RefID,
data.NewField("time", nil, []time.Time{}),
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{}, statistic 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
}
matchDimension := true
if len(dimensions) != 0 {
if len(alarm.Dimensions) != len(dimensions) {
matchDimension = false
} else {
for _, d := range alarm.Dimensions {
if _, ok := dimensions[*d.Name]; !ok {
matchDimension = false
}
2017-09-26 01:45:52 -05:00
}
2017-09-25 04:16:40 -05:00
}
}
if !matchDimension {
2017-09-25 04:16:40 -05:00
continue
}
if *alarm.Statistic != statistic {
continue
2017-09-25 04:16:40 -05:00
}
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
}