Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions api/prometheus/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -1380,6 +1381,19 @@ 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
}

return string(body), nil
}

// Warnings is an array of non critical errors
type Warnings []string

Expand Down
14 changes: 14 additions & 0 deletions api/prometheus/v1/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down Expand Up @@ -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
Expand Down
Loading