diff --git a/api/prometheus/v1/api.go b/api/prometheus/v1/api.go index 8a72f9bfc..ce36e1408 100644 --- a/api/prometheus/v1/api.go +++ b/api/prometheus/v1/api.go @@ -381,6 +381,7 @@ const ( epRuntimeinfo = apiPrefix + "/status/runtimeinfo" epTSDB = apiPrefix + "/status/tsdb" epWalReplay = apiPrefix + "/status/walreplay" + epFormatQuery = apiPrefix + "/format_query" ) // AlertState models the state of an alert. @@ -1380,6 +1381,20 @@ func (h *httpAPI) QueryExemplars(ctx context.Context, query string, startTime, e return res, err } +func (h *httpAPI) FormatQuery(ctx context.Context, query string) (string, error) { + u := h.client.URL(epFormatQuery, nil) + q := u.Query() + q.Set("query", query) + + _, body, _, err := h.client.DoGetFallback(ctx, u, q) + if err != nil { + return "", err + } + + // var qres queryResult + return string(body), nil +} + // Warnings is an array of non critical errors type Warnings []string diff --git a/api/prometheus/v1/api_test.go b/api/prometheus/v1/api_test.go index bacf6a096..8b417db7d 100644 --- a/api/prometheus/v1/api_test.go +++ b/api/prometheus/v1/api_test.go @@ -240,6 +240,13 @@ func TestAPIs(t *testing.T) { } } + doFormatQuery := func(query string) func() (interface{}, Warnings, error) { + return func() (interface{}, Warnings, error) { + v, err := promAPI.FormatQuery(context.Background(), query) + return v, nil, err + } + } + queryTests := []apiTest{ { do: doQuery("2", testTime, WithTimeout(5*time.Second)), @@ -1206,6 +1213,13 @@ func TestAPIs(t *testing.T) { }, }, }, + { + do: doFormatQuery("foo/bar"), + reqMethod: "POST", + reqPath: "/api/v1/format_query", + inRes: "foo / bar", + res: "\"foo / bar\"", + }, } var tests []apiTest