This repository has been archived by the owner on Oct 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
415 lines (348 loc) · 10 KB
/
main.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
package main
import (
"flag"
"fmt"
"io"
"os"
"os/signal"
"sort"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
"net/http"
_ "net/http/pprof"
"github.com/apex/log"
"github.com/apex/log/handlers/cli"
"github.com/apex/log/handlers/multi"
"github.com/segmentio/ecs-logs-go"
"github.com/segmentio/ecs-logs/lib"
_ "github.com/segmentio/ecs-logs/lib/cloudwatchlogs"
_ "github.com/segmentio/ecs-logs/lib/datadog"
_ "github.com/segmentio/ecs-logs/lib/logdna"
_ "github.com/segmentio/ecs-logs/lib/loggly"
_ "github.com/segmentio/ecs-logs/lib/statsd"
_ "github.com/segmentio/ecs-logs/lib/syslog"
)
type source struct {
lib.Source
name string
}
type destination struct {
lib.Destination
name string
}
type reader struct {
lib.Reader
name string
}
func main() {
var err error
var src string
var dst string
var hostname string
var level = lib.LogLevel(log.InfoLevel)
var maxBytes int
var maxCount int
var flushTimeout time.Duration
var cacheTimeout time.Duration
var profileAddr string
hostname, _ = os.Hostname()
flag.StringVar(&src, "src", "stdin", "A comma separated list of log sources from which messages will be read ["+strings.Join(lib.SourcesAvailable(), ", ")+"]")
flag.StringVar(&dst, "dst", "stdout", "A comma separated list of log destinations to which messages will be written ["+strings.Join(lib.DestinationsAvailable(), ", ")+"]")
flag.StringVar(&hostname, "hostname", hostname, "The hostname advertised by ecs-logs")
flag.Var(&level, "log-level", "The minimum level of log messages shown by ecs-logs")
flag.IntVar(&maxBytes, "max-batch-bytes", 1000000, "The maximum size in bytes of a message batch")
flag.IntVar(&maxCount, "max-batch-size", 10000, "The maximum number of messages in a batch")
flag.DurationVar(&flushTimeout, "flush-timeout", 5*time.Second, "How often messages will be flushed")
flag.DurationVar(&cacheTimeout, "cache-timeout", 5*time.Minute, "How to wait before clearing unused internal cache")
flag.StringVar(&profileAddr, "pprof-addr", "", "Address to serve profile information")
flag.Parse()
logger := &lib.LogHandler{
Group: "ecs-logs",
Stream: hostname,
Hostname: hostname,
Queue: lib.NewMessageQueue(),
}
log.SetLevel(log.Level(level))
log.SetHandler(multi.New(cli.New(os.Stderr), logger))
// serve profiles if address is configured
if profileAddr != "" {
go func() {
if err := http.ListenAndServe(profileAddr, nil); err != nil {
log.Errorf("pprof: %v", err)
}
}()
}
var store = lib.NewStore()
var sources []source
var readers []reader
var dests []destination
if len(hostname) == 0 {
log.Fatal("no hostname configured")
}
if sources = getSources(strings.Split(src, ",")); len(sources) == 0 {
log.Fatal("no or invalid log sources")
}
if dests = getDestinations(strings.Split(dst, ",")); len(dests) == 0 {
log.Fatal("no or invalid log destinations")
}
if readers, err = openSources(sources); err != nil {
log.WithError(err).Fatal("failed to open log sources readers")
}
join := &sync.WaitGroup{}
limits := lib.StreamLimits{
MaxCount: maxCount,
MaxBytes: maxBytes,
MaxTime: flushTimeout,
}
expchan := time.Tick(flushTimeout / 2)
msgchan := make(chan lib.Message, len(readers))
sigchan := make(chan os.Signal, 1)
counter := int32(len(readers))
startReaders(readers, msgchan, &counter, hostname)
setupSignals(sigchan)
for _, s := range sources {
log.WithField("source", s.name).Info("source enabled")
}
for _, d := range dests {
log.WithField("destination", d.name).Info("destination enabled")
}
for {
select {
case msg, ok := <-msgchan:
now := time.Now()
if !ok {
log.Info("waiting for all write operations to complete")
limits.Force = true
flushAll(dests, store, limits, now, join)
flushQueue(dests, store, logger.Queue, limits, now, join)
join.Wait()
return
}
_, stream := store.Add(msg, now)
flush(dests, stream, limits, now, join)
case <-logger.Queue.C:
now := time.Now()
flushQueue(dests, store, logger.Queue, limits, now, join)
case <-expchan:
now := time.Now()
flushAll(dests, store, limits, now, join)
removeExpired(dests, store, cacheTimeout, now)
case sig := <-sigchan:
log.WithFields(log.Fields{"signal": sig.String()}).Info("closing message readers")
stopReaders(readers)
}
}
}
func getSources(names []string) (sources []source) {
for i, src := range lib.GetSources(names...) {
sources = append(sources, source{
Source: src,
name: names[i],
})
}
if len(sources) != len(names) {
search:
for _, name := range names {
for _, source := range sources {
if name == source.name {
continue search
}
}
log.WithFields(log.Fields{"source": name}).Warn("source disabled")
}
}
return
}
func getDestinations(names []string) (destinations []destination) {
for i, dst := range lib.GetDestinations(names...) {
destinations = append(destinations, destination{
Destination: dst,
name: names[i],
})
}
if len(destinations) != len(names) {
search:
for _, name := range names {
for _, destination := range destinations {
if name == destination.name {
continue search
}
}
log.WithFields(log.Fields{"destination": name}).Warn("destination disabled")
}
}
return
}
func openSources(sources []source) (readers []reader, err error) {
readers = make([]reader, 0, len(sources))
for _, source := range sources {
if r, e := source.Open(); e != nil {
log.WithFields(log.Fields{
"source": source.name,
"error": e,
}).Error("failed to open log source")
} else {
readers = append(readers, reader{
Reader: r,
name: source.name,
})
}
}
if len(readers) == 0 {
err = fmt.Errorf("no sources to read from")
}
return
}
func setupSignals(sigchan chan<- os.Signal) {
signal.Notify(sigchan, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
}
func startReaders(readers []reader, msgchan chan<- lib.Message, counter *int32, hostname string) {
for _, reader := range readers {
go read(reader, msgchan, counter, hostname)
}
}
func stopReaders(readers []reader) {
for _, reader := range readers {
reader.Close()
}
}
func term(c chan<- lib.Message, counter *int32) {
if atomic.AddInt32(counter, -1) == 0 {
close(c)
}
}
func read(r reader, c chan<- lib.Message, counter *int32, hostname string) {
defer term(c, counter)
for {
var msg lib.Message
var err error
if msg, err = r.ReadMessage(); err != nil {
if err == io.EOF {
log.WithFields(log.Fields{
"reader": r.name,
}).Info("the message reader was closed")
} else {
log.WithFields(log.Fields{
"reader": r.name,
"error": err,
}).Error("the message reader failed")
}
return
}
if len(msg.Group) == 0 {
log.WithFields(log.Fields{
"reader": r.name,
"missing": "group",
}).Warn("dropping message because the a required field wasn't set")
continue
}
if len(msg.Stream) == 0 {
log.WithFields(log.Fields{
"reader": r.name,
"missing": "stream",
}).Warn("dropping message because the a required field wasn't set")
continue
}
if len(msg.Event.Info.Host) == 0 {
msg.Event.Info.Host = hostname
}
if msg.Event.Time == (time.Time{}) {
msg.Event.Time = time.Now()
}
if msg.Event.Data == nil {
msg.Event.Data = ecslogs.EventData{}
}
c <- msg
}
}
func write(dest destination, group, stream string, batch lib.MessageBatch, join *sync.WaitGroup) {
defer join.Done()
var writer lib.Writer
var err error
if writer, err = dest.Open(group, stream); err != nil {
logDropBatch(dest.name, group, stream, err, batch)
return
}
defer writer.Close()
if err = writer.WriteMessageBatch(batch); err != nil {
logDropBatch(dest.name, group, stream, err, batch)
return
}
}
func flush(dests []destination, stream *lib.Stream, limits lib.StreamLimits, now time.Time, join *sync.WaitGroup) {
for {
batch, reason := stream.Flush(limits, now)
if len(batch) == 0 {
break
}
// Ensure all messages in the batch are sorted. Checking if the batch is
// sorted is an optimization since in most cases the batch will be sorted
// because we're reading events that are generated live (checking for a
// sorted list is O(N) vs O(N*log(N)) for sorting it).
// There are cases where some log entries do appear unordered and this is
// causing issues with CloudWatchLogs.
if !sort.IsSorted(batch) {
sort.Stable(batch)
}
log.WithFields(log.Fields{
"group": stream.Group(),
"stream": stream.Name(),
"count": len(batch),
"reason": reason,
}).Info("flushing message batch")
for _, dest := range dests {
join.Add(1)
go write(dest, stream.Group(), stream.Name(), batch, join)
}
}
}
func flushAll(dests []destination, store *lib.Store, limits lib.StreamLimits, now time.Time, join *sync.WaitGroup) {
store.ForEach(func(group *lib.Group) {
group.ForEach(func(stream *lib.Stream) {
flush(dests, stream, limits, now, join)
})
})
}
func flushQueue(dests []destination, store *lib.Store, queue *lib.MessageQueue, limits lib.StreamLimits, now time.Time, join *sync.WaitGroup) {
streams := make(map[string]*lib.Stream)
for _, msg := range queue.Flush() {
_, stream := store.Add(msg, now)
key := stream.Group() + ":" + stream.Name()
if streams[key] == nil {
streams[key] = stream
}
}
for _, stream := range streams {
flush(dests, stream, limits, now, join)
}
}
func removeExpired(dests []destination, store *lib.Store, cacheTimeout time.Duration, now time.Time) {
for _, stream := range store.RemoveExpired(cacheTimeout, now) {
for _, dest := range dests {
dest.Close(stream.Group(), stream.Name())
}
log.WithFields(log.Fields{
"group": stream.Group(),
"stream": stream.Name(),
}).Info("removed expired stream")
}
}
func logDropBatch(dest string, group string, stream string, err error, batch lib.MessageBatch) {
log.WithFields(log.Fields{
"group": group,
"stream": stream,
"destination": dest,
"error": err,
"count": len(batch),
}).Error("dropping message batch")
for _, msg := range batch {
log.WithFields(log.Fields{
"group": msg.Group,
"stream": msg.Stream,
"event": msg.Event,
}).Debug("dropped")
}
}