forked from nerdswords/yet-another-cloudwatch-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_cloudwatch.go
734 lines (648 loc) · 23.4 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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
package main
import (
"errors"
"fmt"
"regexp"
"sort"
"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"
log "github.com/sirupsen/logrus"
)
var percentile = regexp.MustCompile(`^p(\d{1,2}(\.\d{0,2})?|100)$`)
type cloudwatchInterface struct {
client cloudwatchiface.CloudWatchAPI
}
type cloudwatchData struct {
ID *string
MetricID *string
Metric *string
Service *string
Statistics []string
Points []*cloudwatch.Datapoint
GetMetricDataPoint *float64
GetMetricDataTimestamps *time.Time
NilToZero *bool
AddCloudwatchTimestamp *bool
CustomTags []tag
Tags []tag
Dimensions []*cloudwatch.Dimension
Region *string
Period int64
}
var labelMap = make(map[string][]string)
func createCloudwatchSession(region *string, roleArn string) *cloudwatch.CloudWatch {
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
maxCloudwatchRetries := 5
config := &aws.Config{Region: region, MaxRetries: &maxCloudwatchRetries}
if *debug {
config.LogLevel = aws.LogLevel(aws.LogDebugWithHTTPBody)
}
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 len(statistics) != 0 {
log.Debug("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.Debug(*output)
return output
}
func findGetMetricDataById(getMetricDatas []cloudwatchData, value string) (cloudwatchData, error) {
var g cloudwatchData
for _, getMetricData := range getMetricDatas {
if *getMetricData.MetricID == value {
return getMetricData, nil
}
}
return g, fmt.Errorf("Metric with id %s not found", value)
}
func createGetMetricDataInput(getMetricData []cloudwatchData, namespace *string, length int, delay int) (output *cloudwatch.GetMetricDataInput) {
var metricsDataQuery []*cloudwatch.MetricDataQuery
for _, data := range getMetricData {
metricStat := &cloudwatch.MetricStat{
Metric: &cloudwatch.Metric{
Dimensions: data.Dimensions,
MetricName: data.Metric,
Namespace: namespace,
},
Period: &data.Period,
Stat: &data.Statistics[0],
}
ReturnData := true
metricsDataQuery = append(metricsDataQuery, &cloudwatch.MetricDataQuery{
Id: data.MetricID,
MetricStat: metricStat,
ReturnData: &ReturnData,
})
}
endTime := time.Now().Add(-time.Duration(delay) * time.Second)
startTime := time.Now().Add(-(time.Duration(length) + time.Duration(delay)) * time.Second)
dataPointOrder := "TimestampDescending"
output = &cloudwatch.GetMetricDataInput{
EndTime: &endTime,
StartTime: &startTime,
MetricDataQueries: metricsDataQuery,
ScanBy: &dataPointOrder,
}
return output
}
func createListMetricsInput(dimensions []*cloudwatch.Dimension, namespace *string, metricsName *string) (output *cloudwatch.ListMetricsInput) {
var dimensionsFilter []*cloudwatch.DimensionFilter
for _, dim := range dimensions {
if dim.Value != nil {
dimensionsFilter = append(dimensionsFilter, &cloudwatch.DimensionFilter{Name: dim.Name, Value: dim.Value})
}
}
output = &cloudwatch.ListMetricsInput{
MetricName: metricsName,
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
}
return output
}
func (iface cloudwatchInterface) get(filter *cloudwatch.GetMetricStatisticsInput) []*cloudwatch.Datapoint {
c := iface.client
log.Debug(filter)
resp, err := c.GetMetricStatistics(filter)
log.Debug(resp)
cloudwatchAPICounter.Inc()
cloudwatchGetMetricStatisticsAPICounter.Inc()
if err != nil {
log.Warningf("Unable to get metric statistics due to %v", err)
return nil
}
return resp.Datapoints
}
func (iface cloudwatchInterface) getMetricData(filter *cloudwatch.GetMetricDataInput) *cloudwatch.GetMetricDataOutput {
c := iface.client
var resp cloudwatch.GetMetricDataOutput
if *debug {
log.Println(filter)
}
// Using the paged version of the function
err := c.GetMetricDataPages(filter,
func(page *cloudwatch.GetMetricDataOutput, lastPage bool) bool {
cloudwatchAPICounter.Inc()
cloudwatchGetMetricDataAPICounter.Inc()
resp.MetricDataResults = append(resp.MetricDataResults, page.MetricDataResults...)
return !lastPage
})
if *debug {
log.Println(resp)
}
if err != nil {
log.Warningf("Unable to get metric data due to %v", err)
return nil
}
return &resp
}
// https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/aws-services-cloudwatch-metrics.html
func getNamespace(service string) (string, error) {
var ns string
var ok bool
namespaces := map[string]string{
"alb": "AWS/ApplicationELB",
"apigateway": "AWS/ApiGateway",
"appsync": "AWS/AppSync",
"asg": "AWS/AutoScaling",
"cf": "AWS/CloudFront",
"dynamodb": "AWS/DynamoDB",
"ebs": "AWS/EBS",
"ec": "AWS/ElastiCache",
"ec2": "AWS/EC2",
"ecs-svc": "AWS/ECS",
"ecs-containerinsights": "ECS/ContainerInsights",
"efs": "AWS/EFS",
"elb": "AWS/ELB",
"emr": "AWS/ElasticMapReduce",
"es": "AWS/ES",
"firehose": "AWS/Firehose",
"fsx": "AWS/FSx",
"kafka": "AWS/Kafka",
"kinesis": "AWS/Kinesis",
"lambda": "AWS/Lambda",
"ngw": "AWS/NATGateway",
"nlb": "AWS/NetworkELB",
"rds": "AWS/RDS",
"redshift": "AWS/Redshift",
"r53r": "AWS/Route53Resolver",
"s3": "AWS/S3",
"sfn": "AWS/States",
"sns": "AWS/SNS",
"sqs": "AWS/SQS",
"tgw": "AWS/TransitGateway",
"tgwa": "AWS/TransitGateway",
"vpn": "AWS/VPN",
"wafv2": "AWS/WAFV2",
}
if ns, ok = namespaces[service]; !ok {
return "", errors.New("Not implemented namespace for cloudwatch metric: " + service)
}
return ns, nil
}
func createStaticDimensions(dimensions []dimension) (output []*cloudwatch.Dimension) {
for _, d := range dimensions {
output = append(output, buildDimension(d.Name, d.Value))
}
return output
}
func filterDimensionsWithoutValueByDimensionsWithValue(
dimensionsWithoutValue []*cloudwatch.Dimension,
dimensionsWithValue []*cloudwatch.Dimension) (dimensions []*cloudwatch.Dimension) {
for _, dimension := range dimensionsWithoutValue {
if !dimensionIsInListWithoutValues(dimension, dimensionsWithValue) {
dimensions = append(dimensions, dimension)
}
}
return dimensions
}
func getAwsDimensions(job job) (dimensions []*cloudwatch.Dimension) {
for _, awsDimension := range job.AwsDimensions {
dimensions = append(dimensions, buildDimensionWithoutValue(awsDimension))
}
return dimensions
}
func getFullMetricsList(namespace string, metric metric, clientCloudwatch cloudwatchInterface) (resp *cloudwatch.ListMetricsOutput) {
c := clientCloudwatch.client
filter := createListMetricsInput(nil, &namespace, &metric.Name)
var res cloudwatch.ListMetricsOutput
err := c.ListMetricsPages(filter,
func(page *cloudwatch.ListMetricsOutput, lastPage bool) bool {
res.Metrics = append(res.Metrics, page.Metrics...)
return !lastPage
})
cloudwatchAPICounter.Inc()
if err != nil {
log.Fatalf("Unable to list metrics due to %v", err)
}
return &res
}
func filterMetricsBasedOnDimensionsWithValues(
dimensionsWithValue []*cloudwatch.Dimension,
dimensionsWithoutValue []*cloudwatch.Dimension,
metricsToFilter *cloudwatch.ListMetricsOutput) *cloudwatch.ListMetricsOutput {
var numberOfDimensions = len(dimensionsWithValue) + len(dimensionsWithoutValue)
var output cloudwatch.ListMetricsOutput
for _, metric := range metricsToFilter.Metrics {
if len(metric.Dimensions) == numberOfDimensions {
shouldAddMetric := true
for _, metricDimension := range metric.Dimensions {
shouldAddMetric = shouldAddMetric &&
(dimensionIsInListWithValues(metricDimension, dimensionsWithValue) ||
dimensionIsInListWithoutValues(metricDimension, dimensionsWithoutValue))
if !shouldAddMetric {
break
}
}
if shouldAddMetric {
output.Metrics = append(output.Metrics, metric)
}
}
}
return &output
}
func dimensionIsInListWithValues(
dimension *cloudwatch.Dimension,
dimensionsList []*cloudwatch.Dimension) bool {
for _, dimensionInList := range dimensionsList {
if *dimension.Name == *dimensionInList.Name &&
*dimension.Value == *dimensionInList.Value {
return true
}
}
return false
}
func dimensionIsInListWithoutValues(
dimension *cloudwatch.Dimension,
dimensionsList []*cloudwatch.Dimension) bool {
for _, dimensionInList := range dimensionsList {
if *dimension.Name == *dimensionInList.Name {
return true
}
}
return false
}
func getDimensionfromMetric(resp *cloudwatch.ListMetricsOutput) []*cloudwatch.Dimension {
for _, metric := range resp.Metrics {
return metric.Dimensions
}
return nil
}
func queryAvailableDimensions(resource string, namespace *string, fullMetricsList *cloudwatch.ListMetricsOutput) (dimensions []*cloudwatch.Dimension) {
if !strings.HasSuffix(*namespace, "ApplicationELB") && !strings.HasSuffix(*namespace, "NetworkELB") {
log.Fatal("Not implemented queryAvailableDimensions: " + *namespace)
return nil
}
if strings.HasPrefix(resource, "targetgroup/") {
dimensions = append(dimensions, buildDimension("TargetGroup", resource))
resp := filterMetricsBasedOnDimensionsWithValues(dimensions, []*cloudwatch.Dimension{buildDimensionWithoutValue("LoadBalancer")}, fullMetricsList)
if resp != nil {
dimensions = getDimensionfromMetric(resp)
}
} else if strings.HasPrefix(resource, "loadbalancer/") || strings.HasPrefix(resource, "net/") || strings.HasPrefix(resource, "app/") {
trimmedDimensionValue := strings.Replace(resource, "loadbalancer/", "", -1)
dimensions = append(dimensions, buildDimension("LoadBalancer", trimmedDimensionValue))
}
return dimensions
}
func detectDimensionsByService(resource *tagsData, fullMetricsList *cloudwatch.ListMetricsOutput) (dimensions []*cloudwatch.Dimension) {
resourceArn := *resource.ID
service := *resource.Service
arnParsed, err := arn.Parse(resourceArn)
if err != nil && service != "tgwa" {
log.Warningf("Unable to parse ARN (%s) on %s due to %v", resourceArn, service, err)
return dimensions
}
type baseParams struct {
Key string
Prefix string
}
baseDimension := map[string]baseParams{
"appsync": {Key: "GraphQLAPIId", Prefix: "apis/"},
"asg": {Key: "AutoScalingGroupName", Prefix: "autoScalingGroupName/"},
"dynamodb": {Key: "TableName", Prefix: "table/"},
"ebs": {Key: "VolumeId", Prefix: "volume/"},
"ec": {Key: "CacheClusterId", Prefix: "cluster:"},
"ec2": {Key: "InstanceId", Prefix: "instance/"},
"efs": {Key: "FileSystemId", Prefix: "file-system/"},
"elb": {Key: "LoadBalancerName", Prefix: "loadbalancer/"},
"emr": {Key: "JobFlowId", Prefix: "cluster/"},
"firehose": {Key: "DeliveryStreamName", Prefix: "deliverystream/"},
"fsx": {Key: "FileSystemId", Prefix: "file-system/"},
"kinesis": {Key: "StreamName", Prefix: "stream/"},
"lambda": {Key: "FunctionName", Prefix: "function:"},
"ngw": {Key: "NatGatewayId", Prefix: "natgateway/"},
"rds": {Key: "DBInstanceIdentifier", Prefix: "db:"},
"redshift": {Key: "ClusterIdentifier", Prefix: "cluster:"},
"r53r": {Key: "EndpointId", Prefix: "resolver-endpoint/"},
"s3": {Key: "BucketName", Prefix: ""},
"sns": {Key: "TopicName", Prefix: ""},
"sqs": {Key: "QueueName", Prefix: ""},
"tgw": {Key: "TransitGateway", Prefix: "transit-gateway/"},
"vpn": {Key: "VpnId", Prefix: "vpn-connection/"},
}
if params, ok := baseDimension[service]; ok {
return buildBaseDimension(arnParsed.Resource, params.Key, params.Prefix)
}
switch service {
case "alb", "nlb":
namespace, _ := getNamespace(service)
dimensions = queryAvailableDimensions(arnParsed.Resource, &namespace, fullMetricsList)
case "apigateway":
// https://docs.aws.amazon.com/apigateway/latest/developerguide/arn-format-reference.html
dimensions = buildBaseDimension(*resource.Matcher, "ApiName", "")
gatewayType := strings.Split(arnParsed.Resource, "/")[1]
switch gatewayType {
case "restapis", "apis":
// /stages/stage-name
stageRegex := regexp.MustCompile(`stages/(\S+)`)
stageMatches := stageRegex.FindStringSubmatch(arnParsed.Resource)
if len(stageMatches) > 0 {
dimensions = append(dimensions, buildDimension("Stage", stageMatches[1]))
}
// /resources/resource-id
resourceRegex := regexp.MustCompile(`resources/(\S+)`)
resourceMatches := resourceRegex.FindStringSubmatch(arnParsed.Resource)
if len(resourceMatches) > 0 {
dimensions = append(dimensions, buildDimension("Resources", resourceMatches[1]))
}
// /methods/http-method
// only for restapis
if gatewayType == "restapis" {
methodRegex := regexp.MustCompile(`methods/(\S+)`)
methodMatches := methodRegex.FindStringSubmatch(arnParsed.Resource)
if len(methodMatches) > 0 {
dimensions = append(dimensions, buildDimension("Method", methodMatches[1]))
}
}
}
case "cf":
dimensions = buildBaseDimension(arnParsed.Resource, "DistributionId", "distribution/")
dimensions = append(dimensions, buildDimension("Region", "Global"))
case "ecs-svc", "ecs-containerinsights":
parsedResource := strings.Split(arnParsed.Resource, "/")
if parsedResource[0] == "service" {
dimensions = append(dimensions, buildDimension("ClusterName", parsedResource[1]), buildDimension("ServiceName", parsedResource[2]))
}
if parsedResource[0] == "cluster" {
dimensions = append(dimensions, buildDimension("ClusterName", parsedResource[1]))
}
case "es":
dimensions = buildBaseDimension(arnParsed.Resource, "DomainName", "domain/")
dimensions = append(dimensions, buildDimension("ClientId", arnParsed.AccountID))
case "sfn":
// The value of StateMachineArn returned is the Name, not the ARN
// We are setting the value to the ARN in order to correlate dimensions with metric values
// (StateMachineArn will be set back to the name later, once all the filtering is complete)
// https://docs.aws.amazon.com/step-functions/latest/dg/procedure-cw-metrics.html
dimensions = append(dimensions, buildDimension("StateMachineArn", resourceArn))
case "tgwa":
parsedResource := strings.Split(resourceArn, "/")
dimensions = append(dimensions, buildDimension("TransitGateway", parsedResource[0]), buildDimension("TransitGatewayAttachment", parsedResource[1]))
case "kafka":
cluster := strings.Split(arnParsed.Resource, "/")[1]
dimensions = append(dimensions, buildDimension("Cluster Name", cluster))
case "wafv2":
aclId := strings.Split(resourceArn, "/")[2]
dimensions = append(dimensions, buildDimension("WebACL", aclId))
default:
log.Fatal("Not implemented cloudwatch metric: " + service)
}
return dimensions
}
func addAdditionalDimensions(startingDimensions []*cloudwatch.Dimension, additionalDimensions []dimension) (dimensions []*cloudwatch.Dimension) {
// Copy startingDimensions before appending additionalDimensions, since append(x, ...) can modify x
dimensions = append(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 buildDimensionWithoutValue(key string) *cloudwatch.Dimension {
dimension := cloudwatch.Dimension{
Name: &key,
}
return &dimension
}
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 suffixName string
if *serviceName == "alb" || *serviceName == "nlb" {
var albSuffix, tgSuffix string
for _, dimension := range dimensions {
if *dimension.Name == "TargetGroup" {
tgSuffix = "tg"
}
if *dimension.Name == "LoadBalancer" {
albSuffix = *serviceName
}
}
if albSuffix != "" && tgSuffix != "" {
return albSuffix + "_" + tgSuffix
} else if albSuffix == "" && tgSuffix != "" {
return tgSuffix
}
}
if *serviceName == "elb" {
for _, dimension := range dimensions {
if *dimension.Name == "AvailabilityZone" {
suffixName = "_az"
}
}
}
return promString(*serviceName) + suffixName
}
func getStateMachineNameFromArn(resourceArn string) string {
arnParsed, err := arn.Parse(resourceArn)
if err != nil {
log.Warningf("Unable to parse ARN (%s) due to %v", resourceArn, err)
return ""
}
parsedResource := strings.Split(arnParsed.Resource, ":")
return parsedResource[1]
}
func createPrometheusLabels(cwd *cloudwatchData) map[string]string {
labels := make(map[string]string)
labels["name"] = *cwd.ID
labels["region"] = *cwd.Region
// Inject the sfn name back as a label
switch *cwd.Service {
case "sfn":
labels["dimension_"+promStringTag("StateMachineArn")] = getStateMachineNameFromArn(*cwd.ID)
}
for _, dimension := range cwd.Dimensions {
labels["dimension_"+promStringTag(*dimension.Name)] = *dimension.Value
}
for _, label := range cwd.CustomTags {
labels["custom_tag_"+promStringTag(label.Key)] = label.Value
}
for _, tag := range cwd.Tags {
labels["tag_"+promStringTag(tag.Key)] = tag.Value
}
return labels
}
func recordLabelsForMetric(metricName string, promLabels map[string]string) {
var workingLabelsCopy []string
if _, ok := labelMap[metricName]; ok {
copy(labelMap[metricName], workingLabelsCopy)
}
for k, _ := range promLabels {
workingLabelsCopy = append(workingLabelsCopy, k)
}
sort.Strings(workingLabelsCopy)
j := 0
for i := 1; i < len(workingLabelsCopy); i++ {
if workingLabelsCopy[j] == workingLabelsCopy[i] {
continue
}
j++
workingLabelsCopy[j] = workingLabelsCopy[i]
}
labelMap[metricName] = workingLabelsCopy[:j+1]
}
func ensureLabelConsistencyForMetrics(metrics []*PrometheusMetric) []*PrometheusMetric {
var updatedMetrics []*PrometheusMetric
for _, prometheusMetric := range metrics {
metricName := prometheusMetric.name
metricLabels := prometheusMetric.labels
consistentMetricLabels := make(map[string]string)
for _, recordedLabel := range labelMap[*metricName] {
if value, ok := metricLabels[recordedLabel]; ok {
consistentMetricLabels[recordedLabel] = value
} else {
consistentMetricLabels[recordedLabel] = ""
}
}
prometheusMetric.labels = consistentMetricLabels
updatedMetrics = append(updatedMetrics, prometheusMetric)
}
return updatedMetrics
}
func sortByTimestamp(datapoints []*cloudwatch.Datapoint) []*cloudwatch.Datapoint {
sort.Slice(datapoints, func(i, j int) bool {
jTimestamp := *datapoints[j].Timestamp
return datapoints[i].Timestamp.Before(jTimestamp)
})
return datapoints
}
func getDatapoint(cwd *cloudwatchData, statistic string) (*float64, time.Time) {
if cwd.GetMetricDataPoint != nil {
return cwd.GetMetricDataPoint, *cwd.GetMetricDataTimestamps
}
var averageDataPoints []*cloudwatch.Datapoint
// sorting by timestamps so we can consistently export the most updated datapoint
// assuming Timestamp field in cloudwatch.Datapoint struct is never nil
for _, datapoint := range sortByTimestamp(cwd.Points) {
switch {
case statistic == "Maximum":
if datapoint.Maximum != nil {
return datapoint.Maximum, *datapoint.Timestamp
}
case statistic == "Minimum":
if datapoint.Minimum != nil {
return datapoint.Minimum, *datapoint.Timestamp
}
case statistic == "Sum":
if datapoint.Sum != nil {
return datapoint.Sum, *datapoint.Timestamp
}
case statistic == "SampleCount":
if datapoint.SampleCount != nil {
return datapoint.SampleCount, *datapoint.Timestamp
}
case statistic == "Average":
if datapoint.Average != nil {
averageDataPoints = append(averageDataPoints, datapoint)
}
case percentile.MatchString(statistic):
if data, ok := datapoint.ExtendedStatistics[statistic]; ok {
return data, *datapoint.Timestamp
}
default:
log.Fatal("Not implemented statistics: " + statistic)
}
}
if len(averageDataPoints) > 0 {
var total float64
var timestamp time.Time
for _, p := range averageDataPoints {
if p.Timestamp.After(timestamp) {
timestamp = *p.Timestamp
}
total += *p.Average
}
average := total / float64(len(averageDataPoints))
return &average, timestamp
}
return nil, time.Time{}
}
func migrateCloudwatchToPrometheus(cwd []*cloudwatchData) []*PrometheusMetric {
output := make([]*PrometheusMetric, 0)
for _, c := range cwd {
for _, statistic := range c.Statistics {
includeTimestamp := *c.AddCloudwatchTimestamp
exportedDatapoint, timestamp := getDatapoint(c, statistic)
if exportedDatapoint == nil && *c.NilToZero {
var zero float64 = 0
exportedDatapoint = &zero
includeTimestamp = false
}
serviceName := fixServiceName(c.Service, c.Dimensions)
name := "aws_" + serviceName + "_" + strings.ToLower(promString(*c.Metric)) + "_" + strings.ToLower(promString(statistic))
if exportedDatapoint != nil {
promLabels := createPrometheusLabels(c)
recordLabelsForMetric(name, promLabels)
p := PrometheusMetric{
name: &name,
labels: promLabels,
value: exportedDatapoint,
timestamp: timestamp,
includeTimestamp: includeTimestamp,
}
output = append(output, &p)
}
}
}
return output
}