From 7d045bf2cab52c316dd1f943cea3b2440b3ba21f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=82=96=E8=B4=9D=E8=B4=9D?= Date: Tue, 28 Apr 2020 06:29:37 +0800 Subject: [PATCH] fix: vmagent not follow 301/302 redirect bug (#445) Co-authored-by: xiaobeibei --- lib/promscrape/client.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lib/promscrape/client.go b/lib/promscrape/client.go index dcb265ee5..5a47861ad 100644 --- a/lib/promscrape/client.go +++ b/lib/promscrape/client.go @@ -124,6 +124,15 @@ var ( func doRequestWithPossibleRetry(hc *fasthttp.HostClient, req *fasthttp.Request, resp *fasthttp.Response) error { // There is no need in calling DoTimeout, since the timeout must be already set in hc.ReadTimeout. err := hc.Do(req, resp) + if err == nil && (resp.Header.StatusCode() == 301 || resp.Header.StatusCode() == 302) { + l := string(resp.Header.Peek("Location")) + if l != "" { + req.URI().Update(l) + // only redirect once + err = hc.Do(req, resp) + } + } + if err == nil { return nil }