Skip to content

Commit

Permalink
hud: truncate the logs pre-render (#1449)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicks authored Apr 8, 2019
1 parent 8b190c1 commit f4b5648
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 66 deletions.
45 changes: 0 additions & 45 deletions internal/hud/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,51 +178,6 @@ func isCrashing(res view.Resource) bool {
res.IsDC() && res.DockerComposeTarget().Status() == string(dockercompose.StatusCrash)
}

func bestLogs(res view.Resource) string {
// A build is in progress, triggered by an explicit edit.
if res.CurrentBuild.StartTime.After(res.LastBuild().FinishTime) &&
!res.CurrentBuild.Reason.IsCrashOnly() {
return res.CurrentBuild.Log.String()
}

// A build is in progress, triggered by a pod crash.
if res.CurrentBuild.StartTime.After(res.LastBuild().FinishTime) &&
res.CurrentBuild.Reason.IsCrashOnly() {
return res.CrashLog + "\n\n" + res.CurrentBuild.Log.String()
}

// The last build was an error.
if res.LastBuild().Error != nil {
return res.LastBuild().Log.String()
}

if k8sInfo, ok := res.ResourceInfo.(view.K8SResourceInfo); ok {
// Two cases:
// 1) The last build finished before this pod started
// 2) This log is from an in-place container update.
// in either case, prepend them to pod logs.
if (res.LastBuild().StartTime.Equal(k8sInfo.PodUpdateStartTime) ||
res.LastBuild().StartTime.Before(k8sInfo.PodCreationTime)) &&
!res.LastBuild().Log.Empty() {
return res.LastBuild().Log.String() + "\n" + res.ResourceInfo.RuntimeLog().String()
}

// The last build finished, but the pod hasn't started yet.
// NOTE(maia): we truncate build state time down to the second b/c
// pod creation time is only precise to the second.
// TODO(maia): can compare expected/actual DeployID for more accuracy.
if res.LastBuild().StartTime.Truncate(time.Second).After(k8sInfo.PodCreationTime) {
return res.LastBuild().Log.String()
}
}

if res.IsTiltfile {
return res.LastBuild().Log.String()
}

return res.LastBuild().Log.String() + "\n" + res.ResourceInfo.RuntimeLog().String()
}

func (r *Renderer) renderModal(fg rty.Component, bg rty.Component, fixed bool) rty.Component {
return rty.NewModalLayout(bg, fg, .9, fixed)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/hud/renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ ERROR: ImageBuild: executor failed running [/bin/sh -c go install github.com/win
PodRestarts: 0,
},
Endpoints: []string{"1.2.3.4:8080"},
CrashLog: "1\n2\n3\n4\nabe vigoda is now dead\n5\n6\n7\n8\n",
CrashLog: model.NewLog("1\n2\n3\n4\nabe vigoda is now dead\n5\n6\n7\n8\n"),
},
},
}
Expand Down Expand Up @@ -372,7 +372,7 @@ func TestCrashingPodInlineCrashLog(t *testing.T) {
{
Name: "vigoda",
Endpoints: []string{"1.2.3.4:8080"},
CrashLog: "Definitely borken",
CrashLog: model.NewLog("Definitely borken"),
BuildHistory: []model.BuildRecord{{
Log: model.NewLog("Building (1/2)\nBuilding (2/2)\n"),
StartTime: ts,
Expand Down Expand Up @@ -433,7 +433,7 @@ func TestNonCrashingPodNoInlineCrashLog(t *testing.T) {
{
Name: "vigoda",
Endpoints: []string{"1.2.3.4:8080"},
CrashLog: "Definitely borken",
CrashLog: model.NewLog("Definitely borken"),
BuildHistory: []model.BuildRecord{{
Log: model.NewLog("Building (1/2)\nBuilding (2/2)\n"),
StartTime: ts,
Expand Down
10 changes: 5 additions & 5 deletions internal/hud/resourceview.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ func statusColor(res view.Resource, triggerMode model.TriggerMode) tcell.Color {
if res.IsTiltfile {
if !res.CurrentBuild.Empty() {
return cPending
} else if res.CrashLog == "" {
} else if res.CrashLog.Empty() {
return cGood
} else if res.CrashLog != "" {
} else {
return cBad
}
}
Expand Down Expand Up @@ -403,9 +403,9 @@ func (v *ResourceView) resourceExpandedRuntimeError() (rty.Component, bool) {
pane := rty.NewConcatLayout(rty.DirVert)
ok := false
if isCrashing(v.res) {
runtimeLog := v.res.CrashLog
runtimeLog := v.res.CrashLog.Tail(abbreviatedLogLineCount).String()
if runtimeLog == "" {
runtimeLog = v.res.ResourceInfo.RuntimeLog().String()
runtimeLog = v.res.ResourceInfo.RuntimeLog().Tail(abbreviatedLogLineCount).String()
}
abbrevLog := abbreviateLog(runtimeLog)
for _, logLine := range abbrevLog {
Expand All @@ -421,7 +421,7 @@ func (v *ResourceView) resourceExpandedBuildError() (rty.Component, bool) {
ok := false

if v.res.LastBuild().Error != nil {
abbrevLog := abbreviateLog(v.res.LastBuild().Log.String())
abbrevLog := abbreviateLog(v.res.LastBuild().Log.Tail(abbreviatedLogLineCount).String())
for _, logLine := range abbrevLog {
pane.Add(rty.TextString(logLine))
ok = true
Expand Down
3 changes: 0 additions & 3 deletions internal/hud/server/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ func StateToWebView(s store.EngineState) webview.View {
PendingBuildSince: pendingBuildSince,
PendingBuildReason: ms.NextBuildReason(),
CurrentBuild: currentBuild,
CrashLog: ms.CrashLog.String(),
Endpoints: endpoints,
ResourceInfo: resourceInfoView(mt),
ShowBuildStatus: len(mt.Manifest.ImageTargets) > 0 || mt.Manifest.IsDC(),
Expand Down Expand Up @@ -130,10 +129,8 @@ func StateToWebView(s store.EngineState) webview.View {
if !s.LastTiltfileBuild.Empty() {
err := s.LastTiltfileBuild.Error
if err == nil && s.IsEmpty() {
tr.CrashLog = store.EmptyTiltfileMsg
ret.TiltfileErrorMessage = store.EmptyTiltfileMsg
} else if err != nil {
tr.CrashLog = err.Error()
ret.TiltfileErrorMessage = err.Error()
}
}
Expand Down
2 changes: 1 addition & 1 deletion internal/hud/tabview.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func NewTabView(v view.View, vState view.ViewState) *TabView {
func (v *TabView) Build() rty.Component {
l := rty.NewConcatLayout(rty.DirHor)
log := rty.NewTextScrollLayout("log")
log.Add(rty.TextString(v.view.Log.String()))
log.Add(rty.TextString(v.view.Log.Tail(mainLogLineCount).String()))
l.Add(log)
return l
}
5 changes: 5 additions & 0 deletions internal/hud/text.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
"github.com/windmilleng/tilt/internal/rty"
)

// The most lines we can reasonably put in the log pane. If the log pane sticks
// around in the long term, we might want to compute this dynamically based on
// the window size.
const mainLogLineCount = 50

func deployTimeText(t time.Time) rty.Component {
sb := rty.NewStringBuilder()
if t.IsZero() {
Expand Down
4 changes: 2 additions & 2 deletions internal/hud/view/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ type Resource struct {

// If a pod had to be killed because it was crashing, we keep the old log around
// for a little while.
CrashLog string
CrashLog model.Log

IsTiltfile bool
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func (r Resource) DefaultCollapse() bool {

autoExpand = autoExpand ||
r.LastBuild().Error != nil ||
r.CrashLog != "" ||
!r.CrashLog.Empty() ||
r.LastBuild().Reason.Has(model.BuildReasonFlagCrash) ||
r.CurrentBuild.Reason.Has(model.BuildReasonFlagCrash) ||
r.PendingBuildReason.Has(model.BuildReasonFlagCrash)
Expand Down
4 changes: 0 additions & 4 deletions internal/hud/webview/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ type Resource struct {
ResourceInfo ResourceInfoView
RuntimeStatus RuntimeStatus

// If a pod had to be killed because it was crashing, we keep the old log around
// for a little while.
CrashLog string

IsTiltfile bool
ShowBuildStatus bool // if true, we show status & time in 'Build Status'; else, "N/A"
CombinedLog model.Log
Expand Down
8 changes: 8 additions & 0 deletions internal/model/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ func NewLog(s string) Log {
return Log{lines: linesFromString(s)}
}

// Get at most N lines from the tail of the log.
func (l Log) Tail(n int) Log {
if len(l.lines) <= n {
return l
}
return Log{lines: l.lines[len(l.lines)-n:]}
}

func (l Log) MarshalJSON() ([]byte, error) {
return json.Marshal(l.String())
}
Expand Down
11 changes: 11 additions & 0 deletions internal/model/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,14 @@ func TestLog_Timestamps(t *testing.T) {
expected := "hello\n2019/03/06 12:34:56 bar\n2019/03/06 12:34:56 baz\n"
assert.Equal(t, expected, l.String())
}

func TestLogTail(t *testing.T) {
l := NewLog("1\n2\n3\n4\n5\n")
assert.Equal(t, "", l.Tail(0).String())
assert.Equal(t, "5\n", l.Tail(1).String())
assert.Equal(t, "4\n5\n", l.Tail(2).String())
assert.Equal(t, "3\n4\n5\n", l.Tail(3).String())
assert.Equal(t, "2\n3\n4\n5\n", l.Tail(4).String())
assert.Equal(t, "1\n2\n3\n4\n5\n", l.Tail(5).String())
assert.Equal(t, "1\n2\n3\n4\n5\n", l.Tail(6).String())
}
6 changes: 3 additions & 3 deletions internal/store/engine_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ func StateToView(s EngineState) view.View {
PendingBuildSince: pendingBuildSince,
PendingBuildReason: ms.NextBuildReason(),
CurrentBuild: currentBuild,
CrashLog: ms.CrashLog.String(),
CrashLog: ms.CrashLog,
Endpoints: endpoints,
ResourceInfo: resourceInfoView(mt),
}
Expand Down Expand Up @@ -694,10 +694,10 @@ func StateToView(s EngineState) view.View {
if !s.LastTiltfileBuild.Empty() {
err := s.LastTiltfileBuild.Error
if err == nil && s.IsEmpty() {
tr.CrashLog = EmptyTiltfileMsg
tr.CrashLog = model.NewLog(EmptyTiltfileMsg)
ret.TiltfileErrorMessage = EmptyTiltfileMsg
} else if err != nil {
tr.CrashLog = err.Error()
tr.CrashLog = model.NewLog(err.Error())
ret.TiltfileErrorMessage = err.Error()
}
}
Expand Down

0 comments on commit f4b5648

Please sign in to comment.