| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 3 | |
| 4 | package logtree |
| 5 | |
| 6 | import ( |
| Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 7 | "errors" |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 8 | "strings" |
| 9 | "sync" |
| Serge Bazanski | 3c5d063 | 2024-09-12 10:49:12 +0000 | [diff] [blame] | 10 | |
| 11 | "source.monogon.dev/go/logging" |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 12 | ) |
| 13 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 14 | // DN is the Distinguished Name, a dot-delimited path used to address loggers |
| 15 | // within a LogTree. For example, "foo.bar" designates the 'bar' logger node |
| 16 | // under the 'foo' logger node under the root node of the logger. An empty |
| 17 | // string is the root node of the tree. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 18 | type DN string |
| 19 | |
| Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 20 | var ( |
| 21 | ErrInvalidDN = errors.New("invalid DN") |
| 22 | ) |
| 23 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 24 | // Path return the parts of a DN, ie. all the elements of the dot-delimited DN |
| 25 | // path. For the root node, an empty list will be returned. An error will be |
| 26 | // returned if the DN is invalid (contains empty parts, eg. `foo..bar`, `.foo` |
| 27 | // or `foo.`. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 28 | func (d DN) Path() ([]string, error) { |
| 29 | if d == "" { |
| 30 | return nil, nil |
| 31 | } |
| 32 | parts := strings.Split(string(d), ".") |
| 33 | for _, p := range parts { |
| 34 | if p == "" { |
| Serge Bazanski | b027218 | 2020-11-02 18:39:44 +0100 | [diff] [blame] | 35 | return nil, ErrInvalidDN |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | return parts, nil |
| 39 | } |
| 40 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 41 | // journal is the main log recording structure of logtree. It manages linked lists |
| 42 | // containing the actual log entries, and implements scans across them. It does not |
| 43 | // understand the hierarchical nature of logtree, and instead sees all entries as |
| 44 | // part of a global linked list and a local linked list for a given DN. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 45 | // |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 46 | // The global linked list is represented by the head/tail pointers in journal and |
| 47 | // nextGlobal/prevGlobal pointers in entries. The local linked lists are |
| 48 | // represented by heads[DN]/tails[DN] pointers in journal and nextLocal/prevLocal |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 49 | // pointers in entries: |
| 50 | // |
| Serge Bazanski | 8fab014 | 2023-03-29 16:48:16 +0200 | [diff] [blame] | 51 | // .------------. .------------. .------------. |
| 52 | // | dn: A.B | | dn: Z | | dn: A.B | |
| 53 | // | time: 1 | | time: 2 | | time: 3 | |
| 54 | // |------------| |------------| |------------| |
| 55 | // | nextGlobal :------->| nextGlobal :------->| nextGlobal :--> nil |
| 56 | // nil <-: prevGlobal |<-------: prevGlobal |<-------| prevGlobal | |
| 57 | // |------------| |------------| n |------------| |
| 58 | // | nextLocal :---. n | nextLocal :->i .-->| nextLocal :--> nil |
| 59 | // nil <-: prevLocal |<--: i<-: prevLocal | l :---| prevLocal | |
| 60 | // '------------' | l '------------' | '------------' |
| 61 | // ^ '----------------------' ^ |
| 62 | // | ^ | |
| 63 | // | | | |
| 64 | // ( head ) ( tails[Z] ) ( tail ) |
| 65 | // ( heads[A.B] ) ( heads[Z] ) ( tails[A.B] ) |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 66 | type journal struct { |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 67 | // mu locks the rest of the structure. It must be taken during any operation on the |
| 68 | // journal. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 69 | mu sync.RWMutex |
| 70 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 71 | // tail is the side of the global linked list that contains the newest log entry, |
| 72 | // ie. the one that has been pushed the most recently. It can be nil when no log |
| 73 | // entry has yet been pushed. The global linked list contains all log entries |
| 74 | // pushed to the journal. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 75 | tail *entry |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 76 | // head is the side of the global linked list that contains the oldest log entry. |
| 77 | // It can be nil when no log entry has yet been pushed. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 78 | head *entry |
| 79 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 80 | // tails are the tail sides of a local linked list for a given DN, ie. the sides |
| 81 | // that contain the newest entry. They are nil if there are no log entries for that |
| 82 | // DN. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 83 | tails map[DN]*entry |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 84 | // heads are the head sides of a local linked list for a given DN, ie. the sides |
| 85 | // that contain the oldest entry. They are nil if there are no log entries for that |
| 86 | // DN. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 87 | heads map[DN]*entry |
| 88 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 89 | // quota is a map from DN to quota structure, representing the quota policy of a |
| 90 | // particular DN-designated logger. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 91 | quota map[DN]*quota |
| 92 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 93 | // subscribers are observer to logs. New log entries get emitted to channels |
| 94 | // present in the subscriber structure, after filtering them through subscriber- |
| 95 | // provided filters (eg. to limit events to subtrees that interest that particular |
| 96 | // subscriber). |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 97 | subscribers []*subscriber |
| 98 | } |
| 99 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 100 | // newJournal creates a new empty journal. All journals are independent from |
| 101 | // eachother, and as such, all LogTrees are also independent. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 102 | func newJournal() *journal { |
| 103 | return &journal{ |
| 104 | tails: make(map[DN]*entry), |
| 105 | heads: make(map[DN]*entry), |
| 106 | |
| 107 | quota: make(map[DN]*quota), |
| 108 | } |
| 109 | } |
| 110 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 111 | // filter is a predicate that returns true if a log subscriber or reader is |
| 112 | // interested in a given log entry. |
| Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 113 | type filter func(*entry) bool |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 114 | |
| Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 115 | // filterAll returns a filter that accepts all log entries. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 116 | func filterAll() filter { |
| Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 117 | return func(*entry) bool { return true } |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 120 | // filterExact returns a filter that accepts only log entries at a given exact |
| 121 | // DN. This filter should not be used in conjunction with journal.scanEntries |
| 122 | // - instead, journal.getEntries should be used, as it is much faster. |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 123 | func filterExact(dn DN) filter { |
| Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 124 | return func(e *entry) bool { |
| 125 | return e.origin == dn |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 129 | // filterSubtree returns a filter that accepts all log entries at a given DN and |
| 130 | // sub-DNs. For example, filterSubtree at "foo.bar" would allow entries at |
| 131 | // "foo.bar", "foo.bar.baz", but not "foo" or "foo.barr". |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 132 | func filterSubtree(root DN) filter { |
| 133 | if root == "" { |
| 134 | return filterAll() |
| 135 | } |
| 136 | |
| 137 | rootParts := strings.Split(string(root), ".") |
| Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 138 | return func(e *entry) bool { |
| 139 | parts := strings.Split(string(e.origin), ".") |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 140 | if len(parts) < len(rootParts) { |
| 141 | return false |
| 142 | } |
| 143 | |
| 144 | for i, p := range rootParts { |
| 145 | if parts[i] != p { |
| 146 | return false |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | return true |
| 151 | } |
| 152 | } |
| 153 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 154 | // filterSeverity returns a filter that accepts log entries at a given severity |
| 155 | // level or above. See the Severity type for more information about severity |
| 156 | // levels. |
| Serge Bazanski | 3c5d063 | 2024-09-12 10:49:12 +0000 | [diff] [blame] | 157 | func filterSeverity(atLeast logging.Severity) filter { |
| Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 158 | return func(e *entry) bool { |
| 159 | return e.leveled != nil && e.leveled.severity.AtLeast(atLeast) |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 160 | } |
| 161 | } |
| 162 | |
| Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 163 | func filterOnlyRaw(e *entry) bool { |
| 164 | return e.raw != nil |
| 165 | } |
| 166 | |
| 167 | func filterOnlyLeveled(e *entry) bool { |
| 168 | return e.leveled != nil |
| 169 | } |
| 170 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 171 | // scanEntries does a linear scan through the global entry list and returns all |
| 172 | // entries that match the given filters. If retrieving entries for an exact event, |
| 173 | // getEntries should be used instead, as it will leverage DN-local linked lists to |
| 174 | // retrieve them faster. journal.mu must be taken at R or RW level when calling |
| 175 | // this function. |
| Serge Bazanski | 8fab014 | 2023-03-29 16:48:16 +0200 | [diff] [blame] | 176 | func (j *journal) scanEntries(count int, filters ...filter) (res []*entry) { |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 177 | cur := j.tail |
| 178 | for { |
| 179 | if cur == nil { |
| Serge Bazanski | 8fab014 | 2023-03-29 16:48:16 +0200 | [diff] [blame] | 180 | break |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | passed := true |
| 184 | for _, filter := range filters { |
| Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 185 | if !filter(cur) { |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 186 | passed = false |
| 187 | break |
| 188 | } |
| 189 | } |
| 190 | if passed { |
| 191 | res = append(res, cur) |
| 192 | } |
| Serge Bazanski | 8fab014 | 2023-03-29 16:48:16 +0200 | [diff] [blame] | 193 | if count != BacklogAllAvailable && len(res) >= count { |
| 194 | break |
| 195 | } |
| 196 | cur = cur.prevGlobal |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 197 | } |
| Serge Bazanski | 8fab014 | 2023-03-29 16:48:16 +0200 | [diff] [blame] | 198 | |
| Serge Bazanski | 8fab014 | 2023-03-29 16:48:16 +0200 | [diff] [blame] | 199 | return |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 200 | } |
| 201 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 202 | // getEntries returns all entries at a given DN. This is faster than a |
| 203 | // scanEntries(filterExact), as it uses the special local linked list pointers to |
| 204 | // traverse the journal. Additional filters can be passed to further limit the |
| 205 | // entries returned, but a scan through this DN's local linked list will be |
| 206 | // performed regardless. journal.mu must be taken at R or RW level when calling |
| 207 | // this function. |
| Serge Bazanski | 8fab014 | 2023-03-29 16:48:16 +0200 | [diff] [blame] | 208 | func (j *journal) getEntries(count int, exact DN, filters ...filter) (res []*entry) { |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 209 | cur := j.tails[exact] |
| 210 | for { |
| 211 | if cur == nil { |
| Serge Bazanski | 8fab014 | 2023-03-29 16:48:16 +0200 | [diff] [blame] | 212 | break |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | passed := true |
| 216 | for _, filter := range filters { |
| Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame] | 217 | if !filter(cur) { |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 218 | passed = false |
| 219 | break |
| 220 | } |
| 221 | } |
| 222 | if passed { |
| 223 | res = append(res, cur) |
| 224 | } |
| Serge Bazanski | 8fab014 | 2023-03-29 16:48:16 +0200 | [diff] [blame] | 225 | if count != BacklogAllAvailable && len(res) >= count { |
| 226 | break |
| 227 | } |
| 228 | cur = cur.prevLocal |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| Serge Bazanski | 8fab014 | 2023-03-29 16:48:16 +0200 | [diff] [blame] | 231 | return |
| Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 232 | } |
| Serge Bazanski | 367ee27 | 2023-03-16 17:50:39 +0100 | [diff] [blame] | 233 | |
| 234 | // Shorten returns a shortened version of this DN for constrained logging |
| 235 | // environments like tty0 logging. |
| 236 | // |
| 237 | // If ShortenDictionary is given, it will be used to replace DN parts with |
| 238 | // shorter equivalents. For example, with the dictionary: |
| 239 | // |
| 240 | // { "foobar": "foo", "manager": "mgr" } |
| 241 | // |
| 242 | // The DN some.foobar.logger will be turned into some.foo.logger before further |
| 243 | // being processed by the shortening mechanism. |
| 244 | // |
| 245 | // The shortening rules applied are Metropolis-specific. |
| 246 | func (d DN) Shorten(dict ShortenDictionary, maxLen int) string { |
| 247 | path, _ := d.Path() |
| 248 | // Apply DN part shortening rules. |
| 249 | if dict != nil { |
| 250 | for i, p := range path { |
| 251 | if sh, ok := dict[p]; ok { |
| 252 | path[i] = sh |
| 253 | } |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | // This generally shouldn't happen. |
| 258 | if len(path) == 0 { |
| 259 | return "?" |
| 260 | } |
| 261 | |
| 262 | // Strip 'root.' prefix. |
| 263 | if len(path) > 1 && path[0] == "root" { |
| 264 | path = path[1:] |
| 265 | } |
| 266 | |
| 267 | // Replace role.xxx.yyy.zzz with xxx.zzz - stripping everything between the role |
| 268 | // name and the last element of the path. |
| 269 | if path[0] == "role" && len(path) > 1 { |
| 270 | if len(path) == 2 { |
| 271 | path = path[1:] |
| 272 | } else { |
| 273 | path = []string{ |
| 274 | path[1], |
| 275 | path[len(path)-1], |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | // Join back to be ' '-delimited, and ellipsize if too long. |
| 281 | s := strings.Join(path, " ") |
| 282 | if overflow := len(s) - maxLen; overflow > 0 { |
| 283 | s = "..." + s[overflow+3:] |
| 284 | } |
| 285 | return s |
| 286 | } |
| 287 | |
| 288 | type ShortenDictionary map[string]string |
| 289 | |
| 290 | var MetropolisShortenDict = ShortenDictionary{ |
| 291 | "controlplane": "cplane", |
| 292 | "map-cluster-membership": "map-membership", |
| 293 | "cluster-membership": "cluster", |
| 294 | "controller-manager": "controllers", |
| 295 | "networking": "net", |
| 296 | "network": "net", |
| 297 | "interfaces": "ifaces", |
| 298 | "kubernetes": "k8s", |
| 299 | } |