| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 3 | |
| 4 | package logtree |
| 5 | |
| 6 | import ( |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "regexp" |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 10 | "strconv" |
| 11 | "strings" |
| 12 | "time" |
| Serge Bazanski | 96043bc | 2021-10-05 12:10:13 +0200 | [diff] [blame] | 13 | |
| Serge Bazanski | 3c5d063 | 2024-09-12 10:49:12 +0000 | [diff] [blame] | 14 | "source.monogon.dev/go/logging" |
| Tim Windelschmidt | 9f21f53 | 2024-05-07 15:14:20 +0200 | [diff] [blame] | 15 | "source.monogon.dev/osbase/logbuffer" |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 16 | ) |
| 17 | |
| 18 | // KLogParser returns an io.WriteCloser to which raw logging from a klog emitter |
| 19 | // can be piped. It will attempt to parse all lines from this log as |
| 20 | // glog/klog-style entries, and pass them over to a LeveledLogger as if they were |
| 21 | // emitted locally. |
| 22 | // |
| 23 | // This allows for piping in external processes that emit klog logging into a |
| 24 | // logtree, leading to niceties like transparently exposing the log severity or |
| 25 | // source file/line. |
| 26 | // |
| 27 | // One caveat, however, is that V-leveled logs will not be translated |
| 28 | // appropriately - anything that the klog-emitter pumps out as Info will be |
| 29 | // directly ingested as Info logging. There is no way to work around this. |
| 30 | // |
| 31 | // Another important limitation is that any written line is interpreted as having |
| 32 | // happened recently (ie. within one hour of the time of execution of this |
| 33 | // function). This is important as klog/glog-formatted loglines don't have a year |
| 34 | // attached, so we have to infer it based on the current timestamp (note: parsed |
| 35 | // lines do not necessarily have their year aleays equal to the current year, as |
| 36 | // the code handles the edge case of parsing a line from the end of a previous |
| 37 | // year at the beginning of the next). |
| Serge Bazanski | 3c5d063 | 2024-09-12 10:49:12 +0000 | [diff] [blame] | 38 | func KLogParser(logger logging.Leveled) io.WriteCloser { |
| Serge Bazanski | d9775a6 | 2022-02-15 13:28:55 +0100 | [diff] [blame] | 39 | p, ok := logger.(*leveledPublisher) |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 40 | if !ok { |
| 41 | // Fail fast, as this is a programming error. |
| Serge Bazanski | d9775a6 | 2022-02-15 13:28:55 +0100 | [diff] [blame] | 42 | panic("Expected *leveledPublisher in LeveledLogger from supervisor") |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | k := &klogParser{ |
| Serge Bazanski | d9775a6 | 2022-02-15 13:28:55 +0100 | [diff] [blame] | 46 | publisher: p, |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 47 | } |
| 48 | // klog seems to have no line length limit. Let's assume some sane sort of default. |
| 49 | k.buffer = logbuffer.NewLineBuffer(1024, k.consumeLine) |
| 50 | return k |
| 51 | } |
| 52 | |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 53 | type klogParser struct { |
| Serge Bazanski | d9775a6 | 2022-02-15 13:28:55 +0100 | [diff] [blame] | 54 | publisher *leveledPublisher |
| 55 | buffer *logbuffer.LineBuffer |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | func (k *klogParser) Write(p []byte) (n int, err error) { |
| 59 | return k.buffer.Write(p) |
| 60 | } |
| 61 | |
| 62 | // Close must be called exactly once after the parser is done being used. It will |
| 63 | // pipe any leftover data in its write buffer as one last line to parse. |
| 64 | func (k *klogParser) Close() error { |
| 65 | return k.buffer.Close() |
| 66 | } |
| 67 | |
| 68 | // consumeLine is called by the internal LineBuffer any time a new line is fully |
| 69 | // written. |
| 70 | func (k *klogParser) consumeLine(l *logbuffer.Line) { |
| 71 | p := parse(time.Now(), l.Data) |
| 72 | if p == nil { |
| 73 | // We could instead emit that line as a raw log - however, this would lead to |
| 74 | // interleaving raw logging and leveled logging. |
| Serge Bazanski | d9775a6 | 2022-02-15 13:28:55 +0100 | [diff] [blame] | 75 | k.publisher.Errorf("Invalid klog line: %s", l.Data) |
| Serge Bazanski | 5456a3c | 2023-03-16 00:29:17 +0100 | [diff] [blame] | 76 | return |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 77 | } |
| 78 | // TODO(q3k): should this be exposed as an API on LeveledLogger? How much should |
| 79 | // we permit library users to 'fake' logs? This would also permit us to get rid |
| 80 | // of the type assertion in KLogParser(). |
| 81 | e := &entry{ |
| Serge Bazanski | d9775a6 | 2022-02-15 13:28:55 +0100 | [diff] [blame] | 82 | origin: k.publisher.node.dn, |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 83 | leveled: p, |
| 84 | } |
| Serge Bazanski | d9775a6 | 2022-02-15 13:28:55 +0100 | [diff] [blame] | 85 | k.publisher.node.tree.journal.append(e) |
| 86 | k.publisher.node.tree.journal.notify(e) |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | var ( |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 90 | // reKLog matches and parses klog/glog-formatted log lines. Format: I0312 |
| 91 | // 14:20:04.240540 204 shared_informer.go:247] Caches are synced for attach |
| 92 | // detach |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 93 | reKLog = regexp.MustCompile(`^([IEWF])(\d{4})\s+(\d{2}:\d{2}:\d{2}(\.\d+)?)\s+(\d+)\s+([^:]+):(\d+)]\s+(.+)$`) |
| 94 | ) |
| 95 | |
| 96 | // parse attempts to parse a klog-formatted line. Returns nil if the line |
| 97 | // couldn't have been parsed successfully. |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 98 | func parse(now time.Time, s string) *LeveledPayload { |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 99 | parts := reKLog.FindStringSubmatch(s) |
| 100 | if parts == nil { |
| 101 | return nil |
| 102 | } |
| 103 | |
| 104 | severityS := parts[1] |
| 105 | date := parts[2] |
| 106 | timestamp := parts[3] |
| 107 | pid := parts[5] |
| 108 | file := parts[6] |
| 109 | lineS := parts[7] |
| 110 | message := parts[8] |
| 111 | |
| Serge Bazanski | 3c5d063 | 2024-09-12 10:49:12 +0000 | [diff] [blame] | 112 | var severity logging.Severity |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 113 | switch severityS { |
| 114 | case "I": |
| Serge Bazanski | 3c5d063 | 2024-09-12 10:49:12 +0000 | [diff] [blame] | 115 | severity = logging.INFO |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 116 | case "W": |
| Serge Bazanski | 3c5d063 | 2024-09-12 10:49:12 +0000 | [diff] [blame] | 117 | severity = logging.WARNING |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 118 | case "E": |
| Serge Bazanski | 3c5d063 | 2024-09-12 10:49:12 +0000 | [diff] [blame] | 119 | severity = logging.ERROR |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 120 | case "F": |
| Serge Bazanski | 3c5d063 | 2024-09-12 10:49:12 +0000 | [diff] [blame] | 121 | severity = logging.FATAL |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 122 | default: |
| 123 | return nil |
| 124 | } |
| 125 | |
| 126 | // Possible race due to klog's/glog's format not containing a year. |
| 127 | // On 2020/12/31 at 23:59:59.99999 a klog logger emits this line: |
| 128 | // |
| 129 | // I1231 23:59:59.99999 1 example.go:10] It's almost 2021! Hooray. |
| 130 | // |
| 131 | // Then, if this library parses that line at 2021/01/01 00:00:00.00001, the |
| 132 | // time will be interpreted as: |
| 133 | // |
| 134 | // 2021/12/31 23:59:59 |
| 135 | // |
| 136 | // So around one year in the future. We attempt to fix this case further down in |
| 137 | // this function. |
| 138 | year := now.Year() |
| 139 | ts, err := parseKLogTime(year, date, timestamp) |
| 140 | if err != nil { |
| 141 | return nil |
| 142 | } |
| 143 | |
| 144 | // Attempt to fix the aforementioned year-in-the-future issue. |
| 145 | if ts.After(now) && ts.Sub(now) > time.Hour { |
| 146 | // Parsed timestamp is in the future. How close is it to One-Year-From-Now? |
| 147 | oyfn := now.Add(time.Hour * 24 * 365) |
| 148 | dOyfn := ts.Sub(oyfn) |
| 149 | // Let's make sure Duration-To-One-Year-From-Now is always positive. This |
| 150 | // simplifies the rest of the checks and papers over some possible edge cases. |
| 151 | if dOyfn < 0 { |
| 152 | dOyfn = -dOyfn |
| 153 | } |
| 154 | |
| 155 | // Okay, is that very close? Then the issue above happened and we should |
| 156 | // attempt to reparse it with last year. We can't just manipulate the date we |
| 157 | // already have, as it's difficult to 'subtract one year'. |
| 158 | if dOyfn < (time.Hour * 24 * 2) { |
| 159 | ts, err = parseKLogTime(year-1, date, timestamp) |
| 160 | if err != nil { |
| 161 | return nil |
| 162 | } |
| 163 | } else { |
| 164 | // Otherwise, we received some seriously time traveling log entry. Abort. |
| 165 | return nil |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | line, err := strconv.Atoi(lineS) |
| 170 | if err != nil { |
| 171 | return nil |
| 172 | } |
| 173 | |
| 174 | // The PID is discarded. |
| 175 | _ = pid |
| 176 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 177 | // Finally we have extracted all the data from the line. Inject into the log |
| 178 | // publisher. |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 179 | return &LeveledPayload{ |
| 180 | timestamp: ts, |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 181 | severity: severity, |
| 182 | messages: []string{message}, |
| 183 | file: file, |
| 184 | line: line, |
| Serge Bazanski | 0ab4eda | 2021-03-12 17:43:57 +0100 | [diff] [blame] | 185 | } |
| 186 | } |
| 187 | |
| 188 | // parseKLogTime parses a klog date and time (eg. "0314", "12:13:14.12345") into |
| 189 | // a time.Time happening at a given year. |
| 190 | func parseKLogTime(year int, d, t string) (time.Time, error) { |
| 191 | var layout string |
| 192 | if strings.Contains(t, ".") { |
| 193 | layout = "2006 0102 15:04:05.000000" |
| 194 | } else { |
| 195 | layout = "2006 0102 15:04:05" |
| 196 | } |
| 197 | // Make up a string that contains the current year. This permits us to parse |
| 198 | // fully into an actual timestamp. |
| 199 | // TODO(q3k): add a timezone? This currently behaves as UTC, which is probably |
| 200 | // what we want, but we should formalize this. |
| 201 | return time.Parse(layout, fmt.Sprintf("%d %s %s", year, d, t)) |
| 202 | } |