Merge pull request #1791 from prometheus/fabxc-routepref

web: add -web.route-prefix flag
This commit is contained in:
Fabian Reinartz 2016-07-10 12:05:39 +02:00 committed by GitHub
commit e87d604f94
2 changed files with 15 additions and 3 deletions

View file

@ -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
}

View file

@ -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