diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 9fa90e0e90..0b00f76e79 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -23,6 +23,7 @@ sort: 15 * BUGFIX: [vmui](https://docs.victoriametrics.com/#vmui): add missing `query` caption to the input field for the query. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1900). * BUGFIX: [vmui](https://docs.victoriametrics.com/#vmui): fix navigation over query history with `Ctrl+up/down` and fix zoom relatively to the cursor position. See [this pull request](https://github.com/VictoriaMetrics/VictoriaMetrics/pull/1936). * BUGFIX: deduplicate samples more thoroughly if [deduplication](https://docs.victoriametrics.com/#deduplication) is enabled. Previously some duplicate samples may be left on disk for time series with high churn rate. This may result in bigger storage space requirements. +* BUGFIX: [vmagent](https://docs.victoriametrics.com/vmagent.html): follow up to 5 redirects when `follow_redirects: true` is set for a particular scrape config. Previously only a single redirect was performed in this case. It is expected these redirects are performed to the original hostname. See [this issue](https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1945). ## [v1.70.0](https://github.com/VictoriaMetrics/VictoriaMetrics/releases/tag/v1.70.0) diff --git a/lib/promscrape/client.go b/lib/promscrape/client.go index 2447a92e5d..0826d20f18 100644 --- a/lib/promscrape/client.go +++ b/lib/promscrape/client.go @@ -236,19 +236,27 @@ func (c *client) ReadData(dst []byte) ([]byte, error) { } err := doRequestWithPossibleRetry(c.hc, req, resp, deadline) statusCode := resp.StatusCode() - if err == nil && (statusCode == fasthttp.StatusMovedPermanently || statusCode == fasthttp.StatusFound) { + redirectsCount := 0 + for err == nil && (statusCode == fasthttp.StatusMovedPermanently || statusCode == fasthttp.StatusFound) { + if redirectsCount > 5 { + err = fmt.Errorf("too many redirects") + break + } if c.denyRedirects { err = fmt.Errorf("cannot follow redirects if `follow_redirects: false` is set") - } else { - // Allow a single redirect. - // It is expected that the redirect is made on the same host. - // Otherwise it won't work. - if location := resp.Header.Peek("Location"); len(location) > 0 { - req.URI().UpdateBytes(location) - err = c.hc.DoDeadline(req, resp, deadline) - statusCode = resp.StatusCode() - } + break } + // It is expected that the redirect is made on the same host. + // Otherwise it won't work. + location := resp.Header.Peek("Location") + if len(location) == 0 { + err = fmt.Errorf("missing Location header") + break + } + req.URI().UpdateBytes(location) + err = doRequestWithPossibleRetry(c.hc, req, resp, deadline) + statusCode = resp.StatusCode() + redirectsCount++ } if swapResponseBodies { dst = resp.SwapBody(dst)