-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract.go
312 lines (273 loc) · 8.83 KB
/
abstract.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package main
import (
"fmt"
"math"
"math/rand"
"regexp"
"strings"
"sync"
log "github.com/sirupsen/logrus"
)
var (
cloudwatchSemaphore chan struct{}
tagSemaphore chan struct{}
)
func scrapeAwsData(config conf) ([]*tagsData, []*cloudwatchData) {
mux := &sync.Mutex{}
cwData := make([]*cloudwatchData, 0)
awsInfoData := make([]*tagsData, 0)
var wg sync.WaitGroup
for _, discoveryJob := range config.Discovery.Jobs {
for _, roleArn := range discoveryJob.RoleArns {
for _, region := range discoveryJob.Regions {
wg.Add(1)
go func(discoveryJob job, region string, roleArn string) {
defer wg.Done()
clientCloudwatch := cloudwatchInterface{
client: createCloudwatchSession(®ion, roleArn),
}
clientTag := tagsInterface{
client: createTagSession(®ion, roleArn),
apiGatewayClient: createAPIGatewaySession(®ion, roleArn),
asgClient: createASGSession(®ion, roleArn),
ec2Client: createEC2Session(®ion, roleArn),
elbv2Client: createELBV2Session(®ion, roleArn),
}
var resources []*tagsData
var metrics []*cloudwatchData
resources, metrics = scrapeDiscoveryJobUsingMetricData(discoveryJob, region, config.Discovery.ExportedTagsOnMetrics, clientTag, clientCloudwatch)
mux.Lock()
awsInfoData = append(awsInfoData, resources...)
cwData = append(cwData, metrics...)
mux.Unlock()
}(discoveryJob, region, roleArn)
}
}
}
for _, staticJob := range config.Static {
for _, roleArn := range staticJob.RoleArns {
for _, region := range staticJob.Regions {
wg.Add(1)
go func(staticJob static, region string, roleArn string) {
clientCloudwatch := cloudwatchInterface{
client: createCloudwatchSession(®ion, roleArn),
}
metrics := scrapeStaticJob(staticJob, region, clientCloudwatch)
mux.Lock()
cwData = append(cwData, metrics...)
mux.Unlock()
wg.Done()
}(staticJob, region, roleArn)
}
}
}
wg.Wait()
return awsInfoData, cwData
}
func scrapeStaticJob(resource static, region string, clientCloudwatch cloudwatchInterface) (cw []*cloudwatchData) {
mux := &sync.Mutex{}
var wg sync.WaitGroup
for j := range resource.Metrics {
metric := resource.Metrics[j]
wg.Add(1)
go func() {
defer wg.Done()
cloudwatchSemaphore <- struct{}{}
defer func() {
<-cloudwatchSemaphore
}()
id := resource.Name
service := strings.TrimPrefix(resource.Namespace, "AWS/")
data := cloudwatchData{
ID: &id,
Metric: &metric.Name,
Service: &service,
Statistics: metric.Statistics,
NilToZero: &metric.NilToZero,
AddCloudwatchTimestamp: &metric.AddCloudwatchTimestamp,
CustomTags: resource.CustomTags,
Dimensions: createStaticDimensions(resource.Dimensions),
Region: ®ion,
}
filter := createGetMetricStatisticsInput(
data.Dimensions,
&resource.Namespace,
metric,
)
data.Points = clientCloudwatch.get(filter)
if data.Points != nil {
mux.Lock()
cw = append(cw, &data)
mux.Unlock()
}
}()
}
wg.Wait()
return cw
}
func getMetricDataInputLength(job job) int {
var length int
// Why is this here? 120?
if job.Length == 0 {
length = 120
} else {
length = job.Length
}
for _, metric := range job.Metrics {
if metric.Length > length {
length = metric.Length
}
}
return length
}
func getMetricPeriod(job job, metric metric) int64 {
if metric.Period != 0 {
return int64(metric.Period)
}
if job.Period != 0 {
return int64(job.Period)
}
return int64(300)
}
func getMetricDataForQueries(
discoveryJob job,
region string,
tagsOnMetrics exportedTagsOnMetrics,
clientCloudwatch cloudwatchInterface,
resources []*tagsData) []cloudwatchData {
var getMetricDatas []cloudwatchData
// Get the awsDimensions of the job configuration
// Common for all the metrics of the job
commonJobDimensions := getAwsDimensions(discoveryJob)
namespace, _ := getNamespace(discoveryJob.Type)
// For every metric of the job
for _, metric := range discoveryJob.Metrics {
// Get the full list of metrics
// This includes, for this metric the possible combinations
// of dimensions and value of dimensions with data
tagSemaphore <- struct{}{}
fullMetricsList := getFullMetricsList(namespace, metric, clientCloudwatch)
<-tagSemaphore
// For every resource
for _, resource := range resources {
// Creates the dimensions with values for the resource depending on the namespace of the job (p.e. InstanceId=XXXXXXX)
dimensionsWithValue := detectDimensionsByService(resource, fullMetricsList)
// Adds the dimensions with values of that specific metric of the job
dimensionsWithValue = addAdditionalDimensions(dimensionsWithValue, metric.AdditionalDimensions)
// Filter the commonJob Dimensions by the discovered/added dimensions as duplicates cause no metrics to be discovered
commonJobDimensions = filterDimensionsWithoutValueByDimensionsWithValue(commonJobDimensions, dimensionsWithValue)
metricsToAdd := filterMetricsBasedOnDimensionsWithValues(dimensionsWithValue, commonJobDimensions, fullMetricsList)
if metricsToAdd != nil && len(metricsToAdd.Metrics) > 0 {
addCloudwatchTimestamp := discoveryJob.AddCloudwatchTimestamp || metric.AddCloudwatchTimestamp
metricTags := resource.metricTags(tagsOnMetrics)
for _, fetchedMetrics := range metricsToAdd.Metrics {
for _, stats := range metric.Statistics {
id := fmt.Sprintf("id_%d", rand.Int())
name := metric.Name
nilToZero := metric.NilToZero
getMetricDatas = append(getMetricDatas, cloudwatchData{
ID: resource.ID,
MetricID: &id,
Metric: &name,
Service: resource.Service,
Statistics: []string{stats},
NilToZero: &nilToZero,
AddCloudwatchTimestamp: &addCloudwatchTimestamp,
Tags: metricTags,
CustomTags: discoveryJob.CustomTags,
Dimensions: fetchedMetrics.Dimensions,
Region: ®ion,
Period: getMetricPeriod(discoveryJob, metric),
})
}
}
}
}
}
return getMetricDatas
}
func scrapeDiscoveryJobUsingMetricData(
job job,
region string,
tagsOnMetrics exportedTagsOnMetrics,
clientTag tagsInterface,
clientCloudwatch cloudwatchInterface) (resources []*tagsData, cw []*cloudwatchData) {
namespace, err := getNamespace(job.Type)
if err != nil {
log.Fatal(err.Error())
}
// Add the info tags of all the resources
tagSemaphore <- struct{}{}
resources, err = clientTag.get(job, region)
<-tagSemaphore
if err != nil {
log.Printf("Couldn't describe resources for region %s: %s\n", region, err.Error())
return
}
getMetricDatas := getMetricDataForQueries(job, region, tagsOnMetrics, clientCloudwatch, resources)
maxMetricCount := *metricsPerQuery
metricDataLength := len(getMetricDatas)
length := getMetricDataInputLength(job)
partition := int(math.Ceil(float64(metricDataLength) / float64(maxMetricCount)))
mux := &sync.Mutex{}
var wg sync.WaitGroup
wg.Add(partition)
for i := 0; i < metricDataLength; i += maxMetricCount {
go func(i int) {
defer wg.Done()
end := i + maxMetricCount
if end > metricDataLength {
end = metricDataLength
}
filter := createGetMetricDataInput(getMetricDatas[i:end], &namespace, length, job.Delay)
data := clientCloudwatch.getMetricData(filter)
if data != nil {
for _, MetricDataResult := range data.MetricDataResults {
getMetricData, err := findGetMetricDataById(getMetricDatas[i:end], *MetricDataResult.Id)
if err == nil {
if len(MetricDataResult.Values) != 0 {
getMetricData.GetMetricDataPoint = MetricDataResult.Values[0]
getMetricData.GetMetricDataTimestamps = MetricDataResult.Timestamps[0]
}
mux.Lock()
cw = append(cw, &getMetricData)
mux.Unlock()
}
}
}
}(i)
}
wg.Wait()
return resources, cw
}
func (r tagsData) filterThroughTags(filterTags []tag) bool {
tagMatches := 0
for _, resourceTag := range r.Tags {
for _, filterTag := range filterTags {
if resourceTag.Key == filterTag.Key {
r, _ := regexp.Compile(filterTag.Value)
if r.MatchString(resourceTag.Value) {
tagMatches++
}
}
}
}
return tagMatches == len(filterTags)
}
func (r tagsData) metricTags(tagsOnMetrics exportedTagsOnMetrics) []tag {
tags := make([]tag, 0)
for _, tagName := range tagsOnMetrics[*r.Service] {
tag := tag{
Key: tagName,
}
for _, resourceTag := range r.Tags {
if resourceTag.Key == tagName {
tag.Value = resourceTag.Value
break
}
}
// Always add the tag, even if it's empty, to ensure the same labels are present on all metrics for a single service
tags = append(tags, tag)
}
return tags
}