forked from SpectralOps/netz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudwatch.go
304 lines (257 loc) · 7.27 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
package cloud
import (
"context"
"errors"
"fmt"
"sync"
"time"
log "github.com/cmpxchg16/netz/logger"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
)
const (
defaultLogTimeout = time.Minute * 120
defaultLogPollInterval = time.Second * 5
)
type cloudwatchLogsInterface interface {
DescribeLogStreamsPages(input *cloudwatchlogs.DescribeLogStreamsInput,
fn func(*cloudwatchlogs.DescribeLogStreamsOutput, bool) bool) error
DescribeLogStreams(input *cloudwatchlogs.DescribeLogStreamsInput) (*cloudwatchlogs.DescribeLogStreamsOutput, error)
PutLogEvents(input *cloudwatchlogs.PutLogEventsInput) (*cloudwatchlogs.PutLogEventsOutput, error)
FilterLogEventsPages(input *cloudwatchlogs.FilterLogEventsInput,
fn func(*cloudwatchlogs.FilterLogEventsOutput, bool) bool) error
}
type logWaiter struct {
CloudWatchLogs cloudwatchLogsInterface
LogGroupName string
LogStreamName string
Interval time.Duration
Timeout time.Duration
}
// streamExists checks the log group for a specific log stream
func (lw *logWaiter) streamExists() (bool, error) {
params := &cloudwatchlogs.DescribeLogStreamsInput{
LogGroupName: aws.String(lw.LogGroupName),
LogStreamNamePrefix: aws.String(lw.LogStreamName),
Descending: aws.Bool(true),
}
var exists bool
err := lw.CloudWatchLogs.DescribeLogStreamsPages(params,
func(page *cloudwatchlogs.DescribeLogStreamsOutput, lastPage bool) bool {
for _, stream := range page.LogStreams {
// return early if we match the log stream
if *stream.LogStreamName == lw.LogStreamName {
exists = true
return true
}
}
return lastPage
})
return exists, err
}
// Wait waits for a log stream to exist
func (lw *logWaiter) Wait(ctx context.Context) error {
log.Logger.Infof("waiting for log stream %s to exist...", lw.LogStreamName)
t := time.Now()
pollInterval := lw.Interval
if pollInterval == time.Duration(0) {
pollInterval = defaultLogPollInterval
}
timeout := lw.Timeout
if timeout == time.Duration(0) {
timeout = defaultLogTimeout
}
ticker := time.NewTicker(pollInterval)
defer ticker.Stop()
done := make(chan bool)
go func() {
time.Sleep(timeout)
done <- true
}()
for {
exists, err := lw.streamExists()
// handle rate-limiting errors which seem to occur during
// excessive polling operations
if isRateLimited(err) {
time.Sleep(5 * time.Second)
continue
} else if err != nil {
//return err
continue
} else if exists {
log.Logger.Infof("found stream %s after %v", lw.LogStreamName, time.Now().Sub(t))
return nil
}
select {
case <-done:
log.Logger.Error("timed out waiting for stream")
return fmt.Errorf("timed out waiting for stream %s", lw.LogStreamName)
case <-ticker.C:
continue
}
}
}
func isRateLimited(err error) bool {
if aerr, ok := err.(awserr.Error); ok {
if aerr.Code() == "Throttling" {
return true
}
}
return false
}
type logWatcher struct {
CloudWatchLogs cloudwatchLogsInterface
LogGroupName string
LogStreamName string
Printer func(event *cloudwatchlogs.FilteredLogEvent) bool
Interval time.Duration
Timeout time.Duration
mu sync.Mutex
stop chan struct{}
}
func (lw *logWatcher) Watch(ctx context.Context) error {
lw.mu.Lock()
lw.stop = make(chan struct{})
lw.mu.Unlock()
waiter := &logWaiter{
CloudWatchLogs: lw.CloudWatchLogs,
LogGroupName: lw.LogGroupName,
LogStreamName: lw.LogStreamName,
Interval: lw.Interval,
Timeout: lw.Timeout,
}
if err := waiter.Wait(ctx); err != nil {
return err
}
var after int64
var err error
pollInterval := lw.Interval
if pollInterval == time.Duration(0) {
pollInterval = time.Second * 5
}
for {
select {
case <-time.After(pollInterval):
if after, err = lw.printEventsAfter(ctx, after); err != nil {
return err
}
case <-lw.stop:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
}
func (lw *logWatcher) Stop() error {
lw.mu.Lock()
defer lw.mu.Unlock()
if lw.stop != nil {
close(lw.stop)
return nil
}
return errors.New("log watcher not started")
}
func (lw *logWatcher) printEventsAfter(ctx context.Context, ts int64) (int64, error) {
log.Logger.Tracef("Printing events in stream %q after %d", lw.LogStreamName, ts)
t := time.Now()
var count int64
filterInput := &cloudwatchlogs.FilterLogEventsInput{
LogGroupName: aws.String(lw.LogGroupName),
LogStreamNames: aws.StringSlice([]string{lw.LogStreamName}),
StartTime: aws.Int64(ts + 1),
}
err := lw.CloudWatchLogs.FilterLogEventsPages(filterInput,
func(p *cloudwatchlogs.FilterLogEventsOutput, lastPage bool) (shouldContinue bool) {
for _, event := range p.Events {
log.Logger.Trace(event)
count++
if !lw.Printer(event) {
log.Logger.Info("stopping log watcher via print function")
lw.Stop()
}
if *event.Timestamp > ts {
ts = *event.Timestamp
}
}
return lastPage
})
if err != nil {
log.Logger.Tracef("printed %d events in %v", count, time.Now().Sub(t))
}
return ts, err
}
type logWriter struct {
CloudWatchLogs cloudwatchLogsInterface
LogGroupName string
LogStreamName string
Interval time.Duration
Timeout time.Duration
}
func (lw *logWriter) nextSequenceToken() (*string, error) {
log.Logger.Tracef("finding next sequence token for stream %s", lw.LogStreamName)
streams, err := lw.CloudWatchLogs.DescribeLogStreams(&cloudwatchlogs.DescribeLogStreamsInput{
LogGroupName: aws.String(lw.LogGroupName),
LogStreamNamePrefix: aws.String(lw.LogStreamName),
Descending: aws.Bool(true),
Limit: aws.Int64(1),
})
if err != nil {
return nil, err
} else if len(streams.LogStreams) == 0 {
return nil, fmt.Errorf("failed to find stream %s in group %s", lw.LogStreamName, lw.LogGroupName)
}
return streams.LogStreams[0].UploadSequenceToken, nil
}
func (lw *logWriter) WriteString(ctx context.Context, msg string) error {
waiter := &logWaiter{
CloudWatchLogs: lw.CloudWatchLogs,
LogGroupName: lw.LogGroupName,
LogStreamName: lw.LogStreamName,
Interval: lw.Interval,
Timeout: lw.Timeout,
}
if err := waiter.Wait(ctx); err != nil {
return err
}
sequence, err := lw.nextSequenceToken()
if err != nil {
return err
}
log.Logger.Tracef("putting log message %q to %s", msg, lw.LogStreamName)
_, err = lw.CloudWatchLogs.PutLogEvents(&cloudwatchlogs.PutLogEventsInput{
SequenceToken: sequence,
LogGroupName: aws.String(lw.LogGroupName),
LogStreamName: aws.String(lw.LogStreamName),
LogEvents: []*cloudwatchlogs.InputLogEvent{
{
Message: aws.String(msg),
Timestamp: aws.Int64(aws.TimeUnixMilli(time.Now())),
},
},
})
return err
}
func createLogGroup(sess *session.Session, logGroup string) error {
cwl := cloudwatchlogs.New(sess)
groups, err := cwl.DescribeLogGroups(&cloudwatchlogs.DescribeLogGroupsInput{
Limit: aws.Int64(1),
LogGroupNamePrefix: aws.String(logGroup),
})
if err != nil {
return err
}
if len(groups.LogGroups) == 0 {
log.Logger.Infof("creating log group %s", logGroup)
_, err = cwl.CreateLogGroup(&cloudwatchlogs.CreateLogGroupInput{
LogGroupName: aws.String(logGroup),
})
if err != nil {
return err
}
} else {
log.Logger.Debugf("log group %s exists", logGroup)
}
return nil
}