From f1d225c83624a966d3c3aeb82f6affc34090dea5 Mon Sep 17 00:00:00 2001 From: Tom Wilkie Date: Mon, 30 Nov 2015 14:15:07 +0000 Subject: [PATCH] Add hostname to /api --- app/router.go | 7 ++++++- {probe => common/hostname}/hostname.go | 4 ++-- prog/probe/main.go | 3 ++- xfer/app_client.go | 5 +++-- 4 files changed, 13 insertions(+), 6 deletions(-) rename {probe => common/hostname}/hostname.go (85%) diff --git a/app/router.go b/app/router.go index 3d142f588..ab8b6f260 100644 --- a/app/router.go +++ b/app/router.go @@ -11,6 +11,7 @@ import ( "github.com/PuerkitoBio/ghost/handlers" "github.com/gorilla/mux" + "github.com/weaveworks/scope/common/hostname" "github.com/weaveworks/scope/report" "github.com/weaveworks/scope/xfer" ) @@ -105,5 +106,9 @@ func makeReportPostHandler(a Adder) http.HandlerFunc { } func apiHandler(w http.ResponseWriter, r *http.Request) { - respondWith(w, http.StatusOK, xfer.Details{ID: uniqueID, Version: version}) + respondWith(w, http.StatusOK, xfer.Details{ + ID: uniqueID, + Version: version, + Hostname: hostname.Get(), + }) } diff --git a/probe/hostname.go b/common/hostname/hostname.go similarity index 85% rename from probe/hostname.go rename to common/hostname/hostname.go index 305e6beea..722ab2d6d 100644 --- a/probe/hostname.go +++ b/common/hostname/hostname.go @@ -1,9 +1,9 @@ -package probe +package hostname import "os" // Hostname returns the hostname of this host. -func Hostname() string { +func Get() string { if hostname := os.Getenv("SCOPE_HOSTNAME"); hostname != "" { return hostname } diff --git a/prog/probe/main.go b/prog/probe/main.go index 33d4c6636..553d58d04 100644 --- a/prog/probe/main.go +++ b/prog/probe/main.go @@ -16,6 +16,7 @@ import ( "github.com/armon/go-metrics" "github.com/weaveworks/weave/common" + "github.com/weaveworks/scope/common/hostname" "github.com/weaveworks/scope/probe" "github.com/weaveworks/scope/probe/controls" "github.com/weaveworks/scope/probe/docker" @@ -78,7 +79,7 @@ func main() { rand.Seed(time.Now().UnixNano()) probeID := strconv.FormatInt(rand.Int63(), 16) var ( - hostName = probe.Hostname() + hostName = hostname.Get() hostID = hostName // TODO(pb): we should sanitize the hostname ) log.Printf("probe starting, version %s, ID %s", version, probeID) diff --git a/xfer/app_client.go b/xfer/app_client.go index 1b1fd579d..da6998683 100644 --- a/xfer/app_client.go +++ b/xfer/app_client.go @@ -15,8 +15,9 @@ import ( // Details are some generic details that can be fetched from /api type Details struct { - ID string `json:"id"` - Version string `json:"version"` + ID string `json:"id"` + Version string `json:"version"` + Hostname string `json:"hostname"` } // AppClient is a client to an app for dealing with controls.