-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.go
112 lines (92 loc) · 2.19 KB
/
parser.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
package main
import (
"encoding/json"
"fmt"
"strings"
"time"
)
type Severity int
const (
SeverityInfo = iota
SeverityWarning
SeverityError
)
var ErrNotJson = fmt.Errorf("not-json")
type Message struct {
Severity Severity
Msg string
Timestamp time.Time
Data map[string]string
}
type Parser struct {
mapping map[string]string
skip map[string]struct{}
}
func MakeParser(mapping map[string]string) *Parser {
skip := map[string]struct{}{}
for k, v := range skipFields {
skip[k] = v
}
mm := map[string]string{messageKey: messageKey, timestampKey: timestampKey, severityKey: severityKey}
if m, ok := mapping[messageKey]; ok {
mm[messageKey] = m
}
if m, ok := mapping[timestampKey]; ok {
mm[timestampKey] = m
}
if m, ok := mapping[severityKey]; ok {
mm[severityKey] = m
}
for _, v := range mm {
skip[v] = struct{}{}
}
return &Parser{mapping: mm, skip: skip}
}
const (
timestampKey = "timestamp"
messageKey = "message"
severityKey = "severity"
)
var skipFields = map[string]struct{}{
"stacktrace": {},
"logging.googleapis.com/labels": {},
"logging.googleapis.com/sourceLocation": {},
}
var dateLayout = "2006-01-02T15:04:05.99Z"
func (p *Parser) ParseLine(line []byte) (Message, error) {
var rawMsg map[string]interface{}
err := json.Unmarshal(line, &rawMsg)
if err != nil {
return Message{}, ErrNotJson
}
message, _ := rawMsg[p.mapping[messageKey]]
severity, _ := rawMsg[p.mapping[severityKey]]
timestamp, _ := rawMsg[p.mapping[timestampKey]]
t, err := time.Parse(dateLayout, timestamp.(string))
if err != nil {
return Message{}, fmt.Errorf("failed to parse date, %w", err)
}
data := make(map[string]string)
for k, v := range rawMsg {
if _, ok := p.skip[k]; !ok {
data[k] = fmt.Sprintf("%v", v)
}
}
return Message{
Severity: getSeverity(severity.(string)),
Msg: message.(string),
Timestamp: t,
Data: data,
}, nil
}
func getSeverity(s string) Severity {
switch strings.ToLower(s) {
case "debug", "info":
return SeverityInfo
case "notice", "warning":
return SeverityWarning
case "error", "critical", "alert", "emergency", "fatal":
return SeverityError
}
return SeverityInfo
}