Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logcli: Add retries to unsuccessful log queries #3879

Merged
merged 2 commits into from
Jun 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/logcli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func newQueryClient(app *kingpin.Application) client.Client {
app.Flag("org-id", "adds X-Scope-OrgID to API requests for representing tenant ID. Useful for requesting tenant data when bypassing an auth gateway.").Default("").Envar("LOKI_ORG_ID").StringVar(&client.OrgID)
app.Flag("bearer-token", "adds the Authorization header to API requests for authentication purposes. Can also be set using LOKI_BEARER_TOKEN env var.").Default("").Envar("LOKI_BEARER_TOKEN").StringVar(&client.BearerToken)
app.Flag("bearer-token-file", "adds the Authorization header to API requests for authentication purposes. Can also be set using LOKI_BEARER_TOKEN_FILE env var.").Default("").Envar("LOKI_BEARER_TOKEN_FILE").StringVar(&client.BearerTokenFile)
app.Flag("retries", "How many times to retry each query when getting an error response from Loki. Can also be set using LOKI_CLIENT_RETRIES").Default("1").Envar("LOKI_CLIENT_RETRIES").IntVar(&client.Retries)

return client
}
Expand Down
37 changes: 28 additions & 9 deletions pkg/logcli/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ type DefaultClient struct {
Tripperware Tripperware
BearerToken string
BearerTokenFile string
Retries int
}

// Query uses the /api/v1/query endpoint to execute an instant query
Expand Down Expand Up @@ -216,21 +217,39 @@ func (c *DefaultClient) doRequest(path, query string, quiet bool, out interface{
if c.Tripperware != nil {
client.Transport = c.Tripperware(client.Transport)
}
resp, err := client.Do(req)
if err != nil {
return err

var resp *http.Response
retries := c.Retries
success := false

for retries > 0 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a user sets the retries flag value to 0 by mistake then they would unexpectedly skip making any calls. Also, retried calls should be the ones done after seeing a failure in the initial call, so let us initialise the retries above to c.Retries + 1.
What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, but I enhanced your idea a little bit. I renamed the variable to "attempts", which seemed like a better name to me now. I implemented your suggestion, so attempts = c.Retries + 1 and I made the default value for c.Retries = 0. So there still is 1 attempt unless the user sets the retries flag to something other than 0.

retries--

resp, err = client.Do(req)
if err != nil {
log.Println("error sending request", err)
continue
}
if resp.StatusCode/100 != 2 {
buf, _ := ioutil.ReadAll(resp.Body) // nolint
log.Printf("Error response from server: %s (%v) retries remaining: %d", string(buf), err, retries)
if err := resp.Body.Close(); err != nil {
log.Println("error closing body", err)
}
continue
}
success = true
break
}
if !success {
return fmt.Errorf("Run out of retries while querying the server")
}

defer func() {
if err := resp.Body.Close(); err != nil {
log.Println("error closing body", err)
}
}()

if resp.StatusCode/100 != 2 {
buf, _ := ioutil.ReadAll(resp.Body) // nolint
return fmt.Errorf("Error response from server: %s (%v)", string(buf), err)
}

return json.NewDecoder(resp.Body).Decode(out)
}

Expand Down