From 84d42f48e55c09836d2efd0c558af60c4d3f9f7a Mon Sep 17 00:00:00 2001 From: Tobias Gesellchen Date: Fri, 22 May 2015 12:29:33 +0200 Subject: [PATCH] allow to override the default os.Hostname() when creating PrometheusURLs --- web/web.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/web/web.go b/web/web.go index 2b33104b1..e3f6bd1bb 100644 --- a/web/web.go +++ b/web/web.go @@ -40,6 +40,7 @@ var localhostRepresentations = []string{"127.0.0.1", "localhost"} // Commandline flags. var ( listenAddress = flag.String("web.listen-address", ":9090", "Address to listen on for the web interface, API, and telemetry.") + hostname = flag.String("web.hostname", "", "Hostname on which the server is available.") metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.") useLocalAssets = flag.Bool("web.use-local-assets", false, "Read assets/templates from file instead of binary.") userAssetsPath = flag.String("web.user-assets", "", "Path to static asset directory, available at /user.") @@ -176,7 +177,7 @@ func getTemplate(name string, pathPrefix string) (*template.Template, error) { return lset }, "globalURL": func(url string) string { - hostname, err := os.Hostname() + hostname, err := getHostname() if err != nil { log.Warnf("Couldn't get hostname: %s, returning target.URL()", err) return url @@ -240,9 +241,16 @@ func MustBuildServerURL(pathPrefix string) string { if err != nil { panic(err) } - hostname, err := os.Hostname() + hostname, err := getHostname() if err != nil { panic(err) } return fmt.Sprintf("http://%s:%s%s", hostname, port, pathPrefix) } + +func getHostname() (string, error) { + if *hostname != "" { + return *hostname, nil + } + return os.Hostname() +}