Merge pull request #128 from prometheus/feature/convert-host-relative-links

Convert addresses pointing to localhost in status.
This commit is contained in:
juliusv 2013-04-12 07:27:30 -07:00
commit f89d4c2cac
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>