/
cloudwatch.go
680 lines (595 loc) · 18.8 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
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
// forked from telegraf https://github.com/influxdata/telegraf/blob/master/plugins/inputs/cloudwatch/sample.conf
package cloudwatch
import (
"context"
_ "embed"
"fmt"
"log"
"net"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/aws/ratelimit"
"github.com/aws/aws-sdk-go-v2/aws/retry"
cwClient "github.com/aws/aws-sdk-go-v2/service/cloudwatch"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
"github.com/F1997/categraf/config"
"github.com/F1997/categraf/inputs"
internalaws "github.com/F1997/categraf/pkg/aws"
"github.com/F1997/categraf/pkg/filter"
"github.com/F1997/categraf/pkg/limiter"
internalProxy "github.com/F1997/categraf/pkg/proxy"
"github.com/F1997/categraf/pkg/stringx"
internalTypes "github.com/F1997/categraf/types"
internalMetric "github.com/F1997/categraf/types/metric"
)
//go:embed sample.conf
var sampleConfig string
const inputName = "cloudwatch"
const (
StatisticAverage = "Average"
StatisticMaximum = "Maximum"
StatisticMinimum = "Minimum"
StatisticSum = "Sum"
StatisticSampleCount = "SampleCount"
)
type (
CloudWatch struct {
config.PluginConfig
Instances []*Instance `toml:"instances"`
}
// CloudWatch contains the configuration and cache for the cloudwatch plugin.
Instance struct {
config.InstanceConfig
StatisticExclude []string `toml:"statistic_exclude"`
StatisticInclude []string `toml:"statistic_include"`
Timeout config.Duration `toml:"timeout"`
internalProxy.HTTPProxy
Period config.Duration `toml:"period"`
Delay config.Duration `toml:"delay"`
Namespace string `toml:"namespace" deprecated:"1.25.0;use 'namespaces' instead"`
Namespaces []string `toml:"namespaces"`
Metrics []*Metric `toml:"metrics"`
CacheTTL config.Duration `toml:"cache_ttl"`
RateLimit int `toml:"ratelimit"`
SdkRateLimitTokens int `toml:"sdk_ratelimit_tokens"`
RetryMaxAttempts int `toml:"retry_max_attempts"`
DebugMode []string `toml:"debug_mode"`
RecentlyActive string `toml:"recently_active"`
BatchSize int `toml:"batch_size"`
client cloudwatchClient
statFilter filter.Filter
metricCache *metricCache
queryDimensions map[string]*map[string]string
windowStart time.Time
windowEnd time.Time
internalaws.CredentialConfig
}
)
// Metric defines a simplified Cloudwatch metric.
type Metric struct {
StatisticExclude *[]string `toml:"statistic_exclude"`
StatisticInclude *[]string `toml:"statistic_include"`
MetricNames []string `toml:"names"`
Dimensions []*Dimension `toml:"dimensions"`
}
// Dimension defines a simplified Cloudwatch dimension (provides metric filtering).
type Dimension struct {
Name string `toml:"name"`
Value string `toml:"value"`
valueMatcher filter.Filter
}
// metricCache caches metrics, their filters, and generated queries.
type metricCache struct {
ttl time.Duration
built time.Time
metrics []filteredMetric
queries map[string][]types.MetricDataQuery
}
type cloudwatchClient interface {
ListMetrics(context.Context, *cwClient.ListMetricsInput, ...func(*cwClient.Options)) (*cwClient.ListMetricsOutput, error)
GetMetricData(context.Context, *cwClient.GetMetricDataInput, ...func(*cwClient.Options)) (*cwClient.GetMetricDataOutput, error)
}
func (*CloudWatch) SampleConfig() string {
return sampleConfig
}
func (ins *Instance) Init() error {
if ins.BatchSize == 0 {
ins.BatchSize = 500
}
if ins.RateLimit == 0 {
ins.RateLimit = 25
}
if ins.CacheTTL == 0 {
ins.CacheTTL = config.Duration(time.Hour)
}
if ins.Timeout == 0 {
ins.Timeout = config.Duration(time.Second * 5)
}
if len(ins.Namespace) != 0 {
ins.Namespaces = append(ins.Namespaces, ins.Namespace)
}
if ins.RetryMaxAttempts <= 0 {
ins.RetryMaxAttempts = 20
}
if ins.SdkRateLimitTokens <= 0 {
ins.SdkRateLimitTokens = 1000000
}
if len(ins.Namespaces) == 0 {
return internalTypes.ErrInstancesEmpty
}
err := ins.initializeCloudWatch()
if err != nil {
return err
}
// Set config level filter (won't change throughout life of plugin).
ins.statFilter, err = filter.NewIncludeExcludeFilter(ins.StatisticInclude, ins.StatisticExclude)
if err != nil {
return err
}
return nil
}
func (cw *CloudWatch) GetInstances() []inputs.Instance {
ret := make([]inputs.Instance, len(cw.Instances))
for i := 0; i < len(cw.Instances); i++ {
ret[i] = cw.Instances[i]
}
return ret
}
// Gather takes in an accumulator and adds the metrics that the Input
// gathers. This is called every "interval".
func (ins *Instance) Gather(slist *internalTypes.SampleList) {
filteredMetrics, err := getFilteredMetrics(ins)
if err != nil {
log.Println("E! filter metrics error,", err)
return
}
ins.updateWindow(time.Now())
// Get all of the possible queries so we can send groups of 100.
queries := ins.getDataQueries(filteredMetrics)
if len(queries) == 0 {
log.Printf("E! data queries length is 0, namespaces:%+v", ins.Namespaces)
return
}
// Limit concurrency or we can easily exhaust user connection limit.
// See cloudwatch API request limits:
// http://docs.aws.amazon.com/AmazonCloudWatch/latest/DeveloperGuide/cloudwatch_limits.html
lmtr := limiter.NewRateLimiter(ins.RateLimit, time.Second)
defer lmtr.Stop()
wg := sync.WaitGroup{}
rLock := sync.Mutex{}
results := map[string][]types.MetricDataResult{}
for namespace, namespacedQueries := range queries {
var batches [][]types.MetricDataQuery
for ins.BatchSize < len(namespacedQueries) {
namespacedQueries, batches = namespacedQueries[ins.BatchSize:], append(batches, namespacedQueries[0:ins.BatchSize:ins.BatchSize])
}
batches = append(batches, namespacedQueries)
for i := range batches {
wg.Add(1)
<-lmtr.C
go func(n string, inm []types.MetricDataQuery) {
defer wg.Done()
result, err := ins.gatherMetrics(ins.getDataInputs(inm))
if err != nil {
log.Printf("E! gather namespace:%s error:%s", n, err)
return
}
rLock.Lock()
results[n] = append(results[n], result...)
rLock.Unlock()
}(namespace, batches[i])
}
}
wg.Wait()
err = ins.aggregateMetrics(slist, results)
if err != nil {
log.Println("E! aggregate metrics error,", err)
}
}
func (ins *Instance) initializeCloudWatch() error {
proxy, err := ins.HTTPProxy.Proxy()
if err != nil {
return err
}
cfg, err := ins.CredentialConfig.Credentials()
if err != nil {
return err
}
ins.client = cwClient.NewFromConfig(cfg, func(options *cwClient.Options) {
// Disable logging
options.ClientLogMode = 0
if config.Config.DebugMode {
for _, mode := range ins.DebugMode {
switch mode {
case "LogRequest":
options.ClientLogMode |= aws.LogRequest
case "LogRequestWithBody":
options.ClientLogMode |= aws.LogRequestWithBody
case "LogRequestEventMessage":
options.ClientLogMode |= aws.LogRequestEventMessage
case "LogRetries":
options.ClientLogMode |= aws.LogRetries
case "LogResponse":
options.ClientLogMode |= aws.LogResponse
case "LogResponseWithBody":
options.ClientLogMode |= aws.LogResponseWithBody
case "LogResponseEventMessage":
options.ClientLogMode |= aws.LogResponseEventMessage
case "LogSigning":
options.ClientLogMode |= aws.LogSigning
case "LogDeprecatedUsage":
options.ClientLogMode |= aws.LogDeprecatedUsage
}
}
}
options.Retryer = retry.NewStandard(func(options *retry.StandardOptions) {
options.MaxAttempts = ins.RetryMaxAttempts
options.RateLimiter = ratelimit.NewTokenRateLimit(uint(ins.SdkRateLimitTokens))
})
options.HTTPClient = &http.Client{
// use values from DefaultTransport
Transport: &http.Transport{
Proxy: proxy,
DialContext: (&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
DualStack: true,
}).DialContext,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
Timeout: time.Duration(ins.Timeout),
}
})
// Initialize regex matchers for each Dimension value.
for _, m := range ins.Metrics {
for _, dimension := range m.Dimensions {
matcher, err := filter.NewIncludeExcludeFilter([]string{dimension.Value}, nil)
if err != nil {
return err
}
dimension.valueMatcher = matcher
}
}
return nil
}
type filteredMetric struct {
metrics []types.Metric
statFilter filter.Filter
}
// getFilteredMetrics returns metrics specified in the config file or metrics listed from Cloudwatch.
func getFilteredMetrics(c *Instance) ([]filteredMetric, error) {
if c.metricCache != nil && c.metricCache.isValid() {
if config.Config.DebugMode {
log.Printf("D! use filtered metrics cache for namespace %+v", c.Namespaces)
}
return c.metricCache.metrics, nil
}
fMetrics := []filteredMetric{}
// check for provided metric filter
if c.Metrics != nil {
for _, m := range c.Metrics {
metrics := []types.Metric{}
if !hasWildcard(m.Dimensions) {
dimensions := make([]types.Dimension, 0, len(m.Dimensions))
for _, d := range m.Dimensions {
dimensions = append(dimensions, types.Dimension{
Name: aws.String(d.Name),
Value: aws.String(d.Value),
})
}
for _, name := range m.MetricNames {
for _, namespace := range c.Namespaces {
metrics = append(metrics, types.Metric{
Namespace: aws.String(namespace),
MetricName: aws.String(name),
Dimensions: dimensions,
})
}
}
} else {
allMetrics := c.fetchNamespaceMetrics()
for _, name := range m.MetricNames {
for _, metric := range allMetrics {
if isSelected(name, metric, m.Dimensions) {
for _, namespace := range c.Namespaces {
metrics = append(metrics, types.Metric{
Namespace: aws.String(namespace),
MetricName: aws.String(name),
Dimensions: metric.Dimensions,
})
}
}
}
}
}
if m.StatisticExclude == nil {
m.StatisticExclude = &c.StatisticExclude
}
if m.StatisticInclude == nil {
m.StatisticInclude = &c.StatisticInclude
}
statFilter, err := filter.NewIncludeExcludeFilter(*m.StatisticInclude, *m.StatisticExclude)
if err != nil {
return nil, err
}
fMetrics = append(fMetrics, filteredMetric{
metrics: metrics,
statFilter: statFilter,
})
}
} else {
metrics := c.fetchNamespaceMetrics()
fMetrics = []filteredMetric{
{
metrics: metrics,
statFilter: c.statFilter,
},
}
}
c.metricCache = &metricCache{
metrics: fMetrics,
built: time.Now(),
ttl: time.Duration(c.CacheTTL),
}
return fMetrics, nil
}
// fetchNamespaceMetrics retrieves available metrics for a given CloudWatch namespace.
func (ins *Instance) fetchNamespaceMetrics() []types.Metric {
metrics := []types.Metric{}
for _, namespace := range ins.Namespaces {
params := &cwClient.ListMetricsInput{
Dimensions: []types.DimensionFilter{},
Namespace: aws.String(namespace),
}
if ins.RecentlyActive == "PT3H" {
params.RecentlyActive = types.RecentlyActivePt3h
}
for {
resp, err := ins.client.ListMetrics(context.Background(), params)
if err != nil {
log.Printf("E! failed to list metrics with namespace %s: %v", namespace, err)
// skip problem namespace on error and continue to next namespace
break
}
metrics = append(metrics, resp.Metrics...)
if resp.NextToken == nil {
break
}
params.NextToken = resp.NextToken
}
}
return metrics
}
func (ins *Instance) updateWindow(relativeTo time.Time) {
windowEnd := relativeTo.Add(-time.Duration(ins.Delay))
if ins.windowEnd.IsZero() {
// this is the first run, no window info, so just get a single period
ins.windowStart = windowEnd.Add(-time.Duration(ins.Period))
} else {
// subsequent window, start where last window left off
ins.windowStart = ins.windowEnd
}
ins.windowEnd = windowEnd
}
// getDataQueries gets all of the possible queries so we can maximize the request payload.
func (ins *Instance) getDataQueries(filteredMetrics []filteredMetric) map[string][]types.MetricDataQuery {
if ins.metricCache != nil && ins.metricCache.queries != nil && ins.metricCache.isValid() {
return ins.metricCache.queries
}
ins.queryDimensions = map[string]*map[string]string{}
dataQueries := map[string][]types.MetricDataQuery{}
for i, filtered := range filteredMetrics {
for j, metric := range filtered.metrics {
id := strconv.Itoa(j) + "_" + strconv.Itoa(i)
dimension := ctod(metric.Dimensions)
if filtered.statFilter.Match("average") {
ins.queryDimensions["average_"+id] = dimension
dataQueries[*metric.Namespace] = append(dataQueries[*metric.Namespace], types.MetricDataQuery{
Id: aws.String("average_" + id),
Label: aws.String(snakeCase(*metric.MetricName + "_average")),
MetricStat: &types.MetricStat{
Metric: &filtered.metrics[j],
Period: aws.Int32(int32(time.Duration(ins.Period).Seconds())),
Stat: aws.String(StatisticAverage),
},
})
}
if filtered.statFilter.Match("maximum") {
ins.queryDimensions["maximum_"+id] = dimension
dataQueries[*metric.Namespace] = append(dataQueries[*metric.Namespace], types.MetricDataQuery{
Id: aws.String("maximum_" + id),
Label: aws.String(snakeCase(*metric.MetricName + "_maximum")),
MetricStat: &types.MetricStat{
Metric: &filtered.metrics[j],
Period: aws.Int32(int32(time.Duration(ins.Period).Seconds())),
Stat: aws.String(StatisticMaximum),
},
})
}
if filtered.statFilter.Match("minimum") {
ins.queryDimensions["minimum_"+id] = dimension
dataQueries[*metric.Namespace] = append(dataQueries[*metric.Namespace], types.MetricDataQuery{
Id: aws.String("minimum_" + id),
Label: aws.String(snakeCase(*metric.MetricName + "_minimum")),
MetricStat: &types.MetricStat{
Metric: &filtered.metrics[j],
Period: aws.Int32(int32(time.Duration(ins.Period).Seconds())),
Stat: aws.String(StatisticMinimum),
},
})
}
if filtered.statFilter.Match("sum") {
ins.queryDimensions["sum_"+id] = dimension
dataQueries[*metric.Namespace] = append(dataQueries[*metric.Namespace], types.MetricDataQuery{
Id: aws.String("sum_" + id),
Label: aws.String(snakeCase(*metric.MetricName + "_sum")),
MetricStat: &types.MetricStat{
Metric: &filtered.metrics[j],
Period: aws.Int32(int32(time.Duration(ins.Period).Seconds())),
Stat: aws.String(StatisticSum),
},
})
}
if filtered.statFilter.Match("sample_count") {
ins.queryDimensions["sample_count_"+id] = dimension
dataQueries[*metric.Namespace] = append(dataQueries[*metric.Namespace], types.MetricDataQuery{
Id: aws.String("sample_count_" + id),
Label: aws.String(snakeCase(*metric.MetricName + "_sample_count")),
MetricStat: &types.MetricStat{
Metric: &filtered.metrics[j],
Period: aws.Int32(int32(time.Duration(ins.Period).Seconds())),
Stat: aws.String(StatisticSampleCount),
},
})
}
}
}
if len(dataQueries) == 0 {
if config.Config.DebugMode {
log.Printf("D! no metrics found to collect for namespace:%+v", ins.Namespaces)
}
return nil
}
if ins.metricCache == nil || !ins.metricCache.isValid() {
ins.metricCache = &metricCache{
queries: dataQueries,
built: time.Now(),
ttl: time.Duration(ins.CacheTTL),
}
} else {
ins.metricCache.queries = dataQueries
}
return dataQueries
}
// gatherMetrics gets metric data from Cloudwatch.
func (ins *Instance) gatherMetrics(
params *cwClient.GetMetricDataInput,
) ([]types.MetricDataResult, error) {
results := []types.MetricDataResult{}
for {
resp, err := ins.client.GetMetricData(context.Background(), params)
if err != nil {
return nil, fmt.Errorf("failed to get metric data: %v", err)
}
results = append(results, resp.MetricDataResults...)
if resp.NextToken == nil {
break
}
params.NextToken = resp.NextToken
}
return results, nil
}
func (ins *Instance) aggregateMetrics(
slist *internalTypes.SampleList,
metricDataResults map[string][]types.MetricDataResult,
) error {
var (
grouper = internalMetric.NewSeriesGrouper()
)
for namespace, results := range metricDataResults {
ns := namespace
namespace = sanitizeMeasurement(namespace)
for _, result := range results {
tags := map[string]string{}
if dimensions, ok := ins.queryDimensions[*result.Id]; ok {
tags = *dimensions
}
tags["region"] = ins.Region
tags["namespace"] = ns
for i := range result.Values {
grouper.Add(namespace, tags, result.Timestamps[i], *result.Label, result.Values[i])
}
}
}
samples := make([]*internalTypes.Sample, 0, len(grouper.Metrics()))
for _, metric := range grouper.Metrics() {
for name, value := range metric.Fields() {
sample := internalTypes.NewSample(metric.Name(), name, value, metric.Tags()).
SetTime(metric.Time().Local())
samples = append(samples, sample)
}
}
slist.PushFrontN(samples)
return nil
}
var _ inputs.SampleGatherer = new(Instance)
var _ inputs.Input = new(CloudWatch)
var _ inputs.InstancesGetter = new(CloudWatch)
func init() {
inputs.Add(inputName, func() inputs.Input {
return &CloudWatch{}
})
}
func (c *CloudWatch) Clone() inputs.Input {
return &CloudWatch{}
}
func (c *CloudWatch) Name() string {
return inputName
}
func sanitizeMeasurement(namespace string) string {
namespace = strings.ReplaceAll(namespace, "/", "_")
namespace = snakeCase(namespace)
return inputName + "_" + namespace
}
func snakeCase(s string) string {
s = stringx.SnakeCase(s)
s = strings.ReplaceAll(s, " ", "_")
s = strings.ReplaceAll(s, "__", "_")
return s
}
// ctod converts cloudwatch dimensions to regular dimensions.
func ctod(cDimensions []types.Dimension) *map[string]string {
dimensions := map[string]string{}
for i := range cDimensions {
dimensions[snakeCase(*cDimensions[i].Name)] = *cDimensions[i].Value
}
return &dimensions
}
func (ins *Instance) getDataInputs(dataQueries []types.MetricDataQuery) *cwClient.GetMetricDataInput {
return &cwClient.GetMetricDataInput{
StartTime: aws.Time(ins.windowStart),
EndTime: aws.Time(ins.windowEnd),
MetricDataQueries: dataQueries,
}
}
// isValid checks the validity of the metric cache.
func (f *metricCache) isValid() bool {
return f.metrics != nil && time.Since(f.built) < f.ttl
}
func hasWildcard(dimensions []*Dimension) bool {
for _, d := range dimensions {
if d.Value == "" || strings.ContainsAny(d.Value, "*?[") {
return true
}
}
return false
}
func isSelected(name string, metric types.Metric, dimensions []*Dimension) bool {
if name != *metric.MetricName {
return false
}
if len(metric.Dimensions) != len(dimensions) {
return false
}
for _, d := range dimensions {
selected := false
for _, d2 := range metric.Dimensions {
if d.Name == *d2.Name {
if d.Value == "" || d.valueMatcher.Match(*d2.Value) {
selected = true
}
}
}
if !selected {
return false
}
}
return true
}