This repository has been archived by the owner on Dec 19, 2023. It is now read-only.
forked from marcinwyszynski/cloudwatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.go
206 lines (163 loc) · 3.85 KB
/
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
package cloudwatch
import (
"bufio"
"bytes"
"context"
"io"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
iface "github.com/aws/aws-sdk-go/service/cloudwatchlogs/cloudwatchlogsiface"
)
type writerImpl struct {
client iface.CloudWatchLogsAPI
groupName, streamName, sequenceToken *string
ctx context.Context
closeChan chan (struct{})
closed bool
err error
events *eventsBuffer
nowFunc func() time.Time
onEvent func(*cloudwatchlogs.InputLogEvent)
throttle *time.Ticker
sync.Mutex // This protects calls to flush.
}
// WithInputCallback allows setting a function introspecting each input log
// event before it's sent to AWS CloudWatch Logs.
func WithInputCallback(callback func(*cloudwatchlogs.InputLogEvent)) CreateOption {
return func(w *writerImpl) {
w.onEvent = callback
}
}
// FromToken allows writing from an arbitrary sequence token.
func FromToken(sequenceToken string) CreateOption {
return func(w *writerImpl) {
w.sequenceToken = aws.String(sequenceToken)
}
}
func freezeTime(now time.Time) CreateOption {
return func(w *writerImpl) {
w.nowFunc = func() time.Time {
return now
}
}
}
// Write takes the buffer, and creates a Cloudwatch Log event for each
// individual line. If Flush returns an error, subsequent calls to Write will
// fail.
func (w *writerImpl) Write(b []byte) (int, error) {
if w.closed {
return 0, io.ErrClosedPipe
}
if w.err != nil {
return 0, w.err
}
return w.buffer(b)
}
// Start continuously flushing the buffered events.
func (w *writerImpl) start() (err error) {
for {
select {
case <-w.closeChan:
return
case <-w.throttle.C:
if err = w.flushBatch(); err != nil {
return
}
}
}
}
// Close closes the writer. Any subsequent calls to Write will return
// io.ErrClosedPipe.
func (w *writerImpl) Close() error {
defer w.throttle.Stop()
w.closed = true
close(w.closeChan)
for w.events.hasMore() {
if w.flushTrottled() != nil {
break
}
}
return w.err
}
func (w *writerImpl) flushTrottled() error {
<-w.throttle.C
return w.flushBatch()
}
func (w *writerImpl) flushBatch() error {
w.Lock()
defer w.Unlock()
events := w.events.drain()
// No events to flush.
if len(events) == 0 {
return nil
}
w.err = w.flush(events)
return w.err
}
// flush flushes a slice of log events. This method should be called
// sequentially to ensure that the sequence token is updated properly.
func (w *writerImpl) flush(events []*cloudwatchlogs.InputLogEvent) (err error) {
var resp *cloudwatchlogs.PutLogEventsOutput
for {
resp, err = w.client.PutLogEventsWithContext(w.ctx, &cloudwatchlogs.PutLogEventsInput{
LogEvents: events,
LogGroupName: w.groupName,
LogStreamName: w.streamName,
SequenceToken: w.sequenceToken,
})
if err == nil {
break
}
sequenceError, ok := err.(*cloudwatchlogs.InvalidSequenceTokenException)
if !ok {
return err
}
w.sequenceToken = sequenceError.ExpectedSequenceToken
}
if resp.RejectedLogEventsInfo != nil {
w.err = &RejectedLogEventsInfoError{Info: resp.RejectedLogEventsInfo}
return w.err
}
w.sequenceToken = resp.NextSequenceToken
return nil
}
// buffer splits up b into individual log events and inserts them into the
// buffer.
func (w *writerImpl) buffer(b []byte) (int, error) {
r := bufio.NewReader(bytes.NewReader(b))
var (
n int
eof bool
)
for !eof {
b, err := r.ReadBytes('\n')
if err != nil {
if err == io.EOF {
eof = true
} else {
break
}
}
if len(b) == 0 {
continue
}
event := &cloudwatchlogs.InputLogEvent{
Message: aws.String(string(b)),
Timestamp: aws.Int64(w.now().UnixNano() / 1000000),
}
if w.onEvent != nil {
w.onEvent(event)
}
w.events.add(event)
n += len(b)
}
return n, nil
}
func (w *writerImpl) now() time.Time {
if w.nowFunc == nil {
return time.Now()
}
return w.nowFunc()
}