This repository has been archived by the owner on Oct 5, 2023. It is now read-only.
/
client.go
177 lines (144 loc) · 4.09 KB
/
client.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
package cloudwatchlogs
import (
"fmt"
"sync"
"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/segmentio/ecs-logs/lib"
)
type client struct {
cmtx sync.Mutex
client *cloudwatchlogs.CloudWatchLogs
wmtx sync.Mutex
writers map[string]*writer
}
func newClient() *client {
return &client{
writers: make(map[string]*writer, 100),
}
}
func (c *client) Open(group string, stream string) (w lib.Writer, err error) {
var client *cloudwatchlogs.CloudWatchLogs
var token string
var writer = c.get(group, stream)
w = writer
writer.mutex.Lock()
defer writer.mutex.Unlock()
if len(writer.token) != 0 {
// The writer already has a token, this means the log group and streams
// have been created for that writer already.
return
}
if client, err = c.getAwsClient(); err != nil {
return
}
if token, err = createGroupAndStream(client, group, stream); err != nil {
// Creating the log group or stream failed, this writer cannot be used.
delete(c.writers, joinGroupStream(group, stream))
return
}
writer.token = token
return
}
func (c *client) Close(group string, stream string) {
c.remove(group, stream)
}
func (c *client) get(group string, stream string) (w *writer) {
key := joinGroupStream(group, stream)
c.wmtx.Lock()
if w = c.writers[key]; w == nil {
w = &writer{
group: group,
stream: stream,
parent: c,
}
c.writers[key] = w
}
c.wmtx.Unlock()
return
}
func (c *client) remove(group string, stream string) {
key := joinGroupStream(group, stream)
c.wmtx.Lock()
delete(c.writers, key)
c.wmtx.Unlock()
}
func (c *client) getAwsClient() (client *cloudwatchlogs.CloudWatchLogs, err error) {
c.cmtx.Lock()
defer c.cmtx.Unlock()
if client = c.client; client == nil {
if client, err = openAwsClient(); err != nil {
return
}
c.client = client
}
return
}
func openAwsClient() (client *cloudwatchlogs.CloudWatchLogs, err error) {
var region string
if region, err = getAwsRegion(); err != nil {
return
}
client = cloudwatchlogs.New(session.New(&aws.Config{
Region: aws.String(region),
}))
return
}
func createGroupAndStream(client *cloudwatchlogs.CloudWatchLogs, group string, stream string) (token string, err error) {
var result *cloudwatchlogs.DescribeLogStreamsOutput
if _, err := client.CreateLogGroup(&cloudwatchlogs.CreateLogGroupInput{
LogGroupName: aws.String(group),
}); err != nil && !isAlreadyExists(err) {
return "", err
}
_, err = client.CreateLogStream(&cloudwatchlogs.CreateLogStreamInput{
LogGroupName: aws.String(group),
LogStreamName: aws.String(stream),
})
if err == nil {
// Log stream successfully created. No token need be provided.
return "", nil
} else if !isAlreadyExists(err) {
return "", err
}
if result, err = client.DescribeLogStreams(&cloudwatchlogs.DescribeLogStreamsInput{
Limit: aws.Int64(1),
LogGroupName: aws.String(group),
LogStreamNamePrefix: aws.String(stream),
}); err != nil {
if isThrottled(err) {
// The documentation says that we can only make 5 calls per second to
// this endpoint, but we need the sequence token in order to send events
// to streams that already exist.
//
// If we fail to fetch the stream description we still move on without
// a token and let the retry logic around PutLogEvents attempt to handle
// the issue.
return "", nil
}
return "", err
}
// This should be an invariant
if len(result.LogStreams) == 0 {
return "", fmt.Errorf("Assertion failure: Log stream %s: %s not found",
group, stream)
}
return aws.StringValue(result.LogStreams[0].UploadSequenceToken), nil
}
func joinGroupStream(group string, stream string) string {
return group + ":" + stream
}
func isAwsErrorCode(err error, code string) bool {
if err, ok := err.(awserr.Error); ok && err.Code() == code {
return true
}
return false
}
func isAlreadyExists(err error) bool {
return isAwsErrorCode(err, "ResourceAlreadyExistsException")
}
func isThrottled(err error) bool {
return isAwsErrorCode(err, "ThrottlingException")
}