/
logstore.go
710 lines (609 loc) · 17.6 KB
/
logstore.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
package logstore
import (
"fmt"
"strings"
"time"
"github.com/golang/protobuf/ptypes"
"github.com/pkg/errors"
"github.com/windmilleng/tilt/pkg/logger"
"github.com/windmilleng/tilt/pkg/model"
"github.com/windmilleng/tilt/pkg/webview"
)
// All parts of Tilt should display logs incrementally,
// so there's no longer a CPU usage reason why logs can't grow unbounded.
//
// We currently cap logs just to prevent heap usage from blowing up unbounded.
const defaultMaxLogLengthInBytes = 20 * 1000 * 1000
const newlineByte = byte('\n')
type Span struct {
ManifestName model.ManifestName
LastSegmentIndex int
FirstSegmentIndex int
}
func (s *Span) Clone() *Span {
clone := *s
return &clone
}
type SpanID = model.LogSpanID
type LogSegment struct {
SpanID SpanID
Time time.Time
Text []byte
Level logger.Level
Fields logger.Fields
// Continues a line from a previous segment.
ContinuesLine bool
// When we store warnings in the LogStore, we break them up into lines and
// store them as a series of line segments. 'Anchor' marks the beginning of a
// series of logs that should be kept together.
//
// Anchor warning1, line1
// warning1, line2
// Anchor warning2, line1
Anchor bool
}
// Whether these two log segments may be printed on the same line
func (l LogSegment) CanContinueLine(other LogSegment) bool {
return l.SpanID == other.SpanID && l.Level == other.Level
}
func (l LogSegment) StartsLine() bool {
return !l.ContinuesLine
}
func (l LogSegment) IsComplete() bool {
segmentLen := len(l.Text)
return segmentLen > 0 && l.Text[segmentLen-1] == newlineByte
}
func (l LogSegment) Len() int {
return len(l.Text)
}
func (l LogSegment) String() string {
return string(l.Text)
}
func segmentsFromBytes(spanID SpanID, time time.Time, level logger.Level, fields logger.Fields, bs []byte) []LogSegment {
segments := []LogSegment{}
lastBreak := 0
for i, b := range bs {
if b == newlineByte {
segments = append(segments, LogSegment{
SpanID: spanID,
Level: level,
Time: time,
Text: bs[lastBreak : i+1],
Fields: fields,
})
lastBreak = i + 1
}
}
if lastBreak < len(bs) {
segments = append(segments, LogSegment{
SpanID: spanID,
Level: level,
Time: time,
Text: bs[lastBreak:],
Fields: fields,
})
}
return segments
}
func linesToString(lines []LogLine) string {
sb := strings.Builder{}
for _, line := range lines {
sb.WriteString(line.Text)
}
return sb.String()
}
type LogEvent interface {
Message() []byte
Time() time.Time
Level() logger.Level
Fields() logger.Fields
// The manifest that this log is associated with.
ManifestName() model.ManifestName
// The SpanID that identifies what Span this is associated with in the LogStore.
SpanID() SpanID
}
// An abstract checkpoint in the log store, so we can
// ask questions like "give me all logs since checkpoint X" and
// "scrub everything since checkpoint Y". In practice, this
// is just an index into the segment slice.
type Checkpoint int
// A central place for storing logs. Not thread-safe.
//
// If you need to read logs in a thread-safe way outside of
// the normal Store state loop, take a look at logstore.Reader.
type LogStore struct {
// A Span is a grouping of logs by their source.
// The term "Span" is taken from opentracing, and has similar associations.
spans map[SpanID]*Span
// We store logs as an append-only sequence of segments.
// Once a segment has been added, it should not be modified.
segments []LogSegment
// The number of bytes stored in this logstore. This is redundant bookkeeping so
// that we don't need to recompute it each time.
len int
// Used for truncating the log. Set as a property so that we can change it
// for testing.
maxLogLengthInBytes int
// If the log is truncated, we need to adjust all checkpoints
checkpointOffset Checkpoint
}
func NewLogStoreForTesting(msg string) *LogStore {
s := NewLogStore()
s.Append(newGlobalTestLogEvent(msg), nil)
return s
}
func NewLogStore() *LogStore {
return &LogStore{
spans: make(map[SpanID]*Span),
segments: []LogSegment{},
len: 0,
maxLogLengthInBytes: defaultMaxLogLengthInBytes,
}
}
func (s *LogStore) Checkpoint() Checkpoint {
return s.checkpointFromIndex(len(s.segments))
}
func (s *LogStore) checkpointFromIndex(index int) Checkpoint {
return Checkpoint(index) + s.checkpointOffset
}
func (s *LogStore) checkpointToIndex(c Checkpoint) int {
index := int(c - s.checkpointOffset)
if index < 0 {
return 0
}
if index > len(s.segments) {
return len(s.segments)
}
return index
}
func (s *LogStore) ScrubSecretsStartingAt(secrets model.SecretSet, checkpoint Checkpoint) {
index := s.checkpointToIndex(checkpoint)
for i := index; i < len(s.segments); i++ {
s.segments[i].Text = secrets.Scrub(s.segments[i].Text)
}
s.len = s.computeLen()
}
func (s *LogStore) Append(le LogEvent, secrets model.SecretSet) {
spanID := le.SpanID()
if spanID == "" && le.ManifestName() != "" {
spanID = SpanID(fmt.Sprintf("unknown:%s", le.ManifestName()))
}
span, ok := s.spans[spanID]
if !ok {
span = &Span{
ManifestName: le.ManifestName(),
LastSegmentIndex: -1,
FirstSegmentIndex: len(s.segments),
}
s.spans[spanID] = span
}
msg := secrets.Scrub(le.Message())
added := segmentsFromBytes(spanID, le.Time(), le.Level(), le.Fields(), msg)
if len(added) == 0 {
return
}
level := le.Level()
if level.AsSevereAs(logger.WarnLvl) {
added[0].Anchor = true
}
added[0].ContinuesLine = s.computeContinuesLine(added[0], span)
s.segments = append(s.segments, added...)
span.LastSegmentIndex = len(s.segments) - 1
s.len += len(msg)
s.ensureMaxLength()
}
func (s *LogStore) Empty() bool {
return len(s.segments) == 0
}
// Get at most N lines from the tail of the log.
func (s *LogStore) Tail(n int) string {
return s.tailHelper(n, s.spans, true)
}
// Get at most N lines from the tail of the span.
func (s *LogStore) TailSpan(n int, spanID SpanID) string {
spans, ok := s.idToSpanMap(spanID)
if !ok {
return ""
}
return s.tailHelper(n, spans, false)
}
// Get at most N lines from the tail of the log.
func (s *LogStore) tailHelper(n int, spans map[SpanID]*Span, showManifestPrefix bool) string {
if n <= 0 {
return ""
}
// Traverse backwards until we have n lines.
remaining := n
startIndex, lastIndex := s.startAndLastIndices(spans)
if startIndex == -1 {
return ""
}
current := lastIndex
for ; current >= startIndex; current-- {
segment := s.segments[current]
if _, ok := spans[segment.SpanID]; !ok {
continue
}
if segment.StartsLine() {
remaining--
if remaining <= 0 {
break
}
}
}
if remaining > 0 {
// If there aren't enough lines, just return the whole store.
return s.toLogString(logOptions{
spans: spans,
showManifestPrefix: showManifestPrefix,
})
}
startedSpans := make(map[SpanID]bool)
newSegments := []LogSegment{}
for i := current; i <= lastIndex; i++ {
segment := s.segments[i]
spanID := segment.SpanID
if _, ok := spans[segment.SpanID]; !ok {
continue
}
if !segment.StartsLine() && !startedSpans[spanID] {
// Skip any segments that start on lines from before the Tail started.
continue
}
newSegments = append(newSegments, segment)
startedSpans[spanID] = true
}
tempStore := &LogStore{spans: s.cloneSpanMap(), segments: newSegments}
tempStore.recomputeDerivedValues()
return tempStore.toLogString(logOptions{
spans: tempStore.spans,
showManifestPrefix: showManifestPrefix,
})
}
func (s *LogStore) cloneSpanMap() map[SpanID]*Span {
newSpans := make(map[SpanID]*Span, len(s.spans))
for spanID, span := range s.spans {
newSpans[spanID] = span.Clone()
}
return newSpans
}
func (s *LogStore) computeContinuesLine(seg LogSegment, span *Span) bool {
if span.LastSegmentIndex == -1 {
return false
} else {
lastSeg := s.segments[span.LastSegmentIndex]
if lastSeg.IsComplete() {
return false
}
if !lastSeg.CanContinueLine(seg) {
return false
}
}
return true
}
func (s *LogStore) recomputeDerivedValues() {
s.len = s.computeLen()
// Reset the last segment index so we can rebuild them from scratch.
for _, span := range s.spans {
span.FirstSegmentIndex = -1
span.LastSegmentIndex = -1
}
// Rebuild information about line continuations.
for i, segment := range s.segments {
spanID := segment.SpanID
span := s.spans[spanID]
if span.FirstSegmentIndex == -1 {
span.FirstSegmentIndex = i
}
s.segments[i].ContinuesLine = s.computeContinuesLine(segment, span)
span.LastSegmentIndex = i
}
for spanID, span := range s.spans {
if span.FirstSegmentIndex == -1 {
delete(s.spans, spanID)
}
}
}
// Returns logs incrementally from the given checkpoint.
//
// In many use cases, logs are printed to an append-only stream (like os.Stdout).
// Once they've been printed, they can't be called back.
// ContinuingString() tries to make reasonable product decisions about printing
// all the logs that have streamed in since the given checkpoint.
//
// Typical usage, looks like:
//
// Print(store.ContinuingString(state.LastCheckpoint))
// state.LastCheckpoint = store.Checkpoint()
func (s *LogStore) ContinuingString(checkpoint Checkpoint) string {
lines := s.ContinuingLines(checkpoint)
sb := strings.Builder{}
for _, line := range lines {
sb.WriteString(line.Text)
}
return sb.String()
}
func (s *LogStore) IsLastSegmentUncompleted() bool {
if len(s.segments) == 0 {
return false
}
lastSegment := s.segments[len(s.segments)-1]
return !lastSegment.IsComplete()
}
func (s *LogStore) ContinuingLines(checkpoint Checkpoint) []LogLine {
isSameSpanContinuation := false
isChangingSpanContinuation := false
checkpointIndex := s.checkpointToIndex(checkpoint)
precedingIndex := checkpointIndex - 1
var precedingSegment = LogSegment{}
if precedingIndex >= 0 && checkpointIndex < len(s.segments) {
// Check the last thing we printed. If it was wasn't complete,
// we have to do some extra work to properly continue the previous print.
precedingSegment = s.segments[precedingIndex]
currentSegment := s.segments[checkpointIndex]
if !precedingSegment.IsComplete() {
// If this is the same span id, remove the prefix from this line.
if precedingSegment.CanContinueLine(currentSegment) {
isSameSpanContinuation = true
} else {
isChangingSpanContinuation = true
}
}
}
tempSegments := s.segments[checkpointIndex:]
tempLogStore := &LogStore{
spans: s.cloneSpanMap(),
segments: tempSegments,
}
tempLogStore.recomputeDerivedValues()
result := tempLogStore.toLogLines(logOptions{
spans: tempLogStore.spans,
showManifestPrefix: true,
skipFirstLineManifestPrefix: isSameSpanContinuation,
})
if isSameSpanContinuation {
return result
}
if isChangingSpanContinuation {
return append([]LogLine{
LogLine{
Text: "\n",
SpanID: precedingSegment.SpanID,
ProgressID: precedingSegment.Fields[logger.FieldNameProgressID],
ProgressMustPrint: precedingSegment.Fields[logger.FieldNameProgressMustPrint] == "1",
Time: precedingSegment.Time,
},
}, result...)
}
return result
}
func (s *LogStore) ToLogList(fromCheckpoint Checkpoint) (*webview.LogList, error) {
spans := make(map[string]*webview.LogSpan, len(s.spans))
for spanID, span := range s.spans {
spans[string(spanID)] = &webview.LogSpan{
ManifestName: span.ManifestName.String(),
}
}
startIndex := s.checkpointToIndex(fromCheckpoint)
if startIndex >= len(s.segments) {
// No logs to send down.
return &webview.LogList{
FromCheckpoint: -1,
ToCheckpoint: -1,
}, nil
}
segments := make([]*webview.LogSegment, 0, len(s.segments)-startIndex)
for i := startIndex; i < len(s.segments); i++ {
segment := s.segments[i]
time, err := ptypes.TimestampProto(segment.Time)
if err != nil {
return nil, errors.Wrap(err, "ToLogList")
}
segments = append(segments, &webview.LogSegment{
SpanId: string(segment.SpanID),
Level: webview.LogLevel(segment.Level.ToProtoID()),
Time: time,
Text: string(segment.Text),
Anchor: segment.Anchor,
Fields: segment.Fields,
})
}
return &webview.LogList{
Spans: spans,
Segments: segments,
FromCheckpoint: int32(s.checkpointFromIndex(startIndex)),
ToCheckpoint: int32(s.Checkpoint()),
}, nil
}
func (s *LogStore) String() string {
return s.toLogString(logOptions{
spans: s.spans,
showManifestPrefix: true,
})
}
func (s *LogStore) spansForManifest(mn model.ManifestName) map[SpanID]*Span {
result := make(map[SpanID]*Span)
for spanID, span := range s.spans {
if span.ManifestName == mn {
result[spanID] = span
}
}
return result
}
func (s *LogStore) idToSpanMap(spanID SpanID) (map[SpanID]*Span, bool) {
spans := make(map[SpanID]*Span, 1)
span, ok := s.spans[spanID]
if !ok {
return nil, false
}
spans[spanID] = span
return spans, true
}
func (s *LogStore) SpanLog(spanID SpanID) string {
spans, ok := s.idToSpanMap(spanID)
if !ok {
return ""
}
return s.toLogString(logOptions{spans: spans})
}
func (s *LogStore) Warnings(spanID SpanID) []string {
spans, ok := s.idToSpanMap(spanID)
if !ok {
return nil
}
startIndex, lastIndex := s.startAndLastIndices(spans)
if startIndex == -1 {
return nil
}
result := []string{}
sb := strings.Builder{}
for i := startIndex; i <= lastIndex; i++ {
segment := s.segments[i]
if segment.Level != logger.WarnLvl || spanID != segment.SpanID {
continue
}
if segment.Anchor && sb.Len() > 0 {
result = append(result, sb.String())
sb = strings.Builder{}
}
sb.WriteString(string(segment.Text))
}
if sb.Len() > 0 {
result = append(result, sb.String())
}
return result
}
func (s *LogStore) ManifestLog(mn model.ManifestName) string {
spans := s.spansForManifest(mn)
return s.toLogString(logOptions{spans: spans})
}
func (s *LogStore) startAndLastIndices(spans map[SpanID]*Span) (startIndex, lastIndex int) {
earliestStartIndex := -1
latestEndIndex := -1
for _, span := range spans {
if earliestStartIndex == -1 || span.FirstSegmentIndex < earliestStartIndex {
earliestStartIndex = span.FirstSegmentIndex
}
if latestEndIndex == -1 || span.LastSegmentIndex > latestEndIndex {
latestEndIndex = span.LastSegmentIndex
}
}
if earliestStartIndex == -1 {
return -1, -1
}
startIndex = earliestStartIndex
lastIndex = latestEndIndex
return startIndex, lastIndex
}
type logOptions struct {
spans map[SpanID]*Span
showManifestPrefix bool
skipFirstLineManifestPrefix bool
}
func (s *LogStore) toLogString(options logOptions) string {
return linesToString(s.toLogLines(options))
}
// Returns a sequence of lines, including trailing newlines.
func (s *LogStore) toLogLines(options logOptions) []LogLine {
result := []LogLine{}
var lineBuilder *logLineBuilder
var consumeLineBuilder = func() {
if lineBuilder == nil {
return
}
result = append(result, lineBuilder.build(options)...)
lineBuilder = nil
}
// We want to print the log line-by-line, but we don't actually store the logs
// line-by-line. We store them as segments.
//
// This means we need to:
// 1) At segment x,
// 2) If x starts a new line, print it, then run ahead to print the rest of the line
// until the entire line is consumed.
// 3) If x does not start a new line, skip it, because we assume it was handled
// in a previous line.
//
// This can have some O(n^2) perf characteristics in the worst case, but
// for normal inputs should be fine.
startIndex, lastIndex := s.startAndLastIndices(options.spans)
if startIndex == -1 {
return nil
}
isFirstLine := true
for i := startIndex; i <= lastIndex; i++ {
segment := s.segments[i]
if !segment.StartsLine() {
continue
}
spanID := segment.SpanID
span := s.spans[spanID]
if _, ok := options.spans[spanID]; !ok {
continue
}
// If the last segment never completed, print a newline now, so that the
// logs from different sources don't blend together.
if lineBuilder != nil {
lineBuilder.needsTrailingNewline = true
consumeLineBuilder()
}
lineBuilder = newLogLineBuilder(span, segment, isFirstLine)
isFirstLine = false
// If this segment is not complete, run ahead and try to complete it.
if lineBuilder.isComplete() {
consumeLineBuilder()
continue
}
for currentIndex := i + 1; currentIndex <= span.LastSegmentIndex; currentIndex++ {
currentSeg := s.segments[currentIndex]
if currentSeg.SpanID != spanID {
continue
}
if !currentSeg.CanContinueLine(lineBuilder.lastSegment()) {
break
}
lineBuilder.addSegment(currentSeg)
if lineBuilder.isComplete() {
consumeLineBuilder()
break
}
}
}
consumeLineBuilder()
return result
}
func (s *LogStore) computeLen() int {
result := 0
for _, segment := range s.segments {
result += segment.Len()
}
return result
}
// After a log hits its limit, we need to truncate it to keep it small
// we do this by cutting a big chunk at a time, so that we have rarer, larger changes, instead of
// a small change every time new data is written to the log
// https://github.com/windmilleng/tilt/issues/1935#issuecomment-531390353
func (s *LogStore) logTruncationTarget() int {
return s.maxLogLengthInBytes / 2
}
func (s *LogStore) ensureMaxLength() {
if s.len <= s.maxLogLengthInBytes {
return
}
// Figure out where we have to truncate.
bytesSpent := 0
truncationIndex := -1
for i := len(s.segments) - 1; i >= 0; i-- {
segment := s.segments[i]
bytesSpent += segment.Len()
if truncationIndex == -1 && bytesSpent > s.logTruncationTarget() {
truncationIndex = i + 1
}
if bytesSpent > s.maxLogLengthInBytes {
s.segments = s.segments[truncationIndex:]
s.checkpointOffset += Checkpoint(truncationIndex)
s.recomputeDerivedValues()
return
}
}
}