forked from tsaikd/gogstash
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stream.go
107 lines (90 loc) · 2.07 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
package inputdockerlog
import (
"bytes"
"io"
"regexp"
"time"
"github.com/Sirupsen/logrus"
"github.com/tsaikd/gogstash/config"
"github.com/tsaikd/gogstash/config/logevent"
)
func NewContainerLogStream(inchan config.InChan, id string, eventExtra map[string]interface{}, since *time.Time, logger *logrus.Logger) ContainerLogStream {
return ContainerLogStream{
ID: id,
eventChan: inchan,
eventExtra: eventExtra,
logger: logger,
buffer: bytes.NewBuffer(nil),
since: since,
}
}
type ContainerLogStream struct {
io.Writer
ID string
eventChan config.InChan
eventExtra map[string]interface{}
logger *logrus.Logger
buffer *bytes.Buffer
since *time.Time
}
func (t *ContainerLogStream) Write(p []byte) (n int, err error) {
n, err = t.buffer.Write(p)
if err != nil {
t.logger.Fatal(err)
return
}
idx := bytes.IndexByte(t.buffer.Bytes(), '\n')
for idx > 0 {
data := t.buffer.Next(idx)
err = t.sendEvent(data)
t.buffer.Next(1)
if err != nil {
t.logger.Fatal(err)
return
}
idx = bytes.IndexByte(t.buffer.Bytes(), '\n')
}
return
}
var (
reTime = regexp.MustCompile(`[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}\.[0-9]+Z[0-9+-]*`)
)
func (t *ContainerLogStream) sendEvent(data []byte) (err error) {
var (
eventTime time.Time
)
event := logevent.LogEvent{
Timestamp: time.Now(),
Message: string(data),
Extra: t.eventExtra,
}
event.Extra["containerid"] = t.ID
loc := reTime.FindIndex(data)
if len(loc) > 0 && loc[0] < 10 {
timestr := string(data[loc[0]:loc[1]])
eventTime, err = time.Parse(time.RFC3339Nano, timestr)
if err == nil {
if eventTime.Before(*t.since) {
return
}
event.Timestamp = eventTime
data = data[loc[1]+1:]
} else {
t.logger.Println(err)
}
} else {
t.logger.Printf("invalid event format %q\n", string(data))
}
event.Message = string(bytes.TrimSpace(data))
if t.since.Before(event.Timestamp) {
*t.since = event.Timestamp
} else {
return
}
if err != nil {
event.AddTag("inputdocker_failed")
err = nil
}
t.eventChan <- event
return
}