From 14407a076a02d80ee66acd3d566e650804231e28 Mon Sep 17 00:00:00 2001 From: Johannes 'fish' Ziemke Date: Wed, 10 Apr 2013 14:26:07 +0200 Subject: [PATCH] Convert addresses pointing to localhost in status. Until now, targets pointing to localhost in the status view are linked to localhost, so you can't follow those links by clicking on them. This change converts the links to point to the hostname of the prometheus server. Before: http://localhost:9090/metrics.json After: http://localhost:9090/metrics.json --- retrieval/target.go | 23 +++++++++++++++++++++++ retrieval/targetmanager_test.go | 4 ++++ web/templates/status.html | 2 +- 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/retrieval/target.go b/retrieval/target.go index e1d448050..d4a4f181a 100644 --- a/retrieval/target.go +++ b/retrieval/target.go @@ -17,7 +17,10 @@ import ( "github.com/prometheus/client_golang/metrics" "github.com/prometheus/prometheus/model" "github.com/prometheus/prometheus/retrieval/format" + "log" "net/http" + "os" + "strings" "time" ) @@ -25,6 +28,10 @@ const ( instance = "instance" ) +var ( + localhostRepresentations = []string{"http://127.0.0.1", "http://localhost"} +) + // The state of the given Target. type TargetState int @@ -92,6 +99,9 @@ type Target interface { // points in this interface, this one is the best candidate to change given // the ways to express the endpoint. Address() string + // The address as seen from other hosts. References to localhost are resolved + // to the address of the prometheus server. + GlobalAddress() string // Return the target's base labels. BaseLabels() model.LabelSet // Merge a new externally supplied target definition (e.g. with changed base @@ -213,6 +223,19 @@ func (t target) Address() string { return t.address } +func (t target) GlobalAddress() string { + address := t.address + hostname, err := os.Hostname() + if err != nil { + log.Printf("Couldn't get hostname: %s, returning target.Address()") + return address + } + for _, localhostRepresentation := range localhostRepresentations { + address = strings.Replace(address, localhostRepresentation, fmt.Sprintf("http://%s", hostname), -1) + } + return address +} + func (t target) BaseLabels() model.LabelSet { return t.baseLabels } diff --git a/retrieval/targetmanager_test.go b/retrieval/targetmanager_test.go index a793b6bb2..709697a76 100644 --- a/retrieval/targetmanager_test.go +++ b/retrieval/targetmanager_test.go @@ -33,6 +33,10 @@ func (t fakeTarget) Address() string { return "fake" } +func (t fakeTarget) GlobalAddress() string { + return t.Address() +} + func (t fakeTarget) BaseLabels() model.LabelSet { return model.LabelSet{} } diff --git a/web/templates/status.html b/web/templates/status.html index 722d9dc4a..26b27b6a4 100644 --- a/web/templates/status.html +++ b/web/templates/status.html @@ -26,7 +26,7 @@