-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.go
349 lines (296 loc) · 8.53 KB
/
logger.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
package logger
import (
"bufio"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"os"
"runtime"
"strings"
"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"
"strconv"
)
type (
Logger struct {
w io.Writer
// Service exposed for direct actions
Service *cloudwatchlogs.CloudWatchLogs
// logging tokens
group *string
stream *string
sequenceToken *string
// internal
sw *ScannerWriter
done chan struct{}
}
LogMessage struct {
Instance *string `json:"instance"`
Image *string `json:"image"`
Level *string `json:"level"`
Message *string `json:"message"`
}
)
const (
MaxMessageLength = 32 << 10
)
var (
// this is how long a batch will continue to be retried, in the event CloudWatch is
// not available. At which point the batch is dumped to stderr
MaxRetryTime = time.Hour
// the buffer length of the log event channel
EventLogBufferLength = 64 << 10
// this occurs when the buffered channel receiving log writes blocks
ErrStreamBackedUp = errors.New("stream backed up")
)
func NewLogger(sess *session.Session, endpoint, group, stream, level string, flushInterval time.Duration, image, instance string, x *bool) (*Logger, error) {
config := aws.NewConfig()
config.Endpoint = &endpoint
l := &Logger{
Service: cloudwatchlogs.New(sess, config),
group: &group,
stream: &stream,
done: make(chan struct{}),
}
events := make(chan *cloudwatchlogs.InputLogEvent, EventLogBufferLength)
go func() {
flushTime := time.NewTicker(flushInterval)
defer flushTime.Stop()
var logEvents []*cloudwatchlogs.InputLogEvent
for {
func() {
defer func() {
if e := recover(); e != nil {
fmt.Fprintln(os.Stderr, "panic:", e)
}
}()
select {
case e := <-events:
logEvents = append(logEvents, e)
case <-flushTime.C:
if len(logEvents) > 0 {
l.flush(logEvents, x)
logEvents = nil
}
case <-l.done:
for {
select {
case e := <-events:
logEvents = append(logEvents, e)
default:
l.flush(logEvents, x)
l.done <- struct{}{}
close(l.done)
runtime.Goexit()
}
}
}
}()
}
}()
l.sw = NewScannerWriter(bufio.ScanLines, MaxMessageLength, func(token []byte) error {
message := string(token)
m := &LogMessage{
Instance : &instance,
Image : &image,
Level : &level,
Message : &message}
json, _ := json.Marshal(m)
s := string(json)
if (*x) {
println("J: " + s)
}
e := &cloudwatchlogs.InputLogEvent{
Timestamp: aws.Int64(time.Now().UnixNano() / int64(time.Millisecond)),
Message: aws.String(s),
}
select {
case events <- e:
default:
// we're backed up, drop to stderr
fmt.Fprintf(os.Stderr, "%#v\n", e)
// this error will never be caught because
// no one ever checks the return values of log.* calls
// but return it anyway to be a good citizen
return ErrStreamBackedUp
}
return nil
})
return l, nil
}
func eventLength(e *cloudwatchlogs.InputLogEvent) int {
return len(*e.Message) + 26 // padding per spec
}
func (l *Logger) flush(logEvents []*cloudwatchlogs.InputLogEvent, x *bool) {
// The maximum rate of a PutLogEvents request is 5 requests per second per log stream.
rate := NewRateLimiter(5, time.Second)
defer rate.Close()
for len(logEvents) > 0 && rate.Ready() {
var (
batchSize int
batch []*cloudwatchlogs.InputLogEvent
)
// None of the log events in the batch can be more than 2 hours in the future.
// None of the log events in the batch can be older than 14 days or the retention period of the log group.
// The log events in the batch must be in chronological ordered by their timestamp.
const (
// The maximum batch size is 1,048,576 bytes, and this size is calculated as the sum of all messages in UTF-8, plus 26 bytes for each log entry.
MaxBatchSize = 1 << 20
// The maximum number of log events in a batch is 10,000.
MaxBatchCount = 10000
)
for batchSize < MaxBatchSize &&
len(batch) < MaxBatchCount &&
len(logEvents) > 0 {
batch = append(batch, logEvents[0])
batchSize += eventLength(logEvents[0])
logEvents = logEvents[1:]
}
input := &cloudwatchlogs.PutLogEventsInput{
LogEvents: batch,
LogGroupName: l.group,
LogStreamName: l.stream,
SequenceToken: l.sequenceToken,
}
if err := NewTrier(MaxRetryTime).TryFunc(func() (error, bool) {
if (*x) {
println("P: " + strconv.Itoa(len(input.LogEvents)))
}
resp, err := l.Service.PutLogEvents(input)
if (*x) {
println(fmt.Sprintf("R: %v", resp))
println(fmt.Sprintf("E: %v", err))
}
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
switch awsErr.Code() {
case "DataAlreadyAcceptedException":
fmt.Fprintln(os.Stderr, "batch already added..")
return nil, false
case "ResourceNotFoundException":
fmt.Fprintln(os.Stderr, "group or stream not found, creating...")
if _, err := l.Service.CreateLogGroup(&cloudwatchlogs.CreateLogGroupInput{
LogGroupName: l.group,
}); err != nil {
fmt.Fprintf(os.Stderr, "create group err: %v", err)
}
if _, err = l.Service.CreateLogStream(&cloudwatchlogs.CreateLogStreamInput{
LogGroupName: l.group,
LogStreamName: l.stream,
}); err != nil {
fmt.Fprintf(os.Stderr, "create stream err: %v", err)
}
return errors.New("retry"), true
case "InvalidSequenceTokenException":
// parse token from error (jank aws)
// The given sequenceToken is invalid. The next expected sequenceToken is: 49540114571107725906840645449746451546762543407852177650
msg := awsErr.Message()
if i := strings.LastIndex(msg, " "); i > -1 {
token := strings.TrimSpace(msg[i:])
input.SequenceToken = &token
}
return err, true
// Returned if a parameter of the request is incorrectly specified.
case "InvalidParameterException":
fmt.Fprintln(os.Stderr, "aws error", awsErr.Code(), awsErr.Message(), awsErr.OrigErr())
return err, false
}
fmt.Fprintln(os.Stderr, "aws error", awsErr.Code(), awsErr.Message(), awsErr.OrigErr())
fmt.Fprintln(os.Stderr, "retrying...")
return err, true
}
// Generic AWS error with Code, Message, and original error (if any)
if reqErr, ok := err.(awserr.RequestFailure); ok {
// A Service error occurred
fmt.Fprintln(os.Stderr, "aws fail", reqErr.Code(), reqErr.Message(), reqErr.OrigErr())
return reqErr, false
}
// This case should never be hit, the SDK should always return an
// error which satisfies the awserr.Error interface.
fmt.Fprintf(os.Stderr, "unexpected err: %v\n", err)
return err, false
}
l.sequenceToken = resp.NextSequenceToken
return nil, false
}); err != nil {
failBatch(batch)
}
}
}
func failBatch(batch []*cloudwatchlogs.InputLogEvent) {
// batch failed, drop it and move on
fmt.Fprint(os.Stderr, "batch failed: ")
if err := json.NewEncoder(os.Stderr).Encode(batch); err != nil {
fmt.Fprintf(os.Stderr, "%#v\n", batch)
}
}
func (l *Logger) Write(b []byte) (int, error) {
return l.sw.Write(b)
}
func (l *Logger) WriteJSON(v interface{}) error {
return json.NewEncoder(l).Encode(v)
}
func (l *Logger) WriteRoundTrip(resp *http.Response, duration time.Duration) error {
type (
Request struct {
Method string
URL *url.URL
Header http.Header
ContentLength int64
}
Response struct {
StatusCode int
Header http.Header
ContentLength int64
}
Payload struct {
Type string
Request Request
Response Response
Duration time.Duration
}
)
return l.WriteJSON(&Payload{
Type: "roundtrip",
Request: Request{
Method: resp.Request.Method,
URL: resp.Request.URL,
Header: resp.Request.Header,
ContentLength: resp.Request.ContentLength,
},
Response: Response{
StatusCode: resp.StatusCode,
Header: resp.Header,
ContentLength: resp.ContentLength,
},
Duration: duration,
})
}
func (l *Logger) WriteError(err error) error {
type Payload struct {
Type string
FunctionName string
FileName string
Line int
Error string
}
pc, fn, line, _ := runtime.Caller(1)
return l.WriteJSON(&Payload{
Type: "error",
FunctionName: runtime.FuncForPC(pc).Name(),
FileName: fn,
Line: line,
Error: err.Error(),
})
}
func (l *Logger) Close() error {
l.done <- struct{}{}
<-l.done
return nil
}