Skip to content

Commit 9a19286

Browse files
committed
Add distributor endpoint for validating PromQL expressions
Returns a JSON object of the form: { "valid": true|false } Fixes #176
1 parent 05ef9d6 commit 9a19286

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

cmd/cortex/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ func setupQuerier(
310310
}).WithPrefix("/api/prom/api/v1")
311311
api.Register(promRouter)
312312
router.PathPrefix("/api/v1").Handler(promRouter)
313+
router.Path("/validate_expr").Handler(http.HandlerFunc(distributor.ValidateExprHandler))
313314
router.Path("/user_stats").Handler(http.HandlerFunc(distributor.UserStatsHandler))
314315
router.Path("/graph").Handler(ui.GraphHandler())
315316
router.PathPrefix("/static/").Handler(ui.StaticAssetsHandler("/api/prom/static/"))

distributor/http_server.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"net/http"
55

66
"github.com/prometheus/common/log"
7+
"github.com/prometheus/prometheus/promql"
78
"github.com/prometheus/prometheus/storage/remote"
89

910
"github.com/weaveworks/cortex/util"
@@ -54,3 +55,10 @@ func (d *Distributor) UserStatsHandler(w http.ResponseWriter, r *http.Request) {
5455

5556
util.WriteJSONResponse(w, stats)
5657
}
58+
59+
// ValidateExprHandler validates a PromQL expression.
60+
func (d *Distributor) ValidateExprHandler(w http.ResponseWriter, r *http.Request) {
61+
_, err := promql.ParseExpr(r.FormValue("expr"))
62+
valid := (err == nil)
63+
util.WriteJSONResponse(w, map[string]bool{"valid": valid})
64+
}

0 commit comments

Comments
 (0)