From 59d26e8536d3f85fcb7fd42acc4453e7eb668e8d Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Tue, 5 Jul 2016 15:05:43 +0200 Subject: [PATCH] web: add -web.route-prefix flag Fixes #1191 --- cmd/prometheus/config.go | 11 +++++++++++ web/web.go | 7 ++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/cmd/prometheus/config.go b/cmd/prometheus/config.go index 59f0c336c8..f2b3978b67 100644 --- a/cmd/prometheus/config.go +++ b/cmd/prometheus/config.go @@ -81,6 +81,10 @@ func init() { &cfg.prometheusURL, "web.external-url", "", "The URL under which Prometheus is externally reachable (for example, if Prometheus is served via a reverse proxy). Used for generating relative and absolute links back to Prometheus itself. If the URL has a path portion, it will be used to prefix all HTTP endpoints served by Prometheus. If omitted, relevant URL components will be derived automatically.", ) + cfg.fs.StringVar( + &cfg.web.RoutePrefix, "web.route-prefix", "", + "Prefix for the internal routes of web endpoints. Defaults to path of -web.external-url.", + ) cfg.fs.StringVar( &cfg.web.MetricsPath, "web.telemetry-path", "/metrics", "Path under which to expose metrics.", @@ -248,6 +252,13 @@ func parse(args []string) error { if err := parsePrometheusURL(); err != nil { return err } + // Default -web.route-prefix to path of -web.external-url. + if cfg.web.RoutePrefix == "" { + cfg.web.RoutePrefix = cfg.web.ExternalURL.Path + } + // RoutePrefix must always be at least '/'. + cfg.web.RoutePrefix = "/" + strings.Trim(cfg.web.RoutePrefix, "/") + if err := parseInfluxdbURL(); err != nil { return err } diff --git a/web/web.go b/web/web.go index a231592721..338d6f0f5f 100644 --- a/web/web.go +++ b/web/web.go @@ -99,6 +99,7 @@ type PrometheusVersion struct { type Options struct { ListenAddress string ExternalURL *url.URL + RoutePrefix string MetricsPath string UseLocalAssets bool UserAssetsPath string @@ -137,12 +138,12 @@ func New( apiV1: api_v1.NewAPI(qe, st), } - if o.ExternalURL.Path != "" { + if o.RoutePrefix != "/" { // If the prefix is missing for the root path, prepend it. router.Get("/", func(w http.ResponseWriter, r *http.Request) { - http.Redirect(w, r, o.ExternalURL.Path, http.StatusFound) + http.Redirect(w, r, o.RoutePrefix, http.StatusFound) }) - router = router.WithPrefix(o.ExternalURL.Path) + router = router.WithPrefix(o.RoutePrefix) } instrh := prometheus.InstrumentHandler