-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcloudwatchcore.go
234 lines (200 loc) · 5.58 KB
/
cloudwatchcore.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
package zapcloudwatchcore
import (
"fmt"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"go.uber.org/zap/zapcore"
)
// CloudwatchCore is a zap Core for dispatching messages to the specified
type CloudwatchCore struct {
// Messages with a log level not contained in this array
// will not be dispatched. If nil, all messages will be dispatched.
AcceptedLevels []zapcore.Level
GroupName string
StreamName string
AWSConfig *aws.Config
nextSequenceToken *string
svc *cloudwatchlogs.CloudWatchLogs
Async bool // if async is true, send a message asynchronously.
m sync.Mutex
zapcore.LevelEnabler
enc zapcore.Encoder
out zapcore.WriteSyncer
}
type NewCloudwatchCoreParams struct {
GroupName string
StreamName string
IsAsync bool
Config *aws.Config
Level zapcore.Level
Enc zapcore.Encoder
Out zapcore.WriteSyncer
LevelEnabler zapcore.LevelEnabler
}
func NewCloudwatchCore(params *NewCloudwatchCoreParams) (zapcore.Core, error) {
core := &CloudwatchCore{
GroupName: params.GroupName,
StreamName: params.StreamName,
AWSConfig: params.Config,
Async: params.IsAsync,
AcceptedLevels: LevelThreshold(params.Level),
LevelEnabler: params.LevelEnabler,
enc: params.Enc,
out: params.Out,
}
err := core.cloudWatchInit()
if err != nil {
return nil, err
}
return core, nil
}
func (c *CloudwatchCore) With(fields []zapcore.Field) zapcore.Core {
clone := c.clone()
addFields(clone.enc, fields)
return clone
}
func (c *CloudwatchCore) clone() *CloudwatchCore {
return &CloudwatchCore{
GroupName: c.GroupName,
StreamName: c.StreamName,
AWSConfig: c.AWSConfig,
Async: c.Async,
AcceptedLevels: c.AcceptedLevels,
LevelEnabler: c.LevelEnabler,
enc: c.enc.Clone(),
out: c.out,
}
}
func addFields(enc zapcore.ObjectEncoder, fields []zapcore.Field) {
for i := range fields {
fields[i].AddTo(enc)
}
}
func (c *CloudwatchCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
if c.Enabled(ent.Level) {
return ce.AddCore(ent, c)
}
return ce
}
func (c *CloudwatchCore) Write(ent zapcore.Entry, fields []zapcore.Field) error {
buf, err := c.enc.EncodeEntry(ent, fields)
if err != nil {
return err
}
err = c.cloudwatchWriter(ent, buf.String())
buf.Free()
if err != nil {
return err
}
if ent.Level > zapcore.ErrorLevel {
// Since we may be crashing the program, sync the output. Ignore Sync
// errors, pending a clean solution to issue #370.
c.Sync()
}
return nil
}
func (c *CloudwatchCore) Sync() error {
return c.out.Sync()
}
func (c *CloudwatchCore) cloudwatchWriter(e zapcore.Entry, msg string) error {
if !c.isAcceptedLevel(e.Level) {
return nil
}
event := &cloudwatchlogs.InputLogEvent{
Message: aws.String(fmt.Sprintf("%s", msg)),
Timestamp: aws.Int64(int64(time.Nanosecond) * time.Now().UnixNano() / int64(time.Millisecond)),
}
params := &cloudwatchlogs.PutLogEventsInput{
LogEvents: []*cloudwatchlogs.InputLogEvent{event},
LogGroupName: aws.String(c.GroupName),
LogStreamName: aws.String(c.StreamName),
SequenceToken: c.nextSequenceToken,
}
if c.Async {
go c.sendEvent(params)
return nil
}
return c.sendEvent(params)
}
// GetHook function returns hook to zap
func (c *CloudwatchCore) cloudWatchInit() error {
c.svc = cloudwatchlogs.New(session.New(c.AWSConfig))
lgresp, err := c.svc.DescribeLogGroups(&cloudwatchlogs.DescribeLogGroupsInput{LogGroupNamePrefix: aws.String(c.GroupName), Limit: aws.Int64(1)})
if err != nil {
return err
}
if len(lgresp.LogGroups) < 1 {
// we need to create this log group
_, err := c.svc.CreateLogGroup(&cloudwatchlogs.CreateLogGroupInput{LogGroupName: aws.String(c.GroupName)})
if err != nil {
return err
}
}
resp, err := c.svc.DescribeLogStreams(&cloudwatchlogs.DescribeLogStreamsInput{
LogGroupName: aws.String(c.GroupName), // Required
LogStreamNamePrefix: aws.String(c.StreamName),
})
if err != nil {
return err
}
// grab the next sequence token
if len(resp.LogStreams) > 0 {
c.nextSequenceToken = resp.LogStreams[0].UploadSequenceToken
return nil
}
// create stream if it doesn't exist. the next sequence token will be null
_, err = c.svc.CreateLogStream(&cloudwatchlogs.CreateLogStreamInput{
LogGroupName: aws.String(c.GroupName),
LogStreamName: aws.String(c.StreamName),
})
if err != nil {
return err
}
return nil
}
func (c *CloudwatchCore) sendEvent(params *cloudwatchlogs.PutLogEventsInput) error {
c.m.Lock()
defer c.m.Unlock()
resp, err := c.svc.PutLogEvents(params)
if err != nil {
return err
}
c.nextSequenceToken = resp.NextSequenceToken
return nil
}
// Levels sets which levels to sent to cloudwatch
func (c *CloudwatchCore) Levels() []zapcore.Level {
if c.AcceptedLevels == nil {
return AllLevels
}
return c.AcceptedLevels
}
func (c *CloudwatchCore) isAcceptedLevel(level zapcore.Level) bool {
for _, lv := range c.Levels() {
if lv == level {
return true
}
}
return false
}
// AllLevels Supported log levels
var AllLevels = []zapcore.Level{
zapcore.DebugLevel,
zapcore.InfoLevel,
zapcore.WarnLevel,
zapcore.ErrorLevel,
zapcore.FatalLevel,
zapcore.PanicLevel,
}
// LevelThreshold - Returns every logging level above and including the given parameter.
func LevelThreshold(l zapcore.Level) []zapcore.Level {
for i := range AllLevels {
if AllLevels[i] == l {
return AllLevels[i:]
}
}
return []zapcore.Level{}
}