-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream.go
195 lines (161 loc) · 5.24 KB
/
stream.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
package cwl
import (
"bytes"
"fmt"
"regexp"
"time"
"github.com/elastic/beats/libbeat/logp"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
)
type Stream struct {
Name string
Group *Group
Params *Params
queryParams *cloudwatchlogs.GetLogEventsInput
// This is used for multi line mode. We store all text needed until we find
// the end of message
buffer bytes.Buffer
multiline *Multiline
multiRegex *regexp.Regexp // cached regex for performance
LastEventTimestamp int64 // the last event that we've processed (in milliseconds since 1970)
finished chan<- bool // channel for the stream to signal that its processing is over
publishedEvents int64 // number of published events
}
func NewStream(name string, group *Group, multiline *Multiline, finished chan<- bool, params *Params) *Stream {
startTime := time.Now().UTC().Add(-params.Config.StreamEventHorizon)
queryParams := &cloudwatchlogs.GetLogEventsInput{
LogGroupName: aws.String(group.Name),
LogStreamName: aws.String(name),
StartFromHead: aws.Bool(true),
Limit: aws.Int64(params.Config.StreamEventLimit),
StartTime: aws.Int64(startTime.UnixNano() / 1e6),
}
stream := &Stream{
Name: name,
Group: group,
Params: params,
queryParams: queryParams,
multiline: multiline,
finished: finished,
LastEventTimestamp: 1000 * time.Now().Unix(),
}
// Construct regular expression if multiline mode
var regx *regexp.Regexp
var err error
if stream.multiline != nil {
regx, err = regexp.Compile(stream.multiline.Pattern)
Fatal(err)
}
stream.multiRegex = regx
return stream
}
// Next fetches the next batch of events from the cloudwatchlogs stream
// returns the error (if any) otherwise nil
func (stream *Stream) Next() error {
var err error
output, err := stream.Params.AWSClient.GetLogEvents(stream.queryParams)
if err != nil {
return err
}
// have we got anything new?
if len(output.Events) == 0 {
return nil
}
// process the events
for _, streamEvent := range output.Events {
stream.digest(streamEvent)
stream.LastEventTimestamp = aws.Int64Value(streamEvent.Timestamp)
}
stream.queryParams.NextToken = output.NextForwardToken
err = stream.Params.Registry.WriteStreamInfo(stream)
return err
}
// Monitor continuously monitors the stream for new events. If an error is
// encountered, monitoring will stop and the stream will send an event
// to the finished channel for the group to cleanup
func (stream *Stream) Monitor() {
logp.Info("[stream] %s started", stream.FullName())
defer func() {
logp.Info("[stream] %s stopped", stream.FullName())
stream.finished <- true
}()
// first of all, read the stream's info from our registry storage
err := stream.Params.Registry.ReadStreamInfo(stream)
if err != nil {
logp.Err("[stream] %s encountered err on reading stream info. %s", stream.FullName(), err.Error())
return
}
reportTicker := time.NewTicker(stream.Params.Config.ReportFrequency)
defer reportTicker.Stop()
var eventRefreshFrequency = stream.Params.Config.StreamEventRefreshFrequency
for {
err := stream.Next()
if err != nil {
logp.Err("[stream] %s encountered err on next. %s", stream.FullName(), err.Error())
return
}
// is the stream expired?
if IsBefore(stream.Params.Config.StreamEventHorizon, stream.LastEventTimestamp) {
logp.Info("[stream] %s is expired", stream.FullName())
return
}
// is the stream "hot"?
if stream.IsHot(stream.LastEventTimestamp) {
eventRefreshFrequency = stream.Params.Config.HotStreamEventRefreshFrequency
} else {
eventRefreshFrequency = stream.Params.Config.StreamEventRefreshFrequency
}
select {
case <-reportTicker.C:
stream.report()
default:
time.Sleep(eventRefreshFrequency)
}
}
}
func (stream *Stream) IsHot(lastEventTimestamp int64) bool {
return !IsBefore(stream.Params.Config.HotStreamEventHorizon, lastEventTimestamp)
}
func (stream *Stream) report() {
logp.Info("report[stream] %s published events: %d, frequency: %s",
stream.FullName(), stream.publishedEvents, stream.Params.Config.ReportFrequency)
stream.publishedEvents = 0
}
func (stream *Stream) FullName() string {
return fmt.Sprintf("%s/%s", stream.Group.Name, stream.Name)
}
// fills the buffer's contents into the event,
// publishes the message and empties the buffer
func (stream *Stream) publish(event *Event) {
if stream.buffer.Len() == 0 {
return
}
event.Message = stream.buffer.String()
stream.Params.Publisher.Publish(event)
stream.buffer.Reset()
stream.publishedEvents++
}
func (stream *Stream) digest(streamEvent *cloudwatchlogs.OutputLogEvent) {
event := &Event{
Stream: stream,
Timestamp: aws.Int64Value(streamEvent.Timestamp),
}
if stream.multiline == nil {
stream.buffer.WriteString(*streamEvent.Message)
stream.publish(event)
} else {
switch stream.multiline.Match {
case "after":
if stream.multiRegex.MatchString(*streamEvent.Message) == stream.multiline.Negate {
stream.publish(event)
}
stream.buffer.WriteString(*streamEvent.Message)
case "before":
stream.buffer.WriteString(*streamEvent.Message)
if stream.multiRegex.MatchString(*streamEvent.Message) == stream.multiline.Negate {
stream.publish(event)
}
}
}
}