From 24c08642cd85b84b6c689f569c7a0b110eaef534 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Dohn=C3=A1lek?= Date: Thu, 7 Apr 2022 13:46:32 +0200 Subject: [PATCH 1/3] Make Query requests idempotent MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Address #1020. Signed-off-by: Tomáš Dohnálek --- api/prometheus/v1/api.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/api/prometheus/v1/api.go b/api/prometheus/v1/api.go index 857512fb8..b68b69c25 100644 --- a/api/prometheus/v1/api.go +++ b/api/prometheus/v1/api.go @@ -1138,6 +1138,9 @@ func (h *apiClientImpl) DoGetFallback(ctx context.Context, u *url.URL, args url. return nil, nil, nil, err } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + // Underlying `net.http` library automatically retries` idempotent requests when connectivity issues are hit. + // POST requests are not considered idempotent by default, so we need to explicitly mark them as such. + req.Header.Set("Idempotency-Key", "TODO") resp, body, warnings, err := h.Do(ctx, req) if resp != nil && (resp.StatusCode == http.StatusMethodNotAllowed || resp.StatusCode == http.StatusNotImplemented) { From 7016fd3335c4ef48b2bb7f0f474eb3bed2f74f23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Dohn=C3=A1lek?= Date: Fri, 8 Apr 2022 11:46:26 +0200 Subject: [PATCH 2/3] Use empty header MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tomáš Dohnálek --- api/prometheus/v1/api.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api/prometheus/v1/api.go b/api/prometheus/v1/api.go index b68b69c25..e9242c845 100644 --- a/api/prometheus/v1/api.go +++ b/api/prometheus/v1/api.go @@ -1140,7 +1140,7 @@ func (h *apiClientImpl) DoGetFallback(ctx context.Context, u *url.URL, args url. req.Header.Set("Content-Type", "application/x-www-form-urlencoded") // Underlying `net.http` library automatically retries` idempotent requests when connectivity issues are hit. // POST requests are not considered idempotent by default, so we need to explicitly mark them as such. - req.Header.Set("Idempotency-Key", "TODO") + req.Header["Idempotency-Key"] = nil resp, body, warnings, err := h.Do(ctx, req) if resp != nil && (resp.StatusCode == http.StatusMethodNotAllowed || resp.StatusCode == http.StatusNotImplemented) { From 5588f73bec87736f7eef270dbe1ed800093e079a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Dohn=C3=A1lek?= Date: Tue, 12 Apr 2022 22:55:05 +0200 Subject: [PATCH 3/3] Document issue with original documentation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tomáš Dohnálek --- api/prometheus/v1/api.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/api/prometheus/v1/api.go b/api/prometheus/v1/api.go index e9242c845..080620a24 100644 --- a/api/prometheus/v1/api.go +++ b/api/prometheus/v1/api.go @@ -1138,8 +1138,13 @@ func (h *apiClientImpl) DoGetFallback(ctx context.Context, u *url.URL, args url. return nil, nil, nil, err } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - // Underlying `net.http` library automatically retries` idempotent requests when connectivity issues are hit. - // POST requests are not considered idempotent by default, so we need to explicitly mark them as such. + // Following comment originates from https://pkg.go.dev/net/http#Transport + // Transport only retries a request upon encountering a network error if the request is + // idempotent and either has no body or has its Request.GetBody defined. HTTP requests + // are considered idempotent if they have HTTP methods GET, HEAD, OPTIONS, or TRACE; or + // if their Header map contains an "Idempotency-Key" or "X-Idempotency-Key" entry. If the + // idempotency key value is a zero-length slice, the request is treated as idempotent but + // the header is not sent on the wire. req.Header["Idempotency-Key"] = nil resp, body, warnings, err := h.Do(ctx, req)