/
container.go
393 lines (328 loc) · 10.6 KB
/
container.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
package docker
import (
"bytes"
"context"
"encoding/binary"
"fmt"
"io"
"os"
"strings"
"syscall"
"time"
log "github.com/Sirupsen/logrus"
"github.com/docker/docker/api/types/plugins/logdriver"
"github.com/docker/docker/daemon/logger"
protoio "github.com/gogo/protobuf/io"
"github.com/rchicoli/docker-log-elasticsearch/pkg/elasticsearch"
"github.com/rchicoli/docker-log-elasticsearch/pkg/extension/grok"
"github.com/robfig/cron"
"github.com/tonistiigi/fifo"
"golang.org/x/sync/errgroup"
)
type container struct {
// bulkService map[int]*BulkWorker
cron *cron.Cron
esClient elasticsearch.Client
indexName string
logger *log.Entry
pipeline pipeline
stream io.ReadCloser
}
type pipeline struct {
// commitCh chan struct{}
group *errgroup.Group
inputCh chan logdriver.LogEntry
outputCh chan LogMessage
}
// Processor interface
// type Processor interface {
// Read(ctx context.Context) error
// Parse(ctx context.Context, info logger.Info, fields, grokMatch, grokPattern, grokPatternFrom, grokPatternSplitter string, grokNamedCapture bool) error
// Log(ctx context.Context, workers int, indexName, tzpe string, actions, bulkSize int, flushInterval, timeout time.Duration) error
// }
// newContainer stores the container's configuration in memory
// and returns a pointer to the container
func newContainer(ctx context.Context, file, containerID string) (*container, error) {
f, err := fifo.OpenFifo(ctx, file, syscall.O_RDONLY, 0700)
if err != nil {
return nil, fmt.Errorf("could not open fifo: %q", err)
}
return &container{
// bulkService: make(map[int]*BulkWorker),
stream: f,
logger: log.WithField("containerID", containerID),
pipeline: pipeline{
// commitCh: make(chan struct{}),
inputCh: make(chan logdriver.LogEntry),
outputCh: make(chan LogMessage),
},
}, nil
}
// Read reads messages from proto buffer
func (c *container) Read(ctx context.Context) error {
c.logger.Debug("starting pipeline: Read")
c.pipeline.group.Go(func() error {
dec := protoio.NewUint32DelimitedReader(c.stream, binary.BigEndian, 1e6)
defer func() {
c.logger.Info("closing docker stream")
dec.Close()
close(c.pipeline.inputCh)
}()
var buf logdriver.LogEntry
var err error
for {
if err = dec.ReadMsg(&buf); err != nil {
if err == io.EOF {
c.logger.Debug("shutting down reader eof")
return nil
}
// the connection has been closed
// stop looping and close the input channel
// read /proc/self/fd/6: file already closed
if strings.Contains(err.Error(), os.ErrClosed.Error()) {
c.logger.WithError(err).Debug("shutting down fifo: closed by the writer")
// shutdown gracefully all pipelines
return nil
}
if err != nil {
// the connection has been closed
// stop looping and closing the input channel
// read /proc/self/fd/6: file already closed
c.logger.WithError(err).Debug("shutting down fifo")
return err
}
dec = protoio.NewUint32DelimitedReader(c.stream, binary.BigEndian, 1e6)
}
// in case docker run command throws lots empty line messages
if len(bytes.TrimSpace(buf.Line)) == 0 {
c.logger.WithField("line", string(buf.Line)).Debug("trim space")
continue
}
select {
case c.pipeline.inputCh <- buf:
case <-ctx.Done():
c.logger.WithError(ctx.Err()).Error("closing read pipeline: Read")
return ctx.Err()
}
buf.Reset()
}
})
return nil
}
// Parse filters line messages
func (c *container) Parse(ctx context.Context, info logger.Info, fields, grokMatch, grokPattern, grokPatternFrom, grokPatternSplitter string, grokNamedCapture bool) error {
c.logger.Debug("starting pipeline: Parse")
c.pipeline.group.Go(func() error {
defer close(c.pipeline.outputCh)
groker, err := grok.NewGrok(grokMatch, grokPattern, grokPatternFrom, grokPatternSplitter, grokNamedCapture)
if err != nil {
return err
}
var logMessage string
// custom log message fields
msg := getLogMessageFields(fields, info)
for m := range c.pipeline.inputCh {
logMessage = string(m.Line)
// create message
msg.Source = m.Source
msg.Partial = m.Partial
msg.TimeNano = m.TimeNano
// TODO: create a PR to grok upstream for parsing bytes
// so that we avoid having to convert the message to string
msg.GrokLine, msg.Line, err = groker.ParseLine(grokMatch, logMessage, m.Line)
if err != nil {
c.logger.WithError(err).Error("could not parse line with grok")
}
select {
case c.pipeline.outputCh <- msg:
case <-ctx.Done():
c.logger.WithError(ctx.Err()).Error("closing parse pipeline: Parse")
return ctx.Err()
}
}
return nil
})
return nil
}
// Log sends messages to Elasticsearch Bulk Service
func (c *container) Log(ctx context.Context, workers, actions, size int, flushInterval, timeout time.Duration, stats bool, indexName, tzpe string) error {
c.logger.Debug("starting pipeline: Log")
c.pipeline.group.Go(func() error {
err := c.esClient.NewBulkProcessorService(
ctx,
workers,
actions,
size,
flushInterval,
timeout,
false,
c.logger,
)
if err != nil {
c.logger.WithError(err).Error("could not create bulk processor")
return err
}
defer func() {
if err := c.esClient.Flush(); err != nil {
c.logger.WithError(err).Error("could not flush queue")
}
if err := c.esClient.Close(); err != nil {
c.logger.WithError(err).Error("could not close bulk processor")
}
}()
for doc := range c.pipeline.outputCh {
c.esClient.Add(indexName, tzpe, doc)
select {
case <-ctx.Done():
c.logger.WithError(ctx.Err()).Error("closing log pipeline")
return ctx.Err()
default:
}
}
return nil
})
return nil
}
// BulkWorkerService interface
type BulkWorkerService interface {
Flush(ctx context.Context)
Commit(ctx context.Context)
}
// BulkWorker provides a Bulk Processor
type BulkWorker struct {
elasticsearch.Bulk
logger *log.Entry
ticker *time.Ticker
}
func newWorker(client elasticsearch.Client, logEntry *log.Entry, actions, workerID int, flushInterval, timeout time.Duration) (*BulkWorker, error) {
bulkService, err := elasticsearch.NewBulk(client, timeout, actions)
if err != nil {
return nil, err
}
return &BulkWorker{
Bulk: bulkService,
logger: logEntry.WithField("workerID", workerID),
ticker: time.NewTicker(flushInterval),
}, nil
}
// Flush checks if there are actions to be commited
// before sending them to elasticsearch
func (b BulkWorker) Flush(ctx context.Context) {
if b.NumberOfActions() > 0 {
b.Commit(ctx)
}
}
// Commit sends all messages to elasticsearch
func (b *BulkWorker) Commit(ctx context.Context) {
// b.logger.WithField("size", c.esClient.EstimatedSizeInBytes()).Debug("estimed size in bytes")
// b.logger.WithField("actions", c.esClient.NumberOfActions()).Debug("number of actions...")
// b.logger.WithFields(log.Fields{"docs": b.NumberOfActions(), "workerID": workerID}).Debug("bulking")
bulkResponse, _, rerr, err := b.Do(ctx)
if rerr {
// find out the reasons of the failure
if responses := b.Errors(bulkResponse); responses != nil {
for _, response := range responses {
for range response {
for status, reason := range response {
if status == 429 {
b.logger.WithFields(log.Fields{"reason": reason, "status": status}).Info("resending request")
} else {
b.logger.WithFields(log.Fields{"reason": reason, "status": status}).Info("response error message and status code")
}
}
}
}
return
}
}
if err != nil {
b.logger.WithError(err).Error("could not send all messages to elasticsearch")
}
// b.logger.WithFields(log.Fields{"took": took}).Debug("bulk response time")
}
// CustomBulkProcessor ... this will continue be tested...
// func (c *container) CustomBulkProcessor(ctx context.Context, workers int, indexName, tzpe string, actions, bulkSize int, flushInterval, timeout time.Duration) error {
// c.logger.Debug("starting pipeline: Log")
// for workerID := 0; workerID < workers; workerID++ {
// b, err := newWorker(c.esClient, c.logger, actions, workerID, flushInterval, timeout)
// c.bulkService[workerID] = b
// if err != nil {
// return err
// }
// b.logger.Debug("starting worker")
// c.pipeline.group.Go(func() error {
// defer func() {
// b.logger.Debug("closing worker")
// // commit any left messages in the queue
// b.Flush(ctx)
// b.logger.Debug("stopping ticker")
// b.ticker.Stop()
// delete(c.bulkService, workerID)
// }()
// // healthcheck := func() error {
// // cctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
// // defer cancel()
// // resp, err := c.esClient.Do(cctx, "HEAD", "")
// // if err != nil {
// // c.logger.WithError(err).Debug("error head")
// // time.Sleep(5 * time.Second)
// // return err
// // }
// // if resp >= 200 && resp < 300 {
// // return nil
// // }
// // c.logger.WithField("status", resp).Debug("status code")
// // time.Sleep(3 * time.Second)
// // return errors.New("not beetwen 200 and 300 status code")
// // }
// for {
// // for healthcheck() != nil {
// // c.logger.Debug("healthcheck is checking")
// // }
// select {
// case doc, open := <-c.pipeline.outputCh:
// if !open {
// return nil
// }
// // c.logger.WithField("line", string(doc.Line)).Info("reached last pipeline")
// b.Add(indexName, tzpe, doc)
// if b.CommitRequired(actions, bulkSize) {
// b.Commit(ctx)
// }
// case <-b.ticker.C:
// // b.logger.WithField("ticker", b.ticker).Debug("ticking")
// b.Flush(ctx)
// case <-ctx.Done():
// b.logger.WithError(ctx.Err()).Error("closing log pipeline: Log")
// return ctx.Err()
// // commit has to be in the same goroutine
// // because of reset is called in the Do() func
// // case c.pipeline.commitCh <- struct{}{}:
// }
// }
// })
// }
// return nil
// }
// Stats shows metrics related to the bulk service
// func (d *Driver) Stats(filename string, config Configuration) error {
// TODO: create metrics from stats
// d.pipeline.group.Go(func() error {
// stats := d.esClient.Stats()
// fields := log.Fields{
// "flushed": stats.Flushed,
// "committed": stats.Committed,
// "indexed": stats.Indexed,
// "created": stats.Created,
// "updated": stats.Updated,
// "succeeded": stats.Succeeded,
// "failed": stats.Failed,
// }
// for i, w := range stats.Workers {
// fmt.Printf("Worker %d: Number of requests queued: %d\n", i, w.Queued)
// fmt.Printf(" Last response time : %v\n", w.LastDuration)
// fields[fmt.Sprintf("w%d.queued", i)] = w.Queued
// fields[fmt.Sprintf("w%d.lastduration", i)] = w.LastDuration
// }
// })
// }