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 | |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame^] | 19 | import ( |
| 20 | "fmt" |
| 21 | "log" |
| 22 | "sync/atomic" |
| 23 | |
| 24 | "git.monogon.dev/source/nexantic.git/core/pkg/logbuffer" |
| 25 | ) |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 26 | |
| 27 | // LogReadOption describes options for the LogTree.Read call. |
| 28 | type LogReadOption struct { |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame^] | 29 | withChildren bool |
| 30 | withStream bool |
| 31 | withBacklog int |
| 32 | onlyLeveled bool |
| 33 | onlyRaw bool |
| 34 | leveledWithMinimumSeverity Severity |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | // WithChildren makes Read return/stream data for both a given DN and all its children. |
| 38 | func WithChildren() LogReadOption { return LogReadOption{withChildren: true} } |
| 39 | |
| 40 | // WithStream makes Read return a stream of data. This works alongside WithBacklog to create a read-and-stream |
| 41 | // construct. |
| 42 | func WithStream() LogReadOption { return LogReadOption{withStream: true} } |
| 43 | |
| 44 | // WithBacklog makes Read return already recorded log entries, up to count elements. |
| 45 | func WithBacklog(count int) LogReadOption { return LogReadOption{withBacklog: count} } |
| 46 | |
| 47 | // BacklogAllAvailable makes WithBacklog return all backlogged log data that logtree possesses. |
| 48 | const BacklogAllAvailable int = -1 |
| 49 | |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame^] | 50 | func OnlyRaw() LogReadOption { return LogReadOption{onlyRaw: true} } |
| 51 | |
| 52 | func OnlyLeveled() LogReadOption { return LogReadOption{onlyLeveled: true} } |
| 53 | |
| 54 | // LeveledWithMinimumSeverity makes Read return only log entries that are at least at a given Severity. If only leveled |
| 55 | // entries are needed, OnlyLeveled must be used. This is a no-op when OnlyRaw is used. |
| 56 | func LeveledWithMinimumSeverity(s Severity) LogReadOption { |
| 57 | return LogReadOption{leveledWithMinimumSeverity: s} |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // LogReader permits reading an already existing backlog of log entries and to stream further ones. |
| 61 | type LogReader struct { |
| 62 | // Backlog are the log entries already logged by LogTree. This will only be set if WithBacklog has been passed to |
| 63 | // Read. |
| 64 | Backlog []*LogEntry |
| 65 | // Stream is a channel of new entries as received live by LogTree. This will only be set if WithStream has been |
| 66 | // passed to Read. In this case, entries from this channel must be read as fast as possible by the consumer in order |
| 67 | // to prevent missing entries. |
| 68 | Stream <-chan *LogEntry |
| 69 | // done is channel used to signal (by closing) that the log consumer is not interested in more Stream data. |
| 70 | done chan<- struct{} |
| 71 | // missed is an atomic integer pointer that tells the subscriber how many messages in Stream they missed. This |
| 72 | // pointer is nil if no streaming has been requested. |
| 73 | missed *uint64 |
| 74 | } |
| 75 | |
| 76 | // Missed returns the amount of entries that were missed from Stream (as the channel was not drained fast enough). |
| 77 | func (l *LogReader) Missed() uint64 { |
| 78 | // No Stream. |
| 79 | if l.missed == nil { |
| 80 | return 0 |
| 81 | } |
| 82 | return atomic.LoadUint64(l.missed) |
| 83 | } |
| 84 | |
| 85 | // Close closes the LogReader's Stream. This must be called once the Reader does not wish to receive streaming messages |
| 86 | // anymore. |
| 87 | func (l *LogReader) Close() { |
| 88 | if l.done != nil { |
| 89 | close(l.done) |
| 90 | } |
| 91 | } |
| 92 | |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame^] | 93 | // LogEntry contains a log entry, combining both leveled and raw logging into a single stream of events. A LogEntry |
| 94 | // will contain exactly one of either LeveledPayload or RawPayload. |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 95 | type LogEntry struct { |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame^] | 96 | // If non-nil, this is a leveled logging entry. |
| 97 | Leveled *LeveledPayload |
| 98 | // If non-nil, this is a raw logging entry line. |
| 99 | Raw *logbuffer.Line |
| 100 | // DN from which this entry was logged. |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 101 | DN DN |
| 102 | } |
| 103 | |
| 104 | // Read and/or stream entries from a LogTree. The returned LogReader is influenced by the LogReadOptions passed, which |
| 105 | // influence whether the Read will return existing entries, a stream, or both. In addition the options also dictate |
| 106 | // whether only entries for that particular DN are returned, or for all sub-DNs as well. |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame^] | 107 | func (l *LogTree) Read(dn DN, opts ...LogReadOption) (*LogReader, error) { |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 108 | l.journal.mu.RLock() |
| 109 | defer l.journal.mu.RUnlock() |
| 110 | |
| 111 | var backlog int |
| 112 | var stream bool |
| 113 | var recursive bool |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame^] | 114 | var leveledSeverity Severity |
| 115 | var onlyRaw, onlyLeveled bool |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 116 | |
| 117 | for _, opt := range opts { |
| 118 | if opt.withBacklog > 0 || opt.withBacklog == BacklogAllAvailable { |
| 119 | backlog = opt.withBacklog |
| 120 | } |
| 121 | if opt.withStream { |
| 122 | stream = true |
| 123 | } |
| 124 | if opt.withChildren { |
| 125 | recursive = true |
| 126 | } |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame^] | 127 | if opt.leveledWithMinimumSeverity != "" { |
| 128 | leveledSeverity = opt.leveledWithMinimumSeverity |
| 129 | } |
| 130 | if opt.onlyLeveled { |
| 131 | onlyLeveled = true |
| 132 | } |
| 133 | if opt.onlyRaw { |
| 134 | onlyRaw = true |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame^] | 138 | if onlyLeveled && onlyRaw { |
| 139 | return nil, fmt.Errorf("cannot return logs that are simultaneously OnlyRaw and OnlyLeveled") |
| 140 | } |
| 141 | |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 142 | var filters []filter |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame^] | 143 | if onlyLeveled { |
| 144 | filters = append(filters, filterOnlyLeveled) |
| 145 | } |
| 146 | if onlyRaw { |
| 147 | filters = append(filters, filterOnlyRaw) |
| 148 | } |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 149 | if recursive { |
| 150 | filters = append(filters, filterSubtree(dn)) |
| 151 | } else { |
| 152 | filters = append(filters, filterExact(dn)) |
| 153 | } |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame^] | 154 | if leveledSeverity != "" { |
| 155 | filters = append(filters, filterSeverity(leveledSeverity)) |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | var entries []*entry |
| 159 | if backlog > 0 || backlog == BacklogAllAvailable { |
| 160 | // TODO(q3k): pass over the backlog count to scanEntries/getEntries, instead of discarding them here. |
| 161 | if recursive { |
| 162 | entries = l.journal.scanEntries(filters...) |
| 163 | } else { |
| 164 | entries = l.journal.getEntries(dn, filters...) |
| 165 | } |
| 166 | if backlog != BacklogAllAvailable && len(entries) > backlog { |
| 167 | entries = entries[:backlog] |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | var sub *subscriber |
| 172 | if stream { |
| 173 | sub = &subscriber{ |
| 174 | // TODO(q3k): make buffer size configurable |
| 175 | dataC: make(chan *LogEntry, 128), |
| 176 | doneC: make(chan struct{}), |
| 177 | filters: filters, |
| 178 | } |
| 179 | l.journal.subscribe(sub) |
| 180 | } |
| 181 | |
| 182 | lr := &LogReader{} |
| 183 | lr.Backlog = make([]*LogEntry, len(entries)) |
| 184 | for i, entry := range entries { |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame^] | 185 | log.Printf("backlog %d %+v %+v", i, entry.raw, entry.leveled) |
| 186 | lr.Backlog[i] = entry.external() |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 187 | } |
| 188 | if stream { |
| 189 | lr.Stream = sub.dataC |
| 190 | lr.done = sub.doneC |
| 191 | lr.missed = &sub.missed |
| 192 | } |
Serge Bazanski | f68153c | 2020-10-26 13:54:37 +0100 | [diff] [blame^] | 193 | return lr, nil |
Serge Bazanski | 5faa2fc | 2020-09-07 14:09:30 +0200 | [diff] [blame] | 194 | } |