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:
<a href="http://localhost:9090/metrics.json">http://localhost:9090/metrics.json</a>

After:
<a href="http://hostname-of-prometheus-server:9090/metrics.json">http://localhost:9090/metrics.json</a>
This commit is contained in:
Johannes 'fish' Ziemke 2013-04-10 14:26:07 +02:00
parent 8fba639706
commit 14407a076a
3 changed files with 28 additions and 1 deletions

View File

@ -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
}

View File

@ -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{}
}

View File

@ -26,7 +26,7 @@
<ul>
{{range $pool.Targets}}
<li>
<a href="{{.Address}}">{{.Address}}</a> (State: {{.State}}, Base Labels: {{.BaseLabels}})
<a href="{{.GlobalAddress}}">{{.Address}}</a> (State: {{.State}}, Base Labels: {{.BaseLabels}})
</li>
{{end}}
</ul>