-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaws_cloudwatch.go
407 lines (352 loc) · 11.5 KB
/
aws_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
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package main
import (
"fmt"
"log"
"regexp"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/credentials/stscreds"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/aws/aws-sdk-go/service/cloudwatch/cloudwatchiface"
)
var percentile = regexp.MustCompile(`^p(\d{1,2}(\.\d{0,2})?|100)$`)
type cloudwatchInterface struct {
client cloudwatchiface.CloudWatchAPI
}
type cloudwatchData struct {
ID *string
Metric *string
Service *string
Statistics []string
Points []*cloudwatch.Datapoint
NilToZero *bool
AddCloudwatchTimestamp *bool
CustomTags []tag
Tags []tag
Dimensions []*cloudwatch.Dimension
Region *string
}
func createCloudwatchSession(region *string, roleArn string) *cloudwatch.CloudWatch {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
config := &aws.Config{Region: region}
if roleArn != "" {
config.Credentials = stscreds.NewCredentials(sess, roleArn)
}
return cloudwatch.New(sess, config)
}
func createGetMetricStatisticsInput(dimensions []*cloudwatch.Dimension, namespace *string, metric metric) (output *cloudwatch.GetMetricStatisticsInput) {
period := int64(metric.Period)
length := metric.Length
delay := metric.Delay
endTime := time.Now().Add(-time.Duration(delay) * time.Second)
startTime := time.Now().Add(-(time.Duration(length) + time.Duration(delay)) * time.Second)
var statistics []*string
var extendedStatistics []*string
for _, statistic := range metric.Statistics {
if percentile.MatchString(statistic) {
extendedStatistics = append(extendedStatistics, aws.String(statistic))
} else {
statistics = append(statistics, aws.String(statistic))
}
}
output = &cloudwatch.GetMetricStatisticsInput{
Dimensions: dimensions,
Namespace: namespace,
StartTime: &startTime,
EndTime: &endTime,
Period: &period,
MetricName: &metric.Name,
Statistics: statistics,
ExtendedStatistics: extendedStatistics,
}
if *debug {
if len(statistics) != 0 {
log.Println("CLI helper - " +
"aws cloudwatch get-metric-statistics" +
" --metric-name " + metric.Name +
" --dimensions " + dimensionsToCliString(dimensions) +
" --namespace " + *namespace +
" --statistics " + *statistics[0] +
" --period " + strconv.FormatInt(period, 10) +
" --start-time " + startTime.Format(time.RFC3339) +
" --end-time " + endTime.Format(time.RFC3339))
}
log.Println(*output)
}
return output
}
func createListMetricsInput(dimensions []*cloudwatch.Dimension, namespace *string) (output *cloudwatch.ListMetricsInput) {
var dimensionsFilter []*cloudwatch.DimensionFilter
for _, dim := range dimensions {
dimensionsFilter = append(dimensionsFilter, &cloudwatch.DimensionFilter{Name: dim.Name, Value: dim.Value})
}
output = &cloudwatch.ListMetricsInput{
MetricName: nil,
Dimensions: dimensionsFilter,
Namespace: namespace,
NextToken: nil,
}
return output
}
func dimensionsToCliString(dimensions []*cloudwatch.Dimension) (output string) {
for _, dim := range dimensions {
output = output + "Name=" + *dim.Name + ",Value=" + *dim.Value
fmt.Println(output)
}
return output
}
func (iface cloudwatchInterface) get(filter *cloudwatch.GetMetricStatisticsInput) []*cloudwatch.Datapoint {
c := iface.client
if *debug {
log.Println(filter)
}
resp, err := c.GetMetricStatistics(filter)
if *debug {
log.Println(resp)
}
cloudwatchAPICounter.Inc()
if err != nil {
panic(err)
}
return resp.Datapoints
}
func getNamespace(service *string) *string {
var ns string
switch *service {
case "ec2":
ns = "AWS/EC2"
case "elb":
ns = "AWS/ELB"
case "alb":
ns = "AWS/ApplicationELB"
case "rds":
ns = "AWS/RDS"
case "ec":
ns = "AWS/ElastiCache"
case "es":
ns = "AWS/ES"
case "s3":
ns = "AWS/S3"
case "efs":
ns = "AWS/EFS"
case "ebs":
ns = "AWS/EBS"
case "vpn":
ns = "AWS/VPN"
case "lambda":
ns = "AWS/Lambda"
case "kinesis":
ns = "AWS/Kinesis"
case "dynamodb":
ns = "AWS/DynamoDB"
case "emr":
ns = "AWS/ElasticMapReduce"
case "asg":
ns = "AWS/AutoScaling"
default:
log.Fatal("Not implemented namespace for cloudwatch metric: " + *service)
}
return &ns
}
func createStaticDimensions(dimensions []dimension) (output []*cloudwatch.Dimension) {
for _, d := range dimensions {
output = append(output, buildDimension(d.Name, d.Value))
}
return output
}
func getDimensionValueForName(name string, resp *cloudwatch.ListMetricsOutput) (value *string) {
for _, metric := range resp.Metrics {
for _, dim := range metric.Dimensions {
if strings.Compare(*dim.Name, name) == 0 {
return dim.Value
}
}
}
return nil
}
func getResourceValue(resourceName string, dimensions []*cloudwatch.Dimension, namespace *string, clientCloudwatch cloudwatchInterface) (dimensionResourceName *string) {
c := clientCloudwatch.client
filter := createListMetricsInput(dimensions, namespace)
req, resp := c.ListMetricsRequest(filter)
err := req.Send()
if err != nil {
panic(err)
}
cloudwatchAPICounter.Inc()
return getDimensionValueForName(resourceName, resp)
}
func queryAvailableDimensions(resource string, namespace *string, clientCloudwatch cloudwatchInterface) (dimensions []*cloudwatch.Dimension) {
if !strings.HasSuffix(*namespace, "ApplicationELB") {
log.Fatal("Not implemented queryAvailableDimensions: " + *namespace)
return nil
}
if strings.HasPrefix(resource, "targetgroup/") {
dimensions = append(dimensions, buildDimension("TargetGroup", resource))
loadBalancerName := getResourceValue("LoadBalancer", dimensions, namespace, clientCloudwatch)
if loadBalancerName != nil {
dimensions = append(dimensions, buildDimension("LoadBalancer", *loadBalancerName))
}
} else if strings.HasPrefix(resource, "loadbalancer/") || strings.HasPrefix(resource, "app/") {
trimmedDimensionValue := strings.Replace(resource, "loadbalancer/", "", -1)
dimensions = append(dimensions, buildDimension("LoadBalancer", trimmedDimensionValue))
}
return dimensions
}
func detectDimensionsByService(service *string, resourceArn *string, clientCloudwatch cloudwatchInterface) (dimensions []*cloudwatch.Dimension) {
arnParsed, err := arn.Parse(*resourceArn)
if err != nil {
panic(err)
}
switch *service {
case "ec2":
dimensions = buildBaseDimension(arnParsed.Resource, "InstanceId", "instance/")
case "elb":
dimensions = buildBaseDimension(arnParsed.Resource, "LoadBalancerName", "loadbalancer/")
case "alb":
dimensions = queryAvailableDimensions(arnParsed.Resource, getNamespace(service), clientCloudwatch)
case "rds":
dimensions = buildBaseDimension(arnParsed.Resource, "DBInstanceIdentifier", "db:")
case "ec":
dimensions = buildBaseDimension(arnParsed.Resource, "CacheClusterId", "cluster:")
case "es":
dimensions = buildBaseDimension(arnParsed.Resource, "DomainName", "domain/")
dimensions = append(dimensions, buildDimension("ClientId", arnParsed.AccountID))
case "s3":
dimensions = buildBaseDimension(arnParsed.Resource, "BucketName", "")
dimensions = append(dimensions, buildDimension("StorageType", "AllStorageTypes"))
case "efs":
dimensions = buildBaseDimension(arnParsed.Resource, "FileSystemId", "file-system/")
case "ebs":
dimensions = buildBaseDimension(arnParsed.Resource, "VolumeId", "volume/")
case "vpn":
dimensions = buildBaseDimension(arnParsed.Resource, "VpnId", "vpn-connection/")
case "lambda":
dimensions = buildBaseDimension(arnParsed.Resource, "FunctionName", "function:")
case "kinesis":
dimensions = buildBaseDimension(arnParsed.Resource, "StreamName", "stream/")
case "dynamodb":
dimensions = buildBaseDimension(arnParsed.Resource, "TableName", "table/")
case "emr":
dimensions = buildBaseDimension(arnParsed.Resource, "JobFlowId", "cluster/")
case "asg":
dimensions = buildBaseDimension(arnParsed.Resource, "AutoScalingGroupName", "autoScalingGroupName/")
default:
log.Fatal("Not implemented cloudwatch metric: " + *service)
}
return dimensions
}
func addAdditionalDimensions(startingDimensions []*cloudwatch.Dimension, additionalDimensions []dimension) (dimensions []*cloudwatch.Dimension) {
dimensions = startingDimensions
for _, dimension := range additionalDimensions {
dimensions = append(dimensions, buildDimension(dimension.Name, dimension.Value))
}
return dimensions
}
func buildBaseDimension(identifier string, dimensionKey string, prefix string) (dimensions []*cloudwatch.Dimension) {
helper := strings.TrimPrefix(identifier, prefix)
dimensions = append(dimensions, buildDimension(dimensionKey, helper))
return dimensions
}
func buildDimension(key string, value string) *cloudwatch.Dimension {
dimension := cloudwatch.Dimension{
Name: &key,
Value: &value,
}
return &dimension
}
func fixServiceName(serviceName *string, dimensions []*cloudwatch.Dimension) string {
var targetGroup string
if *serviceName == "alb" {
for _, dimension := range dimensions {
if *dimension.Name == "TargetGroup" {
targetGroup = "tg"
}
}
}
return strings.ToLower(promString(*serviceName)) + targetGroup
}
func migrateCloudwatchToPrometheus(cwd []*cloudwatchData) []*PrometheusMetric {
output := make([]*PrometheusMetric, 0)
for _, c := range cwd {
for _, statistic := range c.Statistics {
name := "aws_" + fixServiceName(c.Service, c.Dimensions) + "_" + strings.ToLower(promString(*c.Metric)) + "_" + strings.ToLower(promString(statistic))
var points []*float64
var timestamp time.Time
for _, point := range c.Points {
if point.Timestamp != nil && timestamp.Before(*point.Timestamp) {
timestamp = *point.Timestamp
}
switch {
case statistic == "Maximum":
if point.Maximum != nil {
points = append(points, point.Maximum)
}
case statistic == "Minimum":
if point.Minimum != nil {
points = append(points, point.Minimum)
}
case statistic == "Sum":
if point.Sum != nil {
points = append(points, point.Sum)
}
case statistic == "Average":
if point.Average != nil {
points = append(points, point.Average)
}
case percentile.MatchString(statistic):
if data, ok := point.ExtendedStatistics[statistic]; ok {
points = append(points, data)
}
default:
log.Fatal("Not implemented statistics: " + statistic)
}
}
if len(points) == 0 {
if *c.NilToZero {
helper := float64(0)
sliceHelper := []*float64{&helper}
points = sliceHelper
}
}
if len(points) > 0 {
promLabels := make(map[string]string)
promLabels["name"] = *c.ID
for _, label := range c.CustomTags {
promLabels["custom_tag_"+label.Key] = label.Value
}
for _, tag := range c.Tags {
promLabels["tag_"+promStringTag(tag.Key)] = tag.Value
}
for _, dimension := range c.Dimensions {
promLabels["dimension_"+promStringTag(*dimension.Name)] = *dimension.Value
}
promLabels["region"] = *c.Region
var value float64
if statistic == "Average" {
var total float64
for _, p := range points {
total += *p
}
value = total / float64(len(points))
} else {
value = *points[len(points)-1]
}
p := PrometheusMetric{
name: &name,
labels: promLabels,
value: &value,
timestamp: timestamp,
includeTimestamp: *c.AddCloudwatchTimestamp,
}
output = append(output, &p)
}
}
}
return output
}