-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcloudwatch.go
110 lines (92 loc) · 2.77 KB
/
cloudwatch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"log"
"strconv"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"
)
const offset time.Duration = time.Hour
type Dimension struct {
Name string
Value string
}
type MetricStat struct {
Id string `yaml:"Id"`
Name string `yaml:"Name"`
Namespace string `yaml:"Namespace"`
Period string `yaml:"Period"`
Unit string `yaml:"Unit"`
Stat string `yaml:"Stat"`
}
func (ms *MetricStat) String() string {
return fmt.Sprintf(" - Name: %s\n Namespace: %s\n Period: %d\n Unit:%s\n Stat:%s\n", ms.Name, ms.Namespace, ms.Period, ms.Unit, ms.Stat)
}
type MetricToFetch struct {
Resource interface{}
MStat MetricStat
Dimensions []Dimension
}
func NewGetMetricDataInput(mTofetch []MetricToFetch) []*cloudwatch.GetMetricDataInput {
// General way:
// []Dimension -> Metric -> MetricStat -> []MetricDataQuery -> GetMetricDataInput
datainputs := make([]*cloudwatch.GetMetricDataInput, 0)
endTime := time.Now()
startTime := endTime.Add(-offset)
mQueries := make([]*cloudwatch.MetricDataQuery, 0)
di := &cloudwatch.GetMetricDataInput{}
for index, metric := range mTofetch {
m := metric.MStat
dimensions := make([]*cloudwatch.Dimension, 0)
for i := 0; i < len(metric.Dimensions); i++ {
name := &metric.Dimensions[i].Name
value := &metric.Dimensions[i].Value
dimensions = append(dimensions, &cloudwatch.Dimension{
Name: name,
Value: value,
})
}
p, _ := strconv.Atoi(m.Period)
mStat := &cloudwatch.MetricStat{
Metric: &cloudwatch.Metric{
Dimensions: dimensions,
MetricName: &m.Name,
Namespace: &m.Namespace,
},
Period: aws.Int64(int64(p)),
Unit: &m.Unit,
Stat: &m.Stat,
}
dimensions = make([]*cloudwatch.Dimension, 0)
mdatQuery := &cloudwatch.MetricDataQuery{
Id: &m.Id,
MetricStat: mStat,
}
mQueries = append(mQueries, mdatQuery)
if index == len(mTofetch)-1 || index%400 == 0 && index != 0 {
di.SetMetricDataQueries(mQueries)
di.SetEndTime(endTime)
di.SetStartTime(startTime)
datainputs = append(datainputs, di)
di = &cloudwatch.GetMetricDataInput{}
mQueries = make([]*cloudwatch.MetricDataQuery, 0)
}
}
return datainputs
}
type CloudWatchFetcher struct {
cloudwatchSvc *cloudwatch.CloudWatch
}
func (cf *CloudWatchFetcher) FetchMetrics(metricinputs []*cloudwatch.GetMetricDataInput) ([]*cloudwatch.MetricDataResult, error) {
metricdataresults := make([]*cloudwatch.MetricDataResult, 0)
for _, mi := range metricinputs {
mo, err := cf.cloudwatchSvc.GetMetricData(mi)
if err != nil {
log.Printf("Cloud not fetch metrics from CLoudWatch : %v", err)
return metricdataresults, err
}
metricdataresults = append(metricdataresults, mo.MetricDataResults...)
}
return metricdataresults, nil
}