-
Notifications
You must be signed in to change notification settings - Fork 255
/
cloudwatch.go
607 lines (547 loc) · 15.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
package cloudwatch
import (
"errors"
"fmt"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/aws/credentials"
"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/qiniu/log"
"github.com/qiniu/pandora-go-sdk/base/ratelimit"
"github.com/qiniu/pandora-go-sdk/pipeline"
"github.com/qiniu/logkit/conf"
"github.com/qiniu/logkit/reader"
. "github.com/qiniu/logkit/reader/config"
. "github.com/qiniu/logkit/utils/models"
)
var (
_ reader.DaemonReader = &Reader{}
_ reader.DataReader = &Reader{}
_ reader.Reader = &Reader{}
)
type (
readInfo struct {
data Data
bytes int64
}
Reader struct {
meta *reader.Meta
// Note: 原子操作,用于表示 reader 整体的运行状态
status int32
/*
Note: 原子操作,用于表示获取数据的线程运行状态
- StatusInit: 当前没有任务在执行
- StatusRunning: 当前有任务正在执行
- StatusStopping: 数据管道已经由上层关闭,执行中的任务完成时直接退出无需再处理
*/
routineStatus int32
stopChan chan struct{} // 用于直接 close 对关闭操作进行广播
readChan chan readInfo
errChan chan error
Region string
CollectInterval time.Duration
Period time.Duration
Delay time.Duration
Namespace string
Metrics []*Metric
CacheTTL time.Duration
RateLimit int64
client cloudwatchClient
metricCache *MetricCache
}
Metric struct {
MetricNames []string
Dimensions []*Dimension
}
Dimension struct {
Name string
Value string
}
MetricCache struct {
TTL time.Duration
Fetched time.Time
Metrics []*cloudwatch.Metric
}
cloudwatchClient interface {
ListMetrics(*cloudwatch.ListMetricsInput) (*cloudwatch.ListMetricsOutput, error)
GetMetricStatistics(*cloudwatch.GetMetricStatisticsInput) (*cloudwatch.GetMetricStatisticsOutput, error)
}
)
func init() {
reader.RegisterConstructor(ModeCloudWatch, NewReader)
}
func NewReader(meta *reader.Meta, conf conf.MapConf) (reader.Reader, error) {
region, err := conf.GetString(KeyRegion)
if err != nil {
return nil, err
}
namespace, err := conf.GetString(KeyNamespace)
if err != nil {
return nil, err
}
ak, _ := conf.GetPasswordEnvStringOr(KeyAWSAccessKey, "")
sk, _ := conf.GetPasswordEnvStringOr(KeyAWSSecretKey, "")
roleARN, _ := conf.GetStringOr(KeyRoleArn, "")
token, _ := conf.GetPasswordEnvStringOr(KeyAWSToken, "")
profile, _ := conf.GetPasswordEnvStringOr(KeyAWSProfile, "")
sharedCredentialFile, _ := conf.GetStringOr(KeySharedCredentialFile, "")
credentialConfig := &CredentialConfig{
Region: region,
AccessKey: ak,
SecretKey: sk,
RoleARN: roleARN,
Profile: profile,
Filename: sharedCredentialFile,
Token: token,
}
configProvider, err := credentialConfig.Credentials()
if err != nil {
log.Errorf("aws Credentials err %v", err)
return nil, err
}
cacheTTL, _ := conf.GetStringOr(KeyCacheTTL, "1hr")
ttl, err := time.ParseDuration(cacheTTL)
if err != nil {
return nil, fmt.Errorf("parse cachettl %v error %v", cacheTTL, err)
}
interval, _ := conf.GetStringOr(KeyCollectInterval, "5m")
collectInteval, err := time.ParseDuration(interval)
if err != nil {
return nil, fmt.Errorf("parse interval %v error %v", interval, err)
}
rateLimit, _ := conf.GetInt64Or(KeyRateLimit, 200)
metrics, _ := conf.GetStringListOr(KeyMetrics, []string{})
var dimensions []*Dimension
dimensionList, _ := conf.GetStringListOr(KeyDimension, []string{})
for _, v := range dimensionList {
v = strings.TrimSpace(v)
sks := strings.Fields(v)
if len(sks) < 1 {
continue
}
var name, value string
name = sks[0]
if len(sks) > 1 {
value = sks[1]
}
dimensions = append(dimensions, &Dimension{name, value})
}
delayStr, _ := conf.GetStringOr(KeyDelay, "5m")
delay, err := time.ParseDuration(delayStr)
if err != nil {
return nil, fmt.Errorf("parse delay %v error %v", delayStr, err)
}
periodStr, _ := conf.GetStringOr(KeyPeriod, "5m")
period, err := time.ParseDuration(periodStr)
if err != nil {
return nil, fmt.Errorf("parse period %v error %v", periodStr, err)
}
cfg := aws.NewConfig()
if log.GetOutputLevel() == log.Ldebug {
cfg.WithLogLevel(aws.LogDebug)
}
var Metrics []*Metric
if len(metrics) > 0 || len(dimensions) > 0 {
Metrics = []*Metric{{MetricNames: metrics, Dimensions: dimensions}}
}
return &Reader{
meta: meta,
status: StatusInit,
routineStatus: StatusInit,
stopChan: make(chan struct{}),
readChan: make(chan readInfo),
errChan: make(chan error),
Region: region,
Namespace: namespace,
client: cloudwatch.New(configProvider, cfg),
RateLimit: rateLimit,
CacheTTL: ttl,
Delay: delay,
Period: period,
CollectInterval: collectInteval,
Metrics: Metrics,
}, nil
}
func (r *Reader) isStopping() bool {
return atomic.LoadInt32(&r.status) == StatusStopping
}
func (r *Reader) hasStopped() bool {
return atomic.LoadInt32(&r.status) == StatusStopped
}
func (r *Reader) Name() string {
return "cloudwatch_" + r.Region + "_" + r.Namespace
}
func (r *Reader) SetMode(mode string, v interface{}) error {
return nil
}
func (r *Reader) sendError(err error) {
if err == nil {
return
}
defer func() {
if rec := recover(); rec != nil {
log.Errorf("Reader %q was panicked and recovered from %v", r.Name(), rec)
}
}()
r.errChan <- err
}
func (r *Reader) Start() error {
if r.isStopping() || r.hasStopped() {
return errors.New("reader is stopping or has stopped")
} else if !atomic.CompareAndSwapInt32(&r.status, StatusInit, StatusRunning) {
log.Warnf("Runner[%v] %q daemon has already started and is running", r.meta.RunnerName, r.Name())
return nil
}
go func() {
ticker := time.NewTicker(r.CollectInterval)
defer ticker.Stop()
for {
err := r.Gather()
if err != nil {
log.Errorf("Runner[%v] %q gather failed: %v ", r.meta.RunnerName, r.Name(), err)
}
select {
case <-r.stopChan:
atomic.StoreInt32(&r.status, StatusStopped)
log.Infof("Runner[%v] %q daemon has stopped from running", r.meta.RunnerName, r.Name())
return
case <-ticker.C:
}
}
}()
log.Infof("Runner[%v] %q daemon has started", r.meta.RunnerName, r.Name())
return nil
}
func (r *Reader) Source() string {
return "cloudwatch_" + r.Region + "_" + r.Namespace
}
func (r *Reader) ReadLine() (string, error) {
return "", errors.New("method ReadLine is not supported, please use ReadData")
}
func (r *Reader) ReadData() (Data, int64, error) {
timer := time.NewTimer(time.Second)
defer timer.Stop()
select {
case info := <-r.readChan:
return info.data, info.bytes, nil
case err := <-r.errChan:
return nil, 0, err
case <-timer.C:
}
return nil, 0, nil
}
func (*Reader) SyncMeta() {}
func (r *Reader) Close() error {
if !atomic.CompareAndSwapInt32(&r.status, StatusRunning, StatusStopping) {
log.Warnf("Runner[%v] reader %q is not running, close operation ignored", r.meta.RunnerName, r.Name())
return nil
}
log.Debugf("Runner[%v] %q daemon is stopping", r.meta.RunnerName, r.Name())
close(r.stopChan)
// 如果此时没有 routine 正在运行,则在此处关闭数据管道,否则由 routine 在退出时负责关闭
if atomic.CompareAndSwapInt32(&r.routineStatus, StatusInit, StatusStopping) {
close(r.readChan)
close(r.errChan)
}
return nil
}
func isIn(metrics []string, now string) bool {
for _, v := range metrics {
if now == v {
return true
}
}
return false
}
func SelectMetrics(c *Reader) ([]*cloudwatch.Metric, error) {
var metrics []*cloudwatch.Metric
// check for provided metric filter
if len(c.Metrics) > 0 {
metrics = []*cloudwatch.Metric{}
for _, m := range c.Metrics {
if !hasWilcard(m.Dimensions) && len(m.MetricNames) > 0 {
dimensions := make([]*cloudwatch.Dimension, len(m.Dimensions))
for k, d := range m.Dimensions {
dimensions[k] = &cloudwatch.Dimension{
Name: aws.String(d.Name),
Value: aws.String(d.Value),
}
}
for _, name := range m.MetricNames {
metrics = append(metrics, &cloudwatch.Metric{
Namespace: aws.String(c.Namespace),
MetricName: aws.String(name),
Dimensions: dimensions,
})
}
} else {
allMetrics, err := c.fetchNamespaceMetrics()
if err != nil {
return nil, err
}
if len(m.MetricNames) <= 0 {
for _, v := range allMetrics {
if isIn(m.MetricNames, *v.MetricName) {
continue
}
m.MetricNames = append(m.MetricNames, *v.MetricName)
}
}
for _, name := range m.MetricNames {
log.Debugf("select metric: %s", name)
for _, v := range m.Dimensions {
log.Debugf("select dimension: %v", *v)
}
for _, metric := range allMetrics {
if isSelected(name, metric, m.Dimensions) {
metrics = append(metrics, &cloudwatch.Metric{
Namespace: aws.String(c.Namespace),
MetricName: aws.String(name),
Dimensions: metric.Dimensions,
})
}
}
}
}
}
} else {
var err error
metrics, err = c.fetchNamespaceMetrics()
if err != nil {
return nil, err
}
}
log.Debugf("get namespace metrics %v", metrics)
return metrics, nil
}
func (r *Reader) Gather() error {
// 未在准备状态(StatusInit)时无法执行此次任务
if !atomic.CompareAndSwapInt32(&r.routineStatus, StatusInit, StatusRunning) {
if r.isStopping() || r.hasStopped() {
log.Warnf("Runner[%v] %q daemon has stopped, this task does not need to be executed and is skipped this time", r.meta.RunnerName, r.Name())
} else {
log.Errorf("Runner[%v] %q daemon is still working on last task, this task will not be executed and is skipped this time", r.meta.RunnerName, r.Name())
}
return nil
}
defer func() {
// 如果 reader 在 routine 运行时关闭,则需要此 routine 负责关闭数据管道
if r.isStopping() || r.hasStopped() {
if atomic.CompareAndSwapInt32(&r.routineStatus, StatusRunning, StatusStopping) {
close(r.readChan)
close(r.errChan)
}
return
}
atomic.StoreInt32(&r.routineStatus, StatusInit)
}()
metrics, err := SelectMetrics(r)
if err != nil {
return err
}
var lastErr error
now := time.Now()
lmtr := ratelimit.NewLimiter(r.RateLimit)
defer lmtr.Close()
var wg sync.WaitGroup
wg.Add(len(metrics))
for _, m := range metrics {
lmtr.Assign(1)
go func(inm *cloudwatch.Metric) {
defer wg.Done()
datas, err := r.gatherMetric(inm, now)
if err != nil {
log.Errorf("gatherMetric error %v", err)
lastErr = err
r.sendError(err)
}
log.Debugf("successfully gatherMetric %v data %v", *inm.MetricName, len(datas))
for _, v := range datas {
r.readChan <- v
}
}(m)
}
wg.Wait()
return lastErr
}
/*
* Fetch available metrics for given CloudWatch Namespace
*/
func (r *Reader) fetchNamespaceMetrics() ([]*cloudwatch.Metric, error) {
if r.metricCache != nil && r.metricCache.IsValid() {
return r.metricCache.Metrics, nil
}
metrics := []*cloudwatch.Metric{}
var token *string
for more := true; more; {
params := &cloudwatch.ListMetricsInput{
Namespace: aws.String(r.Namespace),
Dimensions: []*cloudwatch.DimensionFilter{},
NextToken: token,
MetricName: nil,
}
resp, err := r.client.ListMetrics(params)
if err != nil {
return nil, err
}
metrics = append(metrics, resp.Metrics...)
log.Debugf("listMetrics: %v", resp.Metrics)
token = resp.NextToken
more = token != nil
}
r.metricCache = &MetricCache{
Metrics: metrics,
Fetched: time.Now(),
TTL: r.CacheTTL,
}
return metrics, nil
}
func (r *Reader) gatherMetric(metric *cloudwatch.Metric, now time.Time) ([]readInfo, error) {
params := r.getStatisticsInput(metric, now)
resp, err := r.client.GetMetricStatistics(params)
if err != nil {
return nil, err
}
log.Debugf("gatherMetric resp %v", resp)
readInfos := make([]readInfo, 0, len(resp.Datapoints))
for _, point := range resp.Datapoints {
data := make(Data)
for _, d := range metric.Dimensions {
data[snakeCase(*d.Name)] = *d.Value
}
data[KeyTimestamp] = *point.Timestamp
if point.Average != nil {
data[formatKey(*metric.MetricName, cloudwatch.StatisticAverage)] = *point.Average
}
if point.Maximum != nil {
data[formatKey(*metric.MetricName, cloudwatch.StatisticMaximum)] = *point.Maximum
}
if point.Minimum != nil {
data[formatKey(*metric.MetricName, cloudwatch.StatisticMinimum)] = *point.Minimum
}
if point.SampleCount != nil {
data[formatKey(*metric.MetricName, cloudwatch.StatisticSampleCount)] = *point.SampleCount
}
if point.Sum != nil {
data[formatKey(*metric.MetricName, cloudwatch.StatisticSum)] = *point.Sum
}
readInfos = append(readInfos, readInfo{data, int64(len(point.String()))})
}
return readInfos, nil
}
func formatKey(metricName string, statistic string) string {
return fmt.Sprintf("%s_%s", snakeCase(metricName), snakeCase(statistic))
}
func snakeCase(s string) string {
s, _ = pipeline.PandoraKey(s)
s = strings.Replace(s, "__", "_", -1)
return s
}
/*
* Map Metric to *cloudwatch.GetMetricStatisticsInput for given timeframe
*/
func (r *Reader) getStatisticsInput(metric *cloudwatch.Metric, now time.Time) *cloudwatch.GetMetricStatisticsInput {
end := now.Add(-r.Delay)
input := &cloudwatch.GetMetricStatisticsInput{
StartTime: aws.Time(end.Add(-r.Period)),
EndTime: aws.Time(end),
MetricName: metric.MetricName,
Namespace: metric.Namespace,
Period: aws.Int64(int64(r.Period.Seconds())),
Dimensions: metric.Dimensions,
Statistics: []*string{
aws.String(cloudwatch.StatisticAverage),
aws.String(cloudwatch.StatisticMaximum),
aws.String(cloudwatch.StatisticMinimum),
aws.String(cloudwatch.StatisticSum),
aws.String(cloudwatch.StatisticSampleCount)},
}
return input
}
/*
* Check Metric Cache validity
*/
func (c *MetricCache) IsValid() bool {
return c.Metrics != nil && time.Since(c.Fetched) < c.TTL
}
func hasWilcard(dimensions []*Dimension) bool {
if len(dimensions) <= 0 {
return true
}
for _, d := range dimensions {
if d.Value == "" || d.Value == "*" {
return true
}
}
return false
}
func isSelected(name string, metric *cloudwatch.Metric, dimensions []*Dimension) bool {
log.Debugf("metcis: %v, select: %v real: %v\n", *metric.MetricName, len(dimensions), len(metric.Dimensions))
for _, v := range metric.Dimensions {
log.Debugf("dimensions: %v", *v)
}
if name != *metric.MetricName {
return false
}
// 啥dimension都没写,都选
if len(dimensions) < 1 {
return true
}
for _, d := range dimensions {
selected := false
for _, d2 := range metric.Dimensions {
if d.Name == *d2.Name {
if d.Value == "" || d.Value == "*" || d.Value == *d2.Value {
selected = true
}
}
}
if !selected {
return false
}
}
return true
}
type CredentialConfig struct {
Region string
AccessKey string
SecretKey string
RoleARN string
Profile string
Filename string
Token string
}
func (c *CredentialConfig) Credentials() (client.ConfigProvider, error) {
if c.RoleARN != "" {
return c.assumeCredentials()
}
return c.rootCredentials()
}
func (c *CredentialConfig) rootCredentials() (client.ConfigProvider, error) {
config := &aws.Config{
Region: aws.String(c.Region),
}
if c.AccessKey != "" || c.SecretKey != "" {
config.Credentials = credentials.NewStaticCredentials(c.AccessKey, c.SecretKey, c.Token)
} else if c.Profile != "" || c.Filename != "" {
config.Credentials = credentials.NewSharedCredentials(c.Filename, c.Profile)
}
return session.NewSession(config)
}
func (c *CredentialConfig) assumeCredentials() (client.ConfigProvider, error) {
rootCredentials, err := c.rootCredentials()
if err != nil {
return nil, err
}
config := &aws.Config{
Region: aws.String(c.Region),
}
config.Credentials = stscreds.NewCredentials(rootCredentials, c.RoleARN)
return session.NewSession(config)
}