Browse Source

Merge pull request #1791 from prometheus/fabxc-routepref

web: add -web.route-prefix flag
pull/1809/head
Fabian Reinartz 8 years ago committed by GitHub
parent
commit
e87d604f94
  1. 11
      cmd/prometheus/config.go
  2. 7
      web/web.go

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

7
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

Loading…
Cancel
Save