1
0
mirror of https://github.com/prometheus/prometheus synced 2025-01-17 20:33:24 +00:00

web: add -web.route-prefix flag

Fixes 
This commit is contained in:
Fabian Reinartz 2016-07-05 15:05:43 +02:00
parent 6f19e418e1
commit 59d26e8536
2 changed files with 15 additions and 3 deletions
cmd/prometheus
web

View File

@ -81,6 +81,10 @@ func init() {
&cfg.prometheusURL, "web.external-url", "", &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.", "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.fs.StringVar(
&cfg.web.MetricsPath, "web.telemetry-path", "/metrics", &cfg.web.MetricsPath, "web.telemetry-path", "/metrics",
"Path under which to expose metrics.", "Path under which to expose metrics.",
@ -248,6 +252,13 @@ func parse(args []string) error {
if err := parsePrometheusURL(); err != nil { if err := parsePrometheusURL(); err != nil {
return err 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 { if err := parseInfluxdbURL(); err != nil {
return err return err
} }

View File

@ -99,6 +99,7 @@ type PrometheusVersion struct {
type Options struct { type Options struct {
ListenAddress string ListenAddress string
ExternalURL *url.URL ExternalURL *url.URL
RoutePrefix string
MetricsPath string MetricsPath string
UseLocalAssets bool UseLocalAssets bool
UserAssetsPath string UserAssetsPath string
@ -137,12 +138,12 @@ func New(
apiV1: api_v1.NewAPI(qe, st), apiV1: api_v1.NewAPI(qe, st),
} }
if o.ExternalURL.Path != "" { if o.RoutePrefix != "/" {
// If the prefix is missing for the root path, prepend it. // If the prefix is missing for the root path, prepend it.
router.Get("/", func(w http.ResponseWriter, r *http.Request) { 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 instrh := prometheus.InstrumentHandler