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

No way to know how many retries were done, which can be helpful for monitoring #159

Open
diaasami opened this issue Mar 30, 2022 · 1 comment

Comments

@diaasami
Copy link

One solution would be to add a hook that is called on success (with number of retries done), and failure (also with number of retries), this can be helpful for anyone who wants to monitor how many retries are being done and monitoring their infrastructure.

Is there a way to do this currently?

@SimonRichardson
Copy link

You could pass in a request ID into the context and then just record it in the retry logic.

client := retryablehttp.NewClient()
client.CheckRetry = (ctx context.Context, resp *http.Response, err error) (bool, error) {
	metric.WithLabels(RequestID(ctx), resp.Request.URL).Inc()

	return retryablehttp.DefaultRetryPolicy(ctx, resp, err)
}
func RequestID(ctx context.Context) string {
	 id, ok := ctx.Value(contextKeyRequestID).(string)
	 if !ok {
	 	 return "unknown"
	 }
	 return id
}
ctx := context.WithContext(ctx), contextKeyRequestID, "aaa")
req, _ := http.NewRequestWithContext(ctx, "GET", "http:https://localhost:8080", nil)
resp, err := client.Do(req)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants