Skip to content

Commit

Permalink
web: add -web.route-prefix flag
Browse files Browse the repository at this point in the history
  • Loading branch information
fabxc committed Jul 7, 2016
1 parent 6f19e41 commit 59d26e8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
11 changes: 11 additions & 0 deletions cmd/prometheus/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down Expand Up @@ -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
}
Expand Down
7 changes: 4 additions & 3 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ type PrometheusVersion struct {
type Options struct {
ListenAddress string
ExternalURL *url.URL
RoutePrefix string
MetricsPath string
UseLocalAssets bool
UserAssetsPath string
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 59d26e8

Please sign in to comment.