Skip to content

Commit 4c76b54

Browse files
committed
feat(api/prometheus): add format_query endpoint for query formatting
The changes introduce a new `/format_query` API endpoint to handle query formatting in the Prometheus API. This includes implementing the `FormatQuery` function in `api.go` and adding corresponding test cases in `api_test.go` to ensure proper functionality.
1 parent 83a9730 commit 4c76b54

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

api/prometheus/v1/api.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,7 @@ const (
381381
epRuntimeinfo = apiPrefix + "/status/runtimeinfo"
382382
epTSDB = apiPrefix + "/status/tsdb"
383383
epWalReplay = apiPrefix + "/status/walreplay"
384+
epFormatQuery = apiPrefix + "/format_query"
384385
)
385386

386387
// AlertState models the state of an alert.
@@ -1380,6 +1381,20 @@ func (h *httpAPI) QueryExemplars(ctx context.Context, query string, startTime, e
13801381
return res, err
13811382
}
13821383

1384+
func (h *httpAPI) FormatQuery(ctx context.Context, query string) (string, error) {
1385+
u := h.client.URL(epFormatQuery, nil)
1386+
q := u.Query()
1387+
q.Set("query", query)
1388+
1389+
_, body, _, err := h.client.DoGetFallback(ctx, u, q)
1390+
if err != nil {
1391+
return "", err
1392+
}
1393+
1394+
// var qres queryResult
1395+
return string(body), nil
1396+
}
1397+
13831398
// Warnings is an array of non critical errors
13841399
type Warnings []string
13851400

api/prometheus/v1/api_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,13 @@ func TestAPIs(t *testing.T) {
240240
}
241241
}
242242

243+
doFormatQuery := func(query string) func() (interface{}, Warnings, error) {
244+
return func() (interface{}, Warnings, error) {
245+
v, err := promAPI.FormatQuery(context.Background(), query)
246+
return v, nil, err
247+
}
248+
}
249+
243250
queryTests := []apiTest{
244251
{
245252
do: doQuery("2", testTime, WithTimeout(5*time.Second)),
@@ -1206,6 +1213,13 @@ func TestAPIs(t *testing.T) {
12061213
},
12071214
},
12081215
},
1216+
{
1217+
do: doFormatQuery("foo/bar"),
1218+
reqMethod: "POST",
1219+
reqPath: "/api/v1/format_query",
1220+
inRes: "foo / bar",
1221+
res: "\"foo / bar\"",
1222+
},
12091223
}
12101224

12111225
var tests []apiTest

0 commit comments

Comments
 (0)