Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | package logtree |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 21 | "strconv" |
| 22 | "strings" |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 23 | "time" |
Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 24 | |
Mateusz Zalega | cf92f40 | 2022-07-08 15:08:48 +0200 | [diff] [blame] | 25 | tpb "google.golang.org/protobuf/types/known/timestamppb" |
| 26 | |
Serge Bazanski | da11486 | 2023-03-29 17:46:42 +0200 | [diff] [blame] | 27 | cpb "source.monogon.dev/metropolis/proto/common" |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 28 | ) |
| 29 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 30 | // LeveledPayload is a log entry for leveled logs (as per leveled.go). It contains |
| 31 | // the input to these calls (severity and message split into newline-delimited |
| 32 | // messages) and additional metadata that would be usually seen in a text |
Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 33 | // representation of a leveled log entry. |
Serge Bazanski | 1bfa0c2 | 2020-10-14 16:45:07 +0200 | [diff] [blame] | 34 | type LeveledPayload struct { |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 35 | // messages is the list of messages contained in this payload. This list is built |
| 36 | // from splitting up the given message from the user by newline. |
Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 37 | messages []string |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 38 | // timestamp is the time at which this message was emitted. |
| 39 | timestamp time.Time |
| 40 | // severity is the leveled Severity at which this message was emitted. |
| 41 | severity Severity |
| 42 | // file is the filename of the caller that emitted this message. |
| 43 | file string |
| 44 | // line is the line number within the file of the caller that emitted this message. |
| 45 | line int |
| 46 | } |
| 47 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 48 | // String returns a canonical representation of this payload as a single string |
| 49 | // prefixed with metadata. If the original message was logged with newlines, this |
| 50 | // representation will also contain newlines, with each original message part |
| 51 | // prefixed by the metadata. For an alternative call that will instead return a |
| 52 | // canonical prefix and a list of lines in the message, see Strings(). |
Serge Bazanski | 1bfa0c2 | 2020-10-14 16:45:07 +0200 | [diff] [blame] | 53 | func (p *LeveledPayload) String() string { |
Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 54 | prefix, lines := p.Strings() |
| 55 | res := make([]string, len(p.messages)) |
| 56 | for i, line := range lines { |
| 57 | res[i] = fmt.Sprintf("%s%s", prefix, line) |
| 58 | } |
| 59 | return strings.Join(res, "\n") |
| 60 | } |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 61 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 62 | // Strings returns the canonical representation of this payload split into a |
| 63 | // prefix and all lines that were contained in the original message. This is |
| 64 | // meant to be displayed to the user by showing the prefix before each line, |
| 65 | // concatenated together - possibly in a table form with the prefixes all |
| 66 | // unified with a rowspan- like mechanism. |
Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 67 | // |
| 68 | // For example, this function can return: |
Serge Bazanski | da11486 | 2023-03-29 17:46:42 +0200 | [diff] [blame] | 69 | // |
| 70 | // prefix = "I1102 17:20:06.921395 foo.go:42] " |
| 71 | // lines = []string{"current tags:", " - one", " - two"} |
Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 72 | // |
| 73 | // With this data, the result should be presented to users this way in text form: |
| 74 | // I1102 17:20:06.921395 foo.go:42] current tags: |
| 75 | // I1102 17:20:06.921395 foo.go:42] - one |
| 76 | // I1102 17:20:06.921395 foo.go:42] - two |
| 77 | // |
| 78 | // Or, in a table layout: |
| 79 | // .-----------------------------------------------------------. |
| 80 | // | I1102 17:20:06.921395 0 foo.go:42] : current tags: | |
| 81 | // | :------------------| |
| 82 | // | : - one | |
| 83 | // | :------------------| |
| 84 | // | : - two | |
| 85 | // '-----------------------------------------------------------' |
Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 86 | func (p *LeveledPayload) Strings() (prefix string, lines []string) { |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 87 | _, month, day := p.timestamp.Date() |
| 88 | hour, minute, second := p.timestamp.Clock() |
| 89 | nsec := p.timestamp.Nanosecond() / 1000 |
| 90 | |
Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 91 | // Same format as in glog, but without treadid. |
| 92 | // Lmmdd hh:mm:ss.uuuuuu file:line] |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 93 | // TODO(q3k): rewrite this to printf-less code. |
Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 94 | prefix = fmt.Sprintf("%s%02d%02d %02d:%02d:%02d.%06d %s:%d] ", p.severity, month, day, hour, minute, second, nsec, p.file, p.line) |
| 95 | |
| 96 | lines = p.messages |
| 97 | return |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 98 | } |
| 99 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 100 | // Message returns the inner message lines of this entry, ie. what was passed to |
| 101 | // the actual logging method, but split by newlines. |
Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 102 | func (p *LeveledPayload) Messages() []string { return p.messages } |
| 103 | |
| 104 | func (p *LeveledPayload) MessagesJoined() string { return strings.Join(p.messages, "\n") } |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 105 | |
| 106 | // Timestamp returns the time at which this entry was logged. |
Serge Bazanski | 1bfa0c2 | 2020-10-14 16:45:07 +0200 | [diff] [blame] | 107 | func (p *LeveledPayload) Timestamp() time.Time { return p.timestamp } |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 108 | |
Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 109 | // Location returns a string in the form of file_name:line_number that shows the |
| 110 | // origin of the log entry in the program source. |
Serge Bazanski | 1bfa0c2 | 2020-10-14 16:45:07 +0200 | [diff] [blame] | 111 | func (p *LeveledPayload) Location() string { return fmt.Sprintf("%s:%d", p.file, p.line) } |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 112 | |
| 113 | // Severity returns the Severity with which this entry was logged. |
Serge Bazanski | 1bfa0c2 | 2020-10-14 16:45:07 +0200 | [diff] [blame] | 114 | func (p *LeveledPayload) Severity() Severity { return p.severity } |
Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 115 | |
| 116 | // Proto converts a LeveledPayload to protobuf format. |
Serge Bazanski | da11486 | 2023-03-29 17:46:42 +0200 | [diff] [blame] | 117 | func (p *LeveledPayload) Proto() *cpb.LogEntry_Leveled { |
| 118 | return &cpb.LogEntry_Leveled{ |
Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 119 | Lines: p.Messages(), |
Mateusz Zalega | cf92f40 | 2022-07-08 15:08:48 +0200 | [diff] [blame] | 120 | Timestamp: tpb.New(p.Timestamp()), |
Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 121 | Severity: p.Severity().ToProto(), |
| 122 | Location: p.Location(), |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // LeveledPayloadFromProto parses a protobuf message into the internal format. |
Serge Bazanski | da11486 | 2023-03-29 17:46:42 +0200 | [diff] [blame] | 127 | func LeveledPayloadFromProto(p *cpb.LogEntry_Leveled) (*LeveledPayload, error) { |
Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 128 | severity, err := SeverityFromProto(p.Severity) |
| 129 | if err != nil { |
| 130 | return nil, fmt.Errorf("could not convert severity: %w", err) |
| 131 | } |
| 132 | parts := strings.Split(p.Location, ":") |
| 133 | if len(parts) != 2 { |
| 134 | return nil, fmt.Errorf("invalid location, must be two :-delimited parts, is %d parts", len(parts)) |
| 135 | } |
| 136 | file := parts[0] |
| 137 | line, err := strconv.Atoi(parts[1]) |
| 138 | if err != nil { |
| 139 | return nil, fmt.Errorf("invalid location line number: %w", err) |
| 140 | } |
| 141 | return &LeveledPayload{ |
Serge Bazanski | 12971d6 | 2020-11-17 12:12:58 +0100 | [diff] [blame] | 142 | messages: p.Lines, |
Mateusz Zalega | cf92f40 | 2022-07-08 15:08:48 +0200 | [diff] [blame] | 143 | timestamp: p.Timestamp.AsTime(), |
Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 144 | severity: severity, |
| 145 | file: file, |
| 146 | line: line, |
| 147 | }, nil |
| 148 | } |
Serge Bazanski | 020b7c5 | 2021-07-07 14:22:28 +0200 | [diff] [blame] | 149 | |
| 150 | // ExternalLeveledPayload is a LeveledPayload received from an external source, |
| 151 | // eg. from parsing the logging output of third-party programs. It can be |
| 152 | // converted into a LeveledPayload and inserted into a leveled logger, but will |
| 153 | // be sanitized before that, ensuring that potentially buggy |
| 154 | // emitters/converters do not end up polluting the leveled logger data. |
| 155 | // |
| 156 | // This type should be used only when inserting data from external systems, not |
| 157 | // by code that just wishes to log things. In the future, data inserted this |
| 158 | // way might be explicitly marked as tainted so operators can understand that |
| 159 | // parts of this data might not give the same guarantees as the log entries |
| 160 | // emitted by the native LeveledLogger API. |
| 161 | type ExternalLeveledPayload struct { |
| 162 | // Log line. If any newlines are found, they will split the message into |
| 163 | // multiple messages within LeveledPayload. Empty messages are accepted |
| 164 | // verbatim. |
| 165 | Message string |
| 166 | // Timestamp when this payload was emitted according to its source. If not |
| 167 | // given, will default to the time of conversion to LeveledPayload. |
| 168 | Timestamp time.Time |
| 169 | // Log severity. If invalid or unset will default to INFO. |
| 170 | Severity Severity |
| 171 | // File name of originating code. Defaults to "unknown" if not set. |
| 172 | File string |
| 173 | // Line in File. Zero indicates the line is not known. |
| 174 | Line int |
| 175 | } |
| 176 | |
| 177 | // sanitize the given ExternalLeveledPayload by creating a corresponding |
| 178 | // LeveledPayload. The original object is unaltered. |
| 179 | func (e *ExternalLeveledPayload) sanitize() *LeveledPayload { |
| 180 | l := &LeveledPayload{ |
| 181 | messages: strings.Split(e.Message, "\n"), |
| 182 | timestamp: e.Timestamp, |
| 183 | severity: e.Severity, |
| 184 | file: e.File, |
| 185 | line: e.Line, |
| 186 | } |
| 187 | if l.timestamp.IsZero() { |
| 188 | l.timestamp = time.Now() |
| 189 | } |
| 190 | if !l.severity.Valid() { |
| 191 | l.severity = INFO |
| 192 | } |
| 193 | if l.file == "" { |
| 194 | l.file = "unknown" |
| 195 | } |
| 196 | if l.line < 0 { |
| 197 | l.line = 0 |
| 198 | } |
| 199 | return l |
| 200 | } |