-
Notifications
You must be signed in to change notification settings - Fork 3
/
readstern.go
41 lines (36 loc) · 1.03 KB
/
readstern.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
package logfrog
import (
"encoding/json"
"strings"
)
type sternEntry struct {
Message string `json:"message"`
Namespace string `json:"namespace"`
PodName string `json:"podName"`
ContainerName string `json:"containerName"`
}
type ReaderStern struct{}
func (pr *ReaderStern) Valid(line string) bool {
return line != "" && line[0] == '{' && line[len(line)-1] == '}'
}
func (pr *ReaderStern) Read(line string) (label string, logData LogData, err error) {
sd := &sternEntry{}
logData = LogData{}
errUnmarshal := json.Unmarshal([]byte(line), &sd)
if errUnmarshal != nil {
logData["msg"] = line
return "unknown", logData, nil //nolint:nilerr
}
label = sd.Namespace + ":" + sd.PodName + "(" + sd.ContainerName + ")"
errLogData := json.Unmarshal([]byte(sd.Message), &logData)
if errLogData != nil {
logData["msg"] = strings.Trim(sd.Message, "\n")
}
sd.Message = ""
logData["stern"] = map[string]string{
"namespace": sd.Namespace,
"containerName": sd.ContainerName,
"podName": sd.PodName,
}
return
}