Skip to content

Commit

Permalink
improve web.route-prefix handling (#2208)
Browse files Browse the repository at this point in the history
This makes the handling of web.route-prefix more similar to the
behavior in Prometheus.  Correctly handles '/' and prefixes which
do not begin with a '/'.

Signed-off-by: Paul Gier <[email protected]>
  • Loading branch information
pgier authored Mar 2, 2020
1 parent 6ebc9f0 commit 48c41eb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
9 changes: 7 additions & 2 deletions cmd/thanos/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"math"
"net/http"
"path"
"strings"
"time"

"github.com/go-kit/kit/log"
Expand Down Expand Up @@ -324,11 +325,15 @@ func runQuery(
{
router := route.New()

// RoutePrefix must always start with '/'.
webRoutePrefix = "/" + strings.Trim(webRoutePrefix, "/")

// Redirect from / to /webRoutePrefix.
if webRoutePrefix != "" {
if webRoutePrefix != "/" {
router.Get("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, webRoutePrefix, http.StatusFound)
})
router = router.WithPrefix(webRoutePrefix)
}

flagsMap := map[string]string{
Expand All @@ -338,7 +343,7 @@ func runQuery(
}

ins := extpromhttp.NewInstrumentationMiddleware(reg)
ui.NewQueryUI(logger, reg, stores, flagsMap).Register(router.WithPrefix(webRoutePrefix), ins)
ui.NewQueryUI(logger, reg, stores, flagsMap).Register(router, ins)

api := v1.NewAPI(logger, reg, engine, queryableCreator, enableAutodownsampling, enablePartialResponse, replicaLabels, instantDefaultMaxSourceResolution)

Expand Down
8 changes: 6 additions & 2 deletions cmd/thanos/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -558,11 +558,15 @@ func runRule(
{
router := route.New()

// RoutePrefix must always start with '/'.
webRoutePrefix = "/" + strings.Trim(webRoutePrefix, "/")

// Redirect from / to /webRoutePrefix.
if webRoutePrefix != "" {
if webRoutePrefix != "/" {
router.Get("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, webRoutePrefix, http.StatusFound)
})
router = router.WithPrefix(webRoutePrefix)
}

router.WithPrefix(webRoutePrefix).Post("/-/reload", func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -577,7 +581,7 @@ func runRule(

ins := extpromhttp.NewInstrumentationMiddleware(reg)

ui.NewRuleUI(logger, reg, ruleMgr, alertQueryURL.String(), flagsMap).Register(router.WithPrefix(webRoutePrefix), ins)
ui.NewRuleUI(logger, reg, ruleMgr, alertQueryURL.String(), flagsMap).Register(router, ins)

api := v1.NewAPI(logger, reg, ruleMgr)
api.Register(router.WithPrefix(path.Join(webRoutePrefix, "/api/v1")), tracer, logger, ins)
Expand Down

0 comments on commit 48c41eb

Please sign in to comment.