Skip to content

Commit

Permalink
Implement POST with get fallback for Query/QueryRange
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Jackson <jacksontj.89@gmail.com>

Fixes prometheus#428
  • Loading branch information
jacksontj committed Apr 24, 2019
1 parent 65d3a96 commit d06690e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 19 deletions.
22 changes: 22 additions & 0 deletions api/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ type Client interface {
Do(context.Context, *http.Request) (*http.Response, []byte, error)
}

// DoGetFallback will attempt to do the request as-is, and on a 405 it will fallback to a GET request
func DoGetFallback(c Client, ctx context.Context, u *url.URL, args url.Values) (*http.Response, []byte, error) {
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(args.Encode()))
if err != nil {
return nil, nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

resp, body, err := c.Do(ctx, req)
if resp != nil && resp.StatusCode == http.StatusMethodNotAllowed {
u.RawQuery = args.Encode()
req, err = http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, nil, err
}

} else {
return resp, body, err
}
return c.Do(ctx, req)
}

// NewClient returns a new Client.
//
// It is safe to use the returned Client from multiple goroutines.
Expand Down
16 changes: 2 additions & 14 deletions api/prometheus/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -542,12 +542,7 @@ func (h *httpAPI) Query(ctx context.Context, query string, ts time.Time) (model.

u.RawQuery = q.Encode()

req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}

_, body, err := h.client.Do(ctx, req)
_, body, err := api.DoGetFallback(h.client, ctx, u, q)
if err != nil {
return nil, err
}
Expand All @@ -573,14 +568,7 @@ func (h *httpAPI) QueryRange(ctx context.Context, query string, r Range) (model.
q.Set("end", end)
q.Set("step", step)

u.RawQuery = q.Encode()

req, err := http.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, err
}

_, body, err := h.client.Do(ctx, req)
_, body, err := api.DoGetFallback(h.client, ctx, u, q)
if err != nil {
return nil, err
}
Expand Down
10 changes: 5 additions & 5 deletions api/prometheus/v1/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func TestAPIs(t *testing.T) {
},
},

reqMethod: "GET",
reqMethod: "POST",
reqPath: "/api/v1/query",
reqParam: url.Values{
"query": []string{"2"},
Expand All @@ -196,7 +196,7 @@ func TestAPIs(t *testing.T) {
do: doQuery("2", testTime),
inErr: fmt.Errorf("some error"),

reqMethod: "GET",
reqMethod: "POST",
reqPath: "/api/v1/query",
reqParam: url.Values{
"query": []string{"2"},
Expand All @@ -214,7 +214,7 @@ func TestAPIs(t *testing.T) {
Detail: "some body",
},

reqMethod: "GET",
reqMethod: "POST",
reqPath: "/api/v1/query",
reqParam: url.Values{
"query": []string{"2"},
Expand All @@ -232,7 +232,7 @@ func TestAPIs(t *testing.T) {
Detail: "some body",
},

reqMethod: "GET",
reqMethod: "POST",
reqPath: "/api/v1/query",
reqParam: url.Values{
"query": []string{"2"},
Expand All @@ -249,7 +249,7 @@ func TestAPIs(t *testing.T) {
}),
inErr: fmt.Errorf("some error"),

reqMethod: "GET",
reqMethod: "POST",
reqPath: "/api/v1/query_range",
reqParam: url.Values{
"query": []string{"2"},
Expand Down

0 comments on commit d06690e

Please sign in to comment.