-
Notifications
You must be signed in to change notification settings - Fork 88
/
logparser.go
158 lines (130 loc) · 3.84 KB
/
logparser.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
package snapshot
import (
"fmt"
"io"
"regexp"
"time"
"github.com/go-logfmt/logfmt"
"github.com/pkg/errors"
"github.com/replicatedhq/kots/pkg/kotsadmsnapshot/types"
"github.com/replicatedhq/kots/pkg/logger"
)
var (
stdoutPrefix = regexp.MustCompile(`^stdout: `)
stderrPrefix = regexp.MustCompile(`^stderr: `)
)
func parseLogs(reader io.Reader) ([]types.SnapshotError, []types.SnapshotError, []*types.SnapshotHook, error) {
errs := []types.SnapshotError{}
warnings := []types.SnapshotError{}
execs := []*types.SnapshotHook{}
openExecs := map[string]*types.SnapshotHook{}
d := logfmt.NewDecoder(reader)
for d.ScanRecord() {
line := map[string]string{}
for d.ScanKeyval() {
line[string(d.Key())] = string(d.Value())
}
if isExecBegin(line) {
key := execKey(line)
if open, ok := openExecs[key]; ok {
// close out the existing exec with the same key
execs = append(execs, open)
delete(openExecs, key)
}
open := types.SnapshotHook{
Name: line["hookName"],
Namespace: line["namespace"],
Phase: line["hookPhase"],
PodName: line["name"],
ContainerName: line["hookContainer"],
Command: line["hookCommand"],
}
if startedAt, err := time.Parse(time.RFC3339, line["time"]); err == nil {
open.StartedAt = &startedAt
} else {
logger.Error(errors.Wrap(err, "failed to parse time"))
}
openExecs[key] = &open
continue
}
if isExecStdout(line) {
key := execKey(line)
open, ok := openExecs[key]
if !ok {
// Dropping stdout from backup logs
continue
}
open.Stdout = stdoutPrefix.ReplaceAllString(line["msg"], "")
if finishedAt, err := time.Parse(time.RFC3339, line["time"]); err == nil {
open.FinishedAt = &finishedAt
} else {
logger.Error(errors.Wrap(err, "failed to parse time"))
}
continue
}
if isExecStderr(line) {
key := execKey(line)
open, ok := openExecs[key]
if !ok {
// Dropping stderr from backup logs
continue
}
open.Stderr = stderrPrefix.ReplaceAllString(line["msg"], "")
if finishedAt, err := time.Parse(time.RFC3339, line["time"]); err == nil {
open.FinishedAt = &finishedAt
} else {
logger.Error(errors.Wrap(err, "failed to parse time"))
}
continue
}
if isError(line) && isExec(line) {
key := execKey(line)
open, ok := openExecs[key]
if !ok {
// Dropping exec error from backup logs
continue
}
open.Errors = append(open.Errors, types.SnapshotError{Title: line["msg"], Message: line["error"]})
}
if isWarning(line) && isExec(line) {
key := execKey(line)
open, ok := openExecs[key]
if !ok {
// Dropping exec error from backup logs
continue
}
open.Warnings = append(open.Warnings, types.SnapshotError{Title: line["msg"], Message: line["error"]})
}
if isError(line) {
errs = append(errs, types.SnapshotError{Title: line["msg"], Message: line["error"]})
}
if isWarning(line) {
warnings = append(warnings, types.SnapshotError{Title: line["msg"], Message: line["error"]})
}
}
for _, exec := range openExecs {
execs = append(execs, exec)
}
return errs, warnings, execs, errors.Wrap(d.Err(), "failed to scan logs")
}
func execKey(line map[string]string) string {
return fmt.Sprintf("%s/%s/%s/%s/%s", line["namespace"], line["name"], line["hookPhase"], line["hookSource"], line["hookType"])
}
func isExecBegin(line map[string]string) bool {
return line["msg"] == "running exec hook"
}
func isExecStdout(line map[string]string) bool {
return stdoutPrefix.MatchString(line["msg"])
}
func isExecStderr(line map[string]string) bool {
return stderrPrefix.MatchString(line["msg"])
}
func isError(line map[string]string) bool {
return line["level"] == "error"
}
func isWarning(line map[string]string) bool {
return line["level"] == "warning"
}
func isExec(line map[string]string) bool {
return line["hookName"] != ""
}