| 1 | package diagnostics |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "io" |
| 8 | "regexp" |
| 9 | "strings" |
| 10 | "sync" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | |
| 15 | |
| 16 | type Stream struct { |
| 17 | mu sync.Mutex |
| 18 | store *Store |
| 19 | source, level string |
| 20 | sink func(string) |
| 21 | line []byte |
| 22 | dropping, privateKey bool |
| 23 | last string |
| 24 | lastLevel string |
| 25 | repeated uint64 |
| 26 | reported time.Time |
| 27 | batch []Event |
| 28 | batchBytes int |
| 29 | } |
| 30 | |
| 31 | func (s *Store) Stream(source, level string) *Stream { |
| 32 | return &Stream{store: s, source: source, level: level} |
| 33 | } |
| 34 | func NewStream(store *Store, source, level string, sink func(string)) *Stream { |
| 35 | return &Stream{store: store, source: source, level: level, sink: sink} |
| 36 | } |
| 37 | |
| 38 | |
| 39 | |
| 40 | func (s *Stream) flush() { |
| 41 | if s.store != nil { |
| 42 | _ = s.store.appendEvents(s.batch) |
| 43 | } |
| 44 | s.batch = nil |
| 45 | s.batchBytes = 0 |
| 46 | } |
| 47 | func (s *Stream) record(level, message string) { |
| 48 | message = Redact(message) |
| 49 | if s.batchBytes+len(message) > 64<<10 { |
| 50 | s.flush() |
| 51 | } |
| 52 | if s.sink != nil { |
| 53 | s.sink(message + "\n") |
| 54 | } |
| 55 | s.batch = append(s.batch, Event{Level: level, Source: Redact(s.source), Message: message}) |
| 56 | s.batchBytes += len(message) |
| 57 | if s.batchBytes >= 64<<10 || len(s.batch) >= 128 { |
| 58 | s.flush() |
| 59 | } |
| 60 | } |
| 61 | func (s *Stream) summary() { |
| 62 | if s.repeated > 0 { |
| 63 | s.record(s.lastLevel, fmt.Sprintf("[previous output repeated %d additional times]", s.repeated)) |
| 64 | s.repeated = 0 |
| 65 | } |
| 66 | } |
| 67 | func (s *Stream) output(level, line string) { |
| 68 | |
| 69 | |
| 70 | if line == s.last && !strings.HasPrefix(line, "CAGE_") { |
| 71 | s.repeated++ |
| 72 | if time.Since(s.reported) >= time.Second { |
| 73 | s.summary() |
| 74 | s.reported = time.Now() |
| 75 | } |
| 76 | return |
| 77 | } |
| 78 | s.summary() |
| 79 | s.last = line |
| 80 | s.lastLevel = level |
| 81 | s.reported = time.Now() |
| 82 | s.record(level, line) |
| 83 | } |
| 84 | func (s *Stream) emit() { |
| 85 | line := string(s.line) |
| 86 | if bytes.Contains(s.line, []byte("-----BEGIN ")) { |
| 87 | s.privateKey = true |
| 88 | } |
| 89 | if s.privateKey { |
| 90 | if bytes.Contains(s.line, []byte("-----END ")) { |
| 91 | s.privateKey = false |
| 92 | } |
| 93 | } else if !s.dropping && len(s.line) > 0 { |
| 94 | s.output(s.outputLevel(line), line) |
| 95 | } |
| 96 | if s.dropping { |
| 97 | s.output("warn", "[oversized output line omitted]") |
| 98 | } |
| 99 | s.line = nil |
| 100 | s.dropping = false |
| 101 | } |
| 102 | |
| 103 | var logfmtLevel = regexp.MustCompile(`^time=(?:"[^"]*"|\S+)\s+level=(\w+)(?:\s|$)`) |
| 104 | |
| 105 | |
| 106 | |
| 107 | func (s *Stream) outputLevel(line string) string { |
| 108 | switch s.source { |
| 109 | case "vm-hostagent", "proxy", "proxy-validation": |
| 110 | default: |
| 111 | return s.level |
| 112 | } |
| 113 | var entry struct { |
| 114 | Level string `json:"level"` |
| 115 | } |
| 116 | _ = json.Unmarshal([]byte(line), &entry) |
| 117 | if entry.Level == "" { |
| 118 | if match := logfmtLevel.FindStringSubmatch(line); len(match) == 2 { |
| 119 | entry.Level = match[1] |
| 120 | } |
| 121 | } |
| 122 | switch entry.Level { |
| 123 | case "trace", "debug": |
| 124 | return "debug" |
| 125 | case "info": |
| 126 | return "info" |
| 127 | case "warn", "warning": |
| 128 | return "warn" |
| 129 | case "error", "fatal", "panic": |
| 130 | return "error" |
| 131 | default: |
| 132 | return s.level |
| 133 | } |
| 134 | } |
| 135 | func (s *Stream) Write(p []byte) (int, error) { |
| 136 | s.mu.Lock() |
| 137 | defer s.mu.Unlock() |
| 138 | for _, b := range p { |
| 139 | if b == '\n' || b == '\r' { |
| 140 | s.emit() |
| 141 | continue |
| 142 | } |
| 143 | if s.dropping { |
| 144 | continue |
| 145 | } |
| 146 | if len(s.line) >= MaxMessage { |
| 147 | s.line = nil |
| 148 | s.dropping = true |
| 149 | continue |
| 150 | } |
| 151 | s.line = append(s.line, b) |
| 152 | } |
| 153 | s.flush() |
| 154 | return len(p), nil |
| 155 | } |
| 156 | func (s *Stream) Close() error { |
| 157 | s.mu.Lock() |
| 158 | defer s.mu.Unlock() |
| 159 | s.emit() |
| 160 | s.summary() |
| 161 | s.flush() |
| 162 | s.last = "" |
| 163 | return nil |
| 164 | } |
| 165 | func (s *Store) Capture(source, level string, r io.Reader) error { |
| 166 | w := s.Stream(source, level) |
| 167 | _, err := io.Copy(w, r) |
| 168 | _ = w.Close() |
| 169 | return err |
| 170 | } |