-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathwriter.go
120 lines (105 loc) · 3.13 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
package main
import (
"encoding/json"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
awsSession "github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
)
type Writer struct {
conn *cloudwatchlogs.CloudWatchLogs
logGroupName string
logStreamName string
nextSequenceToken string
}
func NewWriter(sess *awsSession.Session, logGroupName, logStreamName, firstSeqToken string) (*Writer, error) {
conn := cloudwatchlogs.New(sess)
return &Writer{
conn: conn,
logGroupName: logGroupName,
logStreamName: logStreamName,
nextSequenceToken: firstSeqToken,
}, nil
}
func (w *Writer) WriteBatch(records []Record) (string, error) {
events := make([]*cloudwatchlogs.InputLogEvent, 0, len(records))
for _, record := range records {
jsonDataBytes, err := json.MarshalIndent(record, "", " ")
if err != nil {
return "", err
}
jsonData := string(jsonDataBytes)
events = append(events, &cloudwatchlogs.InputLogEvent{
Message: aws.String(jsonData),
Timestamp: aws.Int64(int64(record.TimeUsec)),
})
}
putEvents := func() error {
request := &cloudwatchlogs.PutLogEventsInput{
LogEvents: events,
LogGroupName: &w.logGroupName,
LogStreamName: &w.logStreamName,
}
if w.nextSequenceToken != "" {
request.SequenceToken = aws.String(w.nextSequenceToken)
}
result, err := w.conn.PutLogEvents(request)
if err != nil {
return err
}
w.nextSequenceToken = *result.NextSequenceToken
return nil
}
createStream := func() error {
request := &cloudwatchlogs.CreateLogStreamInput{
LogGroupName: &w.logGroupName,
LogStreamName: &w.logStreamName,
}
_, err := w.conn.CreateLogStream(request)
return err
}
err := putEvents()
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "ResourceNotFoundException" {
// Maybe our log stream doesn't exist yet. We'll try
// to create it and then, if we're successful, try
// writing the events again.
err := createStream()
if err != nil {
return "", fmt.Errorf("failed to create stream: %s", err)
}
err = putEvents()
if err != nil {
return "", fmt.Errorf("failed to put events: %s", err)
}
return w.nextSequenceToken, nil
}
if awsErr.Code() == "DataAlreadyAcceptedException" {
// This batch was already sent
return "", nil
}
if awsErr.Code() == "InvalidSequenceTokenException" {
request := &cloudwatchlogs.DescribeLogStreamsInput{
LogGroupName: &w.logGroupName,
LogStreamNamePrefix: &w.logStreamName,
Descending: aws.Bool(true),
Limit: aws.Int64(1),
}
result, err := w.conn.DescribeLogStreams(request)
if err != nil {
return "", fmt.Errorf("failed to get next sequence token: %s", err)
}
w.nextSequenceToken = *(result.LogStreams[0].UploadSequenceToken)
err = putEvents()
if err != nil {
return "", fmt.Errorf("failed to put events: %s", err)
}
return w.nextSequenceToken, nil
}
}
return "", fmt.Errorf("failed to put events: %s", err)
}
return w.nextSequenceToken, nil
}