Skip to content

Commit

Permalink
Merge pull request #195 from ninech/improve-create-error-logs
Browse files Browse the repository at this point in the history
fix: improve create error logs
  • Loading branch information
ctrox authored Dec 5, 2024
2 parents 834497b + c283f97 commit 1ab8209
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 41 deletions.
36 changes: 1 addition & 35 deletions api/log/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
"github.com/grafana/loki/pkg/logqlmodel"
"github.com/grafana/loki/pkg/util/unmarshal"
"github.com/prometheus/common/config"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/util/retry"
)

type tokenFunc func(ctx context.Context) string
Expand Down Expand Up @@ -76,7 +74,7 @@ func Mode(m string) string {
// StdOut sets up an stdout log output with the specified mode.
func StdOut(mode string) (output.LogOutput, error) {
out, err := output.NewLogOutput(os.Stdout, mode, &output.LogOutputOptions{
NoLabels: false, ColoredOutput: true, Timezone: time.Local,
NoLabels: true, ColoredOutput: true, Timezone: time.Local,
})
if err != nil {
return nil, fmt.Errorf("unable to create log output: %s", err)
Expand Down Expand Up @@ -111,38 +109,6 @@ func (c *Client) QueryRangeResponse(ctx context.Context, q Query) (*loghttp.Quer
return c.Client.QueryRange(q.QueryString, q.Limit, q.Start, q.End, q.Direction, q.Step, q.Interval, q.Quiet)
}

// QueryRangeWithRetry queries logs within a specific time range with a retry
// in case of an error or not finding any logs.
func (c *Client) QueryRangeWithRetry(ctx context.Context, out output.LogOutput, q Query) error {
return retry.OnError(
wait.Backoff{
Steps: 5,
Duration: 200 * time.Millisecond,
Factor: 2.0,
Jitter: 0.1,
Cap: 10 * time.Second,
},
func(err error) bool {
// retry regardless of the error
return true
},
func() error {
resp, err := c.QueryRangeResponse(ctx, q)
if err != nil {
return err
}

switch streams := resp.Data.Result.(type) {
case loghttp.Streams:
if len(streams) == 0 {
return fmt.Errorf("received no log streams")
}
}

return printResult(resp.Data.Result, out)
})
}

// LiveTailQueryConn does a live tailing with a specific query.
func (c *Client) LiveTailQueryConn(ctx context.Context, queryStr string, delayFor time.Duration, limit int, start time.Time, quiet bool) (*websocket.Conn, error) {
c.refreshToken(ctx)
Expand Down
21 changes: 15 additions & 6 deletions create/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,7 @@ func waitForBuildFinish(ctx context.Context, app *apps.Application, logClient *l
case buildStatusError:
fallthrough
case buildStatusUnknown:
p.Send(logbox.Msg{Done: true})
return false, buildError{build: build}
}

Expand Down Expand Up @@ -538,15 +539,19 @@ func printUnverifiedHostsMessage(app *apps.Application) {
}

func printBuildLogs(ctx context.Context, client *api.Client, build *apps.Build) error {
return client.Log.QueryRangeWithRetry(
ctx, client.Log.StdOut,
tailCtx, cancel := context.WithTimeout(ctx, errorTailTimeout)
defer cancel()
return client.Log.TailQuery(
tailCtx, 0, client.Log.StdOut,
errorLogQuery(logs.BuildQuery(build.Name, build.Namespace)),
)
}

func printReleaseLogs(ctx context.Context, client *api.Client, release *apps.Release) error {
return client.Log.QueryRangeWithRetry(
ctx, client.Log.StdOut,
tailCtx, cancel := context.WithTimeout(ctx, errorTailTimeout)
defer cancel()
return client.Log.TailQuery(
tailCtx, 0, client.Log.StdOut,
errorLogQuery(logs.ApplicationQuery(release.Labels[util.ApplicationNameLabel], release.Namespace)),
)
}
Expand All @@ -560,10 +565,14 @@ func printCredentials(basicAuth *util.BasicAuth) {
)
}

// we print the last 20 lines of the log. In most cases this should be
// we print the last 40 lines of the log. In most cases this should be
// enough to give a hint about the problem but we might need to tweak this
// value a bit in the future.
const errorLogLines = 20
const errorLogLines = 40

// when we print error logs, we want to tail the log for a bit as new data might
// still be coming in even after the build/release already has failed.
const errorTailTimeout = 5 * time.Second

func errorLogQuery(queryString string) log.Query {
return log.Query{
Expand Down

0 comments on commit 1ab8209

Please sign in to comment.