Skip to content

Commit

Permalink
fix: retry querying logs on creation failure
Browse files Browse the repository at this point in the history
in some cases a build can fail before any logs have arrived at our
logging service. In this case, we want to retry showing the build logs
as they usually appear within a few hundred milliseconds.
  • Loading branch information
ctrox committed Dec 19, 2023
1 parent b0385b1 commit c80f7c5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
36 changes: 36 additions & 0 deletions api/log/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package log
import (
"context"
"fmt"
"log"
"os"
"sort"
"time"
Expand All @@ -16,6 +17,8 @@ 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 Client struct {
Expand Down Expand Up @@ -89,6 +92,39 @@ func (c *Client) QueryRange(ctx context.Context, out output.LogOutput, q Query)
return printResult(resp.Data.Result, out)
}

// 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.Client.QueryRange(q.QueryString, q.Limit, q.Start, q.End, q.Direction, q.Step, q.Interval, q.Quiet)
if err != nil {
log.Println(err)
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)
})
}

func printResult(value loghttp.ResultValue, out output.LogOutput) error {
switch value.Type() {
case logqlmodel.ValueTypeStreams:
Expand Down
4 changes: 2 additions & 2 deletions create/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,14 +420,14 @@ func printUnverifiedHostsMessage(app *apps.Application) {
}

func printBuildLogs(ctx context.Context, client *api.Client, build *apps.Build) error {
return client.Log.QueryRange(
return client.Log.QueryRangeWithRetry(
ctx, 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.QueryRange(
return client.Log.QueryRangeWithRetry(
ctx, client.Log.StdOut,
errorLogQuery(logs.ApplicationQuery(release.Labels[util.ApplicationNameLabel], release.Namespace)),
)
Expand Down

0 comments on commit c80f7c5

Please sign in to comment.