-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
pusher.go
421 lines (352 loc) · 12.7 KB
/
pusher.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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package cwlogs // import "github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/cwlogs"
import (
"errors"
"sort"
"sync"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"go.uber.org/zap"
)
const (
// http://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/cloudwatch_limits_cwl.html
// In truncation logic, it assuming this constant value is larger than perEventHeaderBytes + len(truncatedSuffix)
defaultMaxEventPayloadBytes = 1024 * 256 // 256KB
// http://docs.aws.amazon.com/AmazonCloudWatchLogs/latest/APIReference/API_PutLogEvents.html
maxRequestEventCount = 10000
perEventHeaderBytes = 26
maxRequestPayloadBytes = 1024 * 1024 * 1
truncatedSuffix = "[Truncated...]"
eventTimestampLimitInPast = 14 * 24 * time.Hour // None of the log events in the batch can be older than 14 days
evenTimestampLimitInFuture = -2 * time.Hour // None of the log events in the batch can be more than 2 hours in the future.
)
var (
maxEventPayloadBytes = defaultMaxEventPayloadBytes
)
// Event struct to present a log event.
type Event struct {
InputLogEvent *cloudwatchlogs.InputLogEvent
// The time which log generated.
GeneratedTime time.Time
// Identify what is the stream of destination of this event
StreamKey
}
// NewEvent creates a new log event
// logType will be propagated to LogEventBatch and used by logPusher to determine which client to call PutLogEvent
func NewEvent(timestampMs int64, message string) *Event {
event := &Event{
InputLogEvent: &cloudwatchlogs.InputLogEvent{
Timestamp: aws.Int64(timestampMs),
Message: aws.String(message)},
}
return event
}
// Uniquely identify a cloudwatch logs stream
type StreamKey struct {
LogGroupName string
LogStreamName string
}
func (logEvent *Event) Validate(logger *zap.Logger) error {
if logEvent.eventPayloadBytes() > maxEventPayloadBytes {
logger.Warn("logpusher: the single log event size is larger than the max event payload allowed. Truncate the log event.",
zap.Int("SingleLogEventSize", logEvent.eventPayloadBytes()), zap.Int("maxEventPayloadBytes", maxEventPayloadBytes))
newPayload := (*logEvent.InputLogEvent.Message)[0:(maxEventPayloadBytes - perEventHeaderBytes - len(truncatedSuffix))]
newPayload += truncatedSuffix
logEvent.InputLogEvent.Message = &newPayload
}
if *logEvent.InputLogEvent.Timestamp == int64(0) {
logEvent.InputLogEvent.Timestamp = aws.Int64(logEvent.GeneratedTime.UnixNano() / int64(time.Millisecond))
}
if len(*logEvent.InputLogEvent.Message) == 0 {
return errors.New("empty log event message")
}
// http://docs.aws.amazon.com/goto/SdkForGoV1/logs-2014-03-28/PutLogEvents
// * 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.
currentTime := time.Now().UTC()
utcTime := time.Unix(0, *logEvent.InputLogEvent.Timestamp*int64(time.Millisecond)).UTC()
duration := currentTime.Sub(utcTime)
if duration > eventTimestampLimitInPast || duration < evenTimestampLimitInFuture {
err := errors.New("the log entry's timestamp is older than 14 days or more than 2 hours in the future")
logger.Error("discard log entry with invalid timestamp",
zap.Error(err), zap.String("LogEventTimestamp", utcTime.String()), zap.String("CurrentTime", currentTime.String()))
return err
}
return nil
}
// Calculate the log event payload bytes.
func (logEvent *Event) eventPayloadBytes() int {
return len(*logEvent.InputLogEvent.Message) + perEventHeaderBytes
}
// eventBatch struct to present a log event batch
type eventBatch struct {
putLogEventsInput *cloudwatchlogs.PutLogEventsInput
// the total bytes already in this log event batch
byteTotal int
// min timestamp recorded in this log event batch (ms)
minTimestampMs int64
// max timestamp recorded in this log event batch (ms)
maxTimestampMs int64
}
// Create a new log event batch if needed.
func newEventBatch(key StreamKey) *eventBatch {
return &eventBatch{
putLogEventsInput: &cloudwatchlogs.PutLogEventsInput{
LogGroupName: aws.String(key.LogGroupName),
LogStreamName: aws.String(key.LogStreamName),
LogEvents: make([]*cloudwatchlogs.InputLogEvent, 0, maxRequestEventCount)},
}
}
func (batch eventBatch) exceedsLimit(nextByteTotal int) bool {
return len(batch.putLogEventsInput.LogEvents) == cap(batch.putLogEventsInput.LogEvents) ||
batch.byteTotal+nextByteTotal > maxEventPayloadBytes
}
// isActive checks whether the eventBatch spans more than 24 hours. Returns
// false if the condition does not match, and this batch should not be processed
// any further.
func (batch *eventBatch) isActive(targetTimestampMs *int64) bool {
// new log event batch
if batch.minTimestampMs == 0 || batch.maxTimestampMs == 0 {
return true
}
if *targetTimestampMs-batch.minTimestampMs > 24*3600*1e3 {
return false
}
if batch.maxTimestampMs-*targetTimestampMs > 24*3600*1e3 {
return false
}
return true
}
func (batch *eventBatch) append(event *Event) {
batch.putLogEventsInput.LogEvents = append(batch.putLogEventsInput.LogEvents, event.InputLogEvent)
batch.byteTotal += event.eventPayloadBytes()
if batch.minTimestampMs == 0 || batch.minTimestampMs > *event.InputLogEvent.Timestamp {
batch.minTimestampMs = *event.InputLogEvent.Timestamp
}
if batch.maxTimestampMs == 0 || batch.maxTimestampMs < *event.InputLogEvent.Timestamp {
batch.maxTimestampMs = *event.InputLogEvent.Timestamp
}
}
// Sort the log events based on the timestamp.
func (batch *eventBatch) sortLogEvents() {
inputLogEvents := batch.putLogEventsInput.LogEvents
sort.Stable(ByTimestamp(inputLogEvents))
}
type ByTimestamp []*cloudwatchlogs.InputLogEvent
func (inputLogEvents ByTimestamp) Len() int {
return len(inputLogEvents)
}
func (inputLogEvents ByTimestamp) Swap(i, j int) {
inputLogEvents[i], inputLogEvents[j] = inputLogEvents[j], inputLogEvents[i]
}
func (inputLogEvents ByTimestamp) Less(i, j int) bool {
return *inputLogEvents[i].Timestamp < *inputLogEvents[j].Timestamp
}
// Pusher is created by log group and log stream
type Pusher interface {
AddLogEntry(logEvent *Event) error
ForceFlush() error
}
// Struct of logPusher implemented Pusher interface.
type logPusher struct {
logger *zap.Logger
// log group name of the current logPusher
logGroupName *string
// log stream name of the current logPusher
logStreamName *string
logEventBatch *eventBatch
svcStructuredLog Client
retryCnt int
}
// NewPusher creates a logPusher instance
func NewPusher(streamKey StreamKey, retryCnt int,
svcStructuredLog Client, logger *zap.Logger) Pusher {
pusher := newLogPusher(streamKey, svcStructuredLog, logger)
pusher.retryCnt = defaultRetryCount
if retryCnt > 0 {
pusher.retryCnt = retryCnt
}
return pusher
}
// Only create a logPusher, but not start the instance.
func newLogPusher(streamKey StreamKey,
svcStructuredLog Client, logger *zap.Logger) *logPusher {
pusher := &logPusher{
logGroupName: aws.String(streamKey.LogGroupName),
logStreamName: aws.String(streamKey.LogStreamName),
svcStructuredLog: svcStructuredLog,
logger: logger,
}
pusher.logEventBatch = newEventBatch(streamKey)
return pusher
}
// AddLogEntry Besides the limit specified by PutLogEvents API, there are some overall limit for the cloudwatchlogs
// listed here: http://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/cloudwatch_limits_cwl.html
//
// Need to pay attention to the below 2 limits:
// Event size 256 KB (maximum). This limit cannot be changed.
// Batch size 1 MB (maximum). This limit cannot be changed.
func (p *logPusher) AddLogEntry(logEvent *Event) error {
var err error
if logEvent != nil {
err = logEvent.Validate(p.logger)
if err != nil {
return err
}
prevBatch := p.addLogEvent(logEvent)
if prevBatch != nil {
err = p.pushEventBatch(prevBatch)
}
}
return err
}
func (p *logPusher) ForceFlush() error {
prevBatch := p.renewEventBatch()
if prevBatch != nil {
return p.pushEventBatch(prevBatch)
}
return nil
}
func (p *logPusher) pushEventBatch(req any) error {
// http://docs.aws.amazon.com/goto/SdkForGoV1/logs-2014-03-28/PutLogEvents
// The log events in the batch must be in chronological ordered by their
// timestamp (the time the event occurred, expressed as the number of milliseconds
// since Jan 1, 1970 00:00:00 UTC).
logEventBatch := req.(*eventBatch)
logEventBatch.sortLogEvents()
putLogEventsInput := logEventBatch.putLogEventsInput
startTime := time.Now()
err := p.svcStructuredLog.PutLogEvents(putLogEventsInput, p.retryCnt)
if err != nil {
return err
}
p.logger.Debug("logpusher: publish log events successfully.",
zap.Int("NumOfLogEvents", len(putLogEventsInput.LogEvents)),
zap.Float64("LogEventsSize", float64(logEventBatch.byteTotal)/float64(1024)),
zap.Int64("Time", time.Since(startTime).Nanoseconds()/int64(time.Millisecond)))
return nil
}
func (p *logPusher) addLogEvent(logEvent *Event) *eventBatch {
if logEvent == nil {
return nil
}
var prevBatch *eventBatch
currentBatch := p.logEventBatch
if currentBatch.exceedsLimit(logEvent.eventPayloadBytes()) || !currentBatch.isActive(logEvent.InputLogEvent.Timestamp) {
prevBatch = currentBatch
currentBatch = newEventBatch(StreamKey{
LogGroupName: *p.logGroupName,
LogStreamName: *p.logStreamName,
})
}
currentBatch.append(logEvent)
p.logEventBatch = currentBatch
return prevBatch
}
func (p *logPusher) renewEventBatch() *eventBatch {
var prevBatch *eventBatch
if len(p.logEventBatch.putLogEventsInput.LogEvents) > 0 {
prevBatch = p.logEventBatch
p.logEventBatch = newEventBatch(StreamKey{
LogGroupName: *p.logGroupName,
LogStreamName: *p.logStreamName,
})
}
return prevBatch
}
// A Pusher that is able to send events to multiple streams.
type multiStreamPusher struct {
logStreamManager LogStreamManager
client Client
pusherMap map[StreamKey]Pusher
logger *zap.Logger
}
func newMultiStreamPusher(logStreamManager LogStreamManager, client Client, logger *zap.Logger) *multiStreamPusher {
return &multiStreamPusher{
logStreamManager: logStreamManager,
client: client,
logger: logger,
pusherMap: make(map[StreamKey]Pusher),
}
}
func (m *multiStreamPusher) AddLogEntry(event *Event) error {
if err := m.logStreamManager.InitStream(event.StreamKey); err != nil {
return err
}
var pusher Pusher
var ok bool
if pusher, ok = m.pusherMap[event.StreamKey]; !ok {
pusher = NewPusher(event.StreamKey, 1, m.client, m.logger)
m.pusherMap[event.StreamKey] = pusher
}
return pusher.AddLogEntry(event)
}
func (m *multiStreamPusher) ForceFlush() error {
var errs []error
for _, val := range m.pusherMap {
err := val.ForceFlush()
if err != nil {
errs = append(errs, err)
}
}
if len(errs) != 0 {
return errors.Join(errs...)
}
return nil
}
// Factory for a Pusher that has capability of sending events to multiple log streams
type MultiStreamPusherFactory interface {
CreateMultiStreamPusher() Pusher
}
type multiStreamPusherFactory struct {
logStreamManager LogStreamManager
logger *zap.Logger
client Client
}
// Creates a new MultiStreamPusherFactory
func NewMultiStreamPusherFactory(logStreamManager LogStreamManager, client Client, logger *zap.Logger) MultiStreamPusherFactory {
return &multiStreamPusherFactory{
logStreamManager: logStreamManager,
client: client,
logger: logger,
}
}
// Factory method to create a Pusher that has support to sending events to multiple log streams
func (msf *multiStreamPusherFactory) CreateMultiStreamPusher() Pusher {
return newMultiStreamPusher(msf.logStreamManager, msf.client, msf.logger)
}
// Manages the creation of streams
type LogStreamManager interface {
// Initialize a stream so that it can receive logs
// This will make sure that the stream exists and if it does not exist,
// It will create one. Implementations of this method MUST be safe for concurrent use.
InitStream(streamKey StreamKey) error
}
type logStreamManager struct {
logStreamMutex sync.Mutex
streams map[StreamKey]bool
client Client
}
func NewLogStreamManager(svcStructuredLog Client) LogStreamManager {
return &logStreamManager{
client: svcStructuredLog,
streams: make(map[StreamKey]bool),
}
}
func (lsm *logStreamManager) InitStream(streamKey StreamKey) error {
if _, ok := lsm.streams[streamKey]; !ok {
lsm.logStreamMutex.Lock()
defer lsm.logStreamMutex.Unlock()
if _, ok := lsm.streams[streamKey]; !ok {
err := lsm.client.CreateStream(&streamKey.LogGroupName, &streamKey.LogStreamName)
lsm.streams[streamKey] = true
return err
}
}
return nil
// does not do anything if stream already exists
}