-
Notifications
You must be signed in to change notification settings - Fork 3
/
logstash.go
419 lines (357 loc) · 9.8 KB
/
logstash.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
package logstash
import (
"encoding/json"
"errors"
_ "expvar"
"log"
"net"
"regexp"
"strconv"
"time"
"github.com/gliderlabs/logspout/router"
"github.com/rcrowley/go-metrics"
"github.com/rcrowley/go-metrics/exp"
"github.com/anashaka/logspout-logstash/multiline"
"strings"
)
var (
logMeter = metrics.NewMeter()
)
func init() {
router.AdapterFactories.Register(NewLogstashAdapter, "logstash")
exp.Exp(metrics.DefaultRegistry)
metrics.Register("logstash_message_rate", logMeter)
}
type newMultilineBufferFn func() (multiline.MultiLine, error)
// LogstashAdapter is an adapter that streams TCP JSON to Logstash.
type LogstashAdapter struct {
write writer
route *router.Route
cache map[string]*multiline.MultiLine
cacheTTL time.Duration
cachedLines metrics.Gauge
mkBuffer newMultilineBufferFn
cleanupRegExp *regexp.Regexp
javaLogRegExp *regexp.Regexp
staskTraceRegExp *regexp.Regexp
causeRegExp *regexp.Regexp
}
type ControlCode int
const (
Continue ControlCode = iota
Quit
)
func newLogstashAdapter(route *router.Route, write writer) *LogstashAdapter {
patternString, ok := route.Options["pattern"]
if !ok {
patternString = `(^\s)|(^Caused by:)`
}
groupWith, ok := route.Options["group_with"]
if !ok {
groupWith = "previous"
}
negate := false
negateStr, _ := route.Options["negate"]
if negateStr == "true" {
negate = true
}
separator, ok := route.Options["separator"]
if !ok {
separator = "\n"
}
maxLines, err := strconv.Atoi(route.Options["max_lines"])
if err != nil {
maxLines = 0
}
cacheTTL, err := time.ParseDuration(route.Options["cache_ttl"])
if err != nil {
cacheTTL = 10 * time.Second
}
cleanupPattern, ok := route.Options["cleanup_pattern"]
if !ok {
cleanupPattern = `\033\[[0-9;]*?m`
}
javaLogPattern, ok := route.Options["java_pattern"]
if !ok {
javaLogPattern = `([\d:.]+?)\[(\w+?)\s*?\]\[(.*?)\]\[(.*?)\](.*?)\s*?:([\S\w\W]*?)$`
}
stacktracePattern, ok := route.Options["stacktrace_pattern"]
if !ok {
stacktracePattern = `at (?P<fullclass>com\.mm.+?)\.(?P<method>[\w]+)\((?P<classLine>[\w\.]+:[\d]+)\)\s\~?\[(?P<file>.*)\]`
}
causePattern, ok := route.Options["cause_pattern"]
if !ok {
causePattern = `^(.*?):\s(.*)`
}
cleanupRegExp := regexp.MustCompile(cleanupPattern)
javaLogRegExp := regexp.MustCompile(javaLogPattern)
staskTraceRegExp := regexp.MustCompile(stacktracePattern)
causeRegExp := regexp.MustCompile(causePattern)
cachedLines := metrics.NewGauge()
metrics.Register(route.ID + "_cached_lines", cachedLines)
return &LogstashAdapter{
route: route,
write: write,
cache: make(map[string]*multiline.MultiLine),
cacheTTL: cacheTTL,
cachedLines: cachedLines,
mkBuffer: func() (multiline.MultiLine, error) {
return multiline.NewMultiLine(
&multiline.MultilineConfig{
Pattern: regexp.MustCompile(patternString),
GroupWith: groupWith,
Negate: negate,
Separator: &separator,
MaxLines: maxLines,
})
},
cleanupRegExp : cleanupRegExp,
javaLogRegExp : javaLogRegExp,
staskTraceRegExp : staskTraceRegExp,
causeRegExp : causeRegExp,
}
}
// NewLogstashAdapter creates a LogstashAdapter with TCP as the default transport.
func NewLogstashAdapter(route *router.Route) (router.LogAdapter, error) {
transportId, ok := route.Options["transport"]
if !ok {
transportId = "udp"
}
transport, found := router.AdapterTransports.Lookup(route.AdapterTransport(transportId))
if !found {
return nil, errors.New("unable to find adapter: " + route.Adapter)
}
conn, err := transport.Dial(route.Address, route.Options)
if err != nil {
return nil, err
}
var write writer
if transportId == "tcp" {
write = tcpWriter(conn)
} else {
write = defaultWriter(conn)
}
return newLogstashAdapter(route, write), nil
}
func (a *LogstashAdapter) lookupBuffer(msg *router.Message) *multiline.MultiLine {
key := msg.Container.ID + msg.Source
if a.cache[key] == nil {
ml, _ := a.mkBuffer()
a.cache[key] = &ml
}
return a.cache[key]
}
// Stream implements the router.LogAdapter interface.
func (a *LogstashAdapter) Stream(logstream chan *router.Message) {
cacheTicker := time.NewTicker(a.cacheTTL).C
for {
msgs, ccode := a.readMessages(logstream, cacheTicker)
a.sendMessages(msgs)
switch ccode {
case Continue:
continue
case Quit:
return
}
}
}
func (a *LogstashAdapter) readMessages(
logstream chan *router.Message,
cacheTicker <-chan time.Time) ([]*router.Message, ControlCode) {
select {
case t := <-cacheTicker:
return a.expireCache(t), Continue
case msg, ok := <-logstream:
if ok {
return a.bufferMessage(msg), Continue
} else {
return a.flushPendingMessages(), Quit
}
}
}
func (a *LogstashAdapter) bufferMessage(msg *router.Message) []*router.Message {
msgOrNil := a.lookupBuffer(msg).Buffer(msg)
if msgOrNil == nil {
return []*router.Message{}
} else {
return []*router.Message{msgOrNil}
}
}
func (a *LogstashAdapter) expireCache(t time.Time) []*router.Message {
var messages []*router.Message
var linesCounter int64 = 0
for id, buf := range a.cache {
linesCounter += int64(buf.PendingSize())
msg := buf.Expire(t, a.cacheTTL)
if msg != nil {
messages = append(messages, msg)
delete(a.cache, id)
}
}
a.cachedLines.Update(linesCounter)
return messages
}
func (a *LogstashAdapter) flushPendingMessages() []*router.Message {
var messages []*router.Message
for _, buf := range a.cache {
msg := buf.Flush()
if msg != nil {
messages = append(messages, msg)
}
}
return messages
}
func (a *LogstashAdapter) sendMessages(msgs []*router.Message) {
for _, msg := range msgs {
if err := a.sendMessage(msg); err != nil {
log.Fatal("logstash:", err)
}
}
logMeter.Mark(int64(len(msgs)))
}
func (a *LogstashAdapter) sendMessage(msg *router.Message) error {
buff, err := a.serialize(msg)
if err != nil {
return err
}
_, err = a.write(buff)
if err != nil {
return err
}
return nil
}
func (a *LogstashAdapter) serialize(msg *router.Message) ([]byte, error) {
var js []byte
var jsonMsg map[string]interface{}
dockerInfo := DockerInfo{
Name: msg.Container.Name,
ID: msg.Container.ID,
Image: msg.Container.Config.Image,
Hostname: msg.Container.Config.Hostname,
}
componentInfo := ComponentInfo{
Name: msg.Container.Config.Labels["com.docker.compose.service"],
Version: msg.Container.Config.Labels["com.mm.version"],
Env: msg.Container.Config.Labels["com.mm.env"],
}
javaLog, parsedMsg := a.parseJavaMsg(&msg.Data)
err := json.Unmarshal([]byte(msg.Data), &jsonMsg)
if err != nil {
// the message is not in JSON make a new JSON message
msgToSend := LogstashMessage{
Message: *parsedMsg,
Docker: dockerInfo,
Component: componentInfo,
Stream: msg.Source,
JavaLog: javaLog,
}
js, err = json.Marshal(msgToSend)
if err != nil {
return nil, err
}
} else {
// the message is already in JSON just add the docker specific fields as a nested structure
jsonMsg["docker"] = dockerInfo
if (javaLog != nil) {
jsonMsg["javaLog"] = javaLog
}
jsonMsg["component"] = componentInfo
jsonMsg["message"] = *parsedMsg
js, err = json.Marshal(jsonMsg)
if err != nil {
return nil, err
}
}
return js, nil
}
func (a *LogstashAdapter) parseJavaMsg(msg *string) (*JavaLog, *string) {
var cleanMsg = a.cleanupRegExp.ReplaceAllLiteralString(*msg, "")
match := a.javaLogRegExp.FindStringSubmatch(cleanMsg)
if (match == nil) {
return nil, msg
}
exception := a.parseJavaException(&match[6])
javaLog := JavaLog{
Timestamp: match[1],
Level: match[2],
Uuid: match[3],
Thread: match[4],
Logger: match[5],
Exception: exception,
}
result := strings.Trim(match[6], " \t\n\r")
return &javaLog, &result
}
func (a *LogstashAdapter) parseJavaException(javaMsg *string) *JavaException {
if (strings.Contains(*javaMsg, "at ")) {
splitByCause := strings.Split(*javaMsg, "Caused by: ")
for i := len(splitByCause) - 1; i >= 0; i -= 1 {
cause := splitByCause[i]
stackMatch := a.staskTraceRegExp.FindStringSubmatch(cause)
if (stackMatch == nil) {
continue
}
causeMatch := a.causeRegExp.FindStringSubmatch(cause)
if (len(causeMatch) == 3 && len(stackMatch) == 5) {
javaException := JavaException{
CauseException : causeMatch[1],
CauseMessage: causeMatch[2],
FullClass : stackMatch[1],
Method : stackMatch[2],
ClassLine : stackMatch[3],
Jar : stackMatch[4],
}
return &javaException
}
}
}
return nil
}
type DockerInfo struct {
Name string `json:"name"`
ID string `json:"id"`
Image string `json:"image"`
Hostname string `json:"hostname"`
}
type ComponentInfo struct {
Name string `json:"name"`
Env string `json:"env"`
Version string `json:"version"`
}
type JavaLog struct {
Timestamp string `json:"timestamp"`
Level string `json:"level"`
Uuid string `json:"uuid"`
Thread string `json:"thread"`
Logger string `json:"logger"`
Exception *JavaException `json:"exception,omitempty"`
}
type JavaException struct {
CauseException string `json:"causeEx"`
CauseMessage string `json:"causeMsg"`
FullClass string `json:"fullclass"`
Method string `json:"method"`
ClassLine string `json:"classline"`
Jar string `json:"jar"`
}
// LogstashMessage is a simple JSON input to Logstash.
type LogstashMessage struct {
Message string `json:"message"`
Stream string `json:"stream"`
Docker DockerInfo `json:"docker"`
Component ComponentInfo `json:"component"`
JavaLog *JavaLog `json:"javaLog,omitempty"`
}
// writers
type writer func(b []byte) (int, error)
func defaultWriter(conn net.Conn) writer {
return func(b []byte) (int, error) {
return conn.Write(b)
}
}
func tcpWriter(conn net.Conn) writer {
return func(b []byte) (int, error) {
// append a newline
return conn.Write([]byte(string(b) + "\n"))
}
}