-
Notifications
You must be signed in to change notification settings - Fork 7
/
cloudwatch_writer.go
335 lines (282 loc) · 9.33 KB
/
cloudwatch_writer.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
package cloudwatchwriter
import (
"fmt"
"sync"
"time"
"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"
"github.com/pkg/errors"
"gopkg.in/oleiade/lane.v1"
)
const (
// minBatchInterval is 200 ms as the maximum rate of PutLogEvents is 5
// requests per second.
minBatchInterval time.Duration = 200000000
// defaultBatchInterval is 5 seconds.
defaultBatchInterval time.Duration = 5000000000
// batchSizeLimit is 1MB in bytes, the limit imposed by AWS CloudWatch Logs
// on the size the batch of logs we send, see:
// https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html
batchSizeLimit = 1048576
// maxNumLogEvents is the maximum number of messages that can be sent in one
// batch, also an AWS limitation, see:
// https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html
maxNumLogEvents = 10000
// additionalBytesPerLogEvent is the number of additional bytes per log
// event, other than the length of the log message, see:
// https://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html
additionalBytesPerLogEvent = 26
)
// CloudWatchLogsClient represents the AWS cloudwatchlogs client that we need to talk to CloudWatch
type CloudWatchLogsClient interface {
DescribeLogStreams(*cloudwatchlogs.DescribeLogStreamsInput) (*cloudwatchlogs.DescribeLogStreamsOutput, error)
CreateLogGroup(*cloudwatchlogs.CreateLogGroupInput) (*cloudwatchlogs.CreateLogGroupOutput, error)
CreateLogStream(*cloudwatchlogs.CreateLogStreamInput) (*cloudwatchlogs.CreateLogStreamOutput, error)
PutLogEvents(*cloudwatchlogs.PutLogEventsInput) (*cloudwatchlogs.PutLogEventsOutput, error)
}
// CloudWatchWriter can be inserted into zerolog to send logs to CloudWatch.
type CloudWatchWriter struct {
sync.RWMutex
client CloudWatchLogsClient
batchInterval time.Duration
queue *lane.Queue
err error
logGroupName *string
logStreamName *string
nextSequenceToken *string
closing bool
done chan struct{}
errorHandler func(error)
}
// New returns a pointer to a CloudWatchWriter struct, or an error.
func New(sess *session.Session, logGroupName, logStreamName string) (*CloudWatchWriter, error) {
return NewWithClient(cloudwatchlogs.New(sess), defaultBatchInterval, logGroupName, logStreamName)
}
// NewWithClient returns a pointer to a CloudWatchWriter struct, or an error.
func NewWithClient(client CloudWatchLogsClient, batchInterval time.Duration, logGroupName, logStreamName string) (*CloudWatchWriter, error) {
writer := &CloudWatchWriter{
client: client,
queue: lane.NewQueue(),
logGroupName: aws.String(logGroupName),
logStreamName: aws.String(logStreamName),
done: make(chan struct{}),
}
err := writer.SetBatchInterval(batchInterval)
if err != nil {
return nil, errors.Wrapf(err, "set batch interval: %v", batchInterval)
}
logStream, err := writer.getOrCreateLogStream()
if err != nil {
return nil, err
}
writer.setNextSequenceToken(logStream.UploadSequenceToken)
go writer.queueMonitor()
return writer, nil
}
// SetBatchInterval sets the maximum time between batches of logs sent to
// CloudWatch.
func (c *CloudWatchWriter) SetBatchInterval(interval time.Duration) error {
if interval < minBatchInterval {
return fmt.Errorf("supplied batch interval, %dms, is less than the minimum, %dms", interval.Milliseconds(), minBatchInterval.Milliseconds())
}
c.setBatchInterval(interval)
return nil
}
// SetErrorHandler adds a function to be run every time there is an error
// sending logs to CloudWatch.
func (c *CloudWatchWriter) SetErrorHandler(handler func(error)) {
c.setErrorHandler(handler)
}
func (c *CloudWatchWriter) setBatchInterval(interval time.Duration) {
c.Lock()
defer c.Unlock()
c.batchInterval = interval
}
func (c *CloudWatchWriter) getBatchInterval() time.Duration {
c.RLock()
defer c.RUnlock()
return c.batchInterval
}
func (c *CloudWatchWriter) setErrorHandler(handler func(error)) {
c.Lock()
defer c.Unlock()
c.errorHandler = handler
}
func (c *CloudWatchWriter) handleError(err error) {
if errHandler := c.getErrorHandler(); errHandler != nil {
errHandler(err)
}
c.setErr(err)
}
func (c *CloudWatchWriter) getErrorHandler() func(error) {
c.RLock()
defer c.RUnlock()
return c.errorHandler
}
func (c *CloudWatchWriter) setErr(err error) {
c.Lock()
defer c.Unlock()
c.err = err
}
func (c *CloudWatchWriter) getErr() error {
c.RLock()
defer c.RUnlock()
return c.err
}
func (c *CloudWatchWriter) setNextSequenceToken(next *string) {
c.Lock()
defer c.Unlock()
c.nextSequenceToken = next
}
func (c *CloudWatchWriter) getNextSequenceToken() *string {
c.RLock()
defer c.RUnlock()
return c.nextSequenceToken
}
// Write implements the io.Writer interface.
func (c *CloudWatchWriter) Write(log []byte) (int, error) {
event := &cloudwatchlogs.InputLogEvent{
Message: aws.String(string(log)),
// Timestamp has to be in milliseconds since the epoch
Timestamp: aws.Int64(time.Now().UTC().UnixNano() / int64(time.Millisecond)),
}
c.queue.Enqueue(event)
// report last sending error
lastErr := c.getErr()
if lastErr != nil {
c.setErr(nil)
return 0, lastErr
}
return len(log), nil
}
func (c *CloudWatchWriter) queueMonitor() {
var batch []*cloudwatchlogs.InputLogEvent
batchSize := 0
nextSendTime := time.Now().Add(c.getBatchInterval())
for {
if time.Now().After(nextSendTime) {
c.sendBatch(batch, 0)
batch = nil
batchSize = 0
nextSendTime = time.Now().Add(c.getBatchInterval())
}
item := c.queue.Dequeue()
if item == nil {
// Empty queue, means no logs to process
if c.isClosing() {
c.sendBatch(batch, 0)
// At this point we've processed all the logs and can safely
// close.
close(c.done)
return
}
time.Sleep(time.Millisecond)
continue
}
logEvent, ok := item.(*cloudwatchlogs.InputLogEvent)
if !ok || logEvent.Message == nil {
// This should not happen!
continue
}
messageSize := len(*logEvent.Message) + additionalBytesPerLogEvent
// Send the batch before adding the next message, if the message would
// push it over the 1MB limit on batch size.
if batchSize+messageSize > batchSizeLimit {
c.sendBatch(batch, 0)
batch = nil
batchSize = 0
nextSendTime = time.Now().Add(c.getBatchInterval())
}
batch = append(batch, logEvent)
batchSize += messageSize
if len(batch) >= maxNumLogEvents {
c.sendBatch(batch, 0)
batch = nil
batchSize = 0
nextSendTime = time.Now().Add(c.getBatchInterval())
}
}
}
// Only allow 1 retry of an invalid sequence token.
func (c *CloudWatchWriter) sendBatch(batch []*cloudwatchlogs.InputLogEvent, retryNum int) {
if len(batch) == 0 {
return
}
input := &cloudwatchlogs.PutLogEventsInput{
LogEvents: batch,
LogGroupName: c.logGroupName,
LogStreamName: c.logStreamName,
SequenceToken: c.getNextSequenceToken(),
}
output, err := c.client.PutLogEvents(input)
if err != nil {
if invalidSequenceTokenErr, ok := err.(*cloudwatchlogs.InvalidSequenceTokenException); ok {
c.setNextSequenceToken(invalidSequenceTokenErr.ExpectedSequenceToken)
if retryNum >= 1 {
c.handleError(err)
return
}
c.sendBatch(batch, retryNum+1)
return
}
c.handleError(err)
return
}
c.setNextSequenceToken(output.NextSequenceToken)
}
// Close blocks until the writer has completed writing the logs to CloudWatch.
func (c *CloudWatchWriter) Close() {
c.setClosing()
// block until the done channel is closed
<-c.done
}
func (c *CloudWatchWriter) isClosing() bool {
c.RLock()
defer c.RUnlock()
return c.closing
}
func (c *CloudWatchWriter) setClosing() {
c.Lock()
defer c.Unlock()
c.closing = true
}
// getOrCreateLogStream gets info on the log stream for the log group and log
// stream we're interested in -- primarily for the purpose of finding the value
// of the next sequence token. If the log group doesn't exist, then we create
// it, if the log stream doesn't exist, then we create it.
func (c *CloudWatchWriter) getOrCreateLogStream() (*cloudwatchlogs.LogStream, error) {
// Get the log streams that match our log group name and log stream
output, err := c.client.DescribeLogStreams(&cloudwatchlogs.DescribeLogStreamsInput{
LogGroupName: c.logGroupName,
LogStreamNamePrefix: c.logStreamName,
})
if err != nil || output == nil {
awserror, ok := err.(awserr.Error)
// i.e. the log group does not exist
if ok && awserror.Code() == cloudwatchlogs.ErrCodeResourceNotFoundException {
_, err = c.client.CreateLogGroup(&cloudwatchlogs.CreateLogGroupInput{
LogGroupName: c.logGroupName,
})
if err != nil {
return nil, errors.Wrap(err, "cloudwatchlog.Client.CreateLogGroup")
}
return c.getOrCreateLogStream()
}
return nil, errors.Wrap(err, "cloudwatchlogs.Client.DescribeLogStreams")
}
if len(output.LogStreams) > 0 {
return output.LogStreams[0], nil
}
// No matching log stream, so we need to create it
_, err = c.client.CreateLogStream(&cloudwatchlogs.CreateLogStreamInput{
LogGroupName: c.logGroupName,
LogStreamName: c.logStreamName,
})
if err != nil {
return nil, errors.Wrap(err, "cloudwatchlogs.Client.CreateLogStream")
}
// We can just return an empty log stream as the initial sequence token would be nil anyway.
return &cloudwatchlogs.LogStream{}, nil
}