Change instance identifiers to be host:port

This changes the PublicURL function into InstanceIdentifier, which now
returns a simple <host>:<port> string instead of a full URL.
This commit is contained in:
Sergiusz 'q3k' Bazański 2015-02-20 16:21:13 +01:00
parent bb69a3d284
commit 0d0bb3c030
3 changed files with 39 additions and 21 deletions

View file

@ -115,8 +115,8 @@ type Target interface {
// points in this interface, this one is the best candidate to change given // points in this interface, this one is the best candidate to change given
// the ways to express the endpoint. // the ways to express the endpoint.
URL() string URL() string
// The URL without any auth data in it, used to label data // Used to populate the `instance` label in metrics.
PublicURL() string InstanceIdentifier() string
// The URL as seen from other hosts. References to localhost are resolved // The URL as seen from other hosts. References to localhost are resolved
// to the address of the prometheus server. // to the address of the prometheus server.
GlobalURL() string GlobalURL() string
@ -189,8 +189,8 @@ func (t *target) recordScrapeHealth(ingester extraction.Ingester, timestamp clie
} }
healthMetric[clientmodel.MetricNameLabel] = clientmodel.LabelValue(scrapeHealthMetricName) healthMetric[clientmodel.MetricNameLabel] = clientmodel.LabelValue(scrapeHealthMetricName)
durationMetric[clientmodel.MetricNameLabel] = clientmodel.LabelValue(scrapeDurationMetricName) durationMetric[clientmodel.MetricNameLabel] = clientmodel.LabelValue(scrapeDurationMetricName)
healthMetric[InstanceLabel] = clientmodel.LabelValue(t.PublicURL()) healthMetric[InstanceLabel] = clientmodel.LabelValue(t.InstanceIdentifier())
durationMetric[InstanceLabel] = clientmodel.LabelValue(t.PublicURL()) durationMetric[InstanceLabel] = clientmodel.LabelValue(t.InstanceIdentifier())
healthValue := clientmodel.SampleValue(0) healthValue := clientmodel.SampleValue(0)
if healthy { if healthy {
@ -323,7 +323,7 @@ func (t *target) scrape(ingester extraction.Ingester) (err error) {
return err return err
} }
baseLabels := clientmodel.LabelSet{InstanceLabel: clientmodel.LabelValue(t.PublicURL())} baseLabels := clientmodel.LabelSet{InstanceLabel: clientmodel.LabelValue(t.InstanceIdentifier())}
for baseLabel, baseValue := range t.baseLabels { for baseLabel, baseValue := range t.baseLabels {
baseLabels[baseLabel] = baseValue baseLabels[baseLabel] = baseValue
} }
@ -366,15 +366,26 @@ func (t *target) URL() string {
return t.url return t.url
} }
// PublicURL implements Target. // InstanceIdentifier implements Target.
func (t *target) PublicURL() string { func (t *target) InstanceIdentifier() string {
u, err := url.Parse(t.url) u, err := url.Parse(t.url)
if err != nil { if err != nil {
glog.Warning("Could not parse URL for auth stripping (%s), returning it unchanged", err) glog.Warningf("Could not parse instance URL when generating identifier, using raw URL: %s", err)
return t.url return t.url
} }
u.User = nil // If we are given a port in the host port, use that.
return u.String() if strings.Contains(u.Host, ":") {
return u.Host
}
// Otherwise, deduce port based on protocol.
if u.Scheme == "http" {
return fmt.Sprintf("%s:80", u.Host)
} else if u.Scheme == "https" {
return fmt.Sprintf("%s:443", u.Host)
}
glog.Warningf("Unknown scheme %s when generating identifier, using raw URL.", u.Scheme)
return t.url
} }
// GlobalURL implements Target. // GlobalURL implements Target.

View file

@ -35,15 +35,22 @@ func (i *collectResultIngester) Ingest(s clientmodel.Samples) error {
} }
func TestTargetHidesURLAuth(t *testing.T) { func TestTargetHidesURLAuth(t *testing.T) {
testTarget := target{ testVectors := []string{"http://secret:data@host.com/query?args#fragment", "https://example.net/foo", "http://foo.com:31337/bar"}
state: Unknown, testResults := []string{"host.com:80", "example.net:443", "foo.com:31337"}
url: "http://secret:data@host.com/query?args#fragment", if len(testVectors) != len(testResults) {
httpClient: utility.NewDeadlineClient(0), t.Errorf("Test vector length does not match test result length.")
} }
expected := "http://host.com/query?args#fragment"
u := testTarget.PublicURL() for i := 0; i < len(testVectors); i++ {
if u != expected { testTarget := target{
t.Errorf("Expected PublicURL to be %v, actual %v", expected, u) state: Unknown,
url: testVectors[i],
httpClient: utility.NewDeadlineClient(0),
}
u := testTarget.InstanceIdentifier()
if u != testResults[i] {
t.Errorf("Expected InstanceIdentifier to be %v, actual %v", testResults[i], u)
}
} }
} }
@ -106,7 +113,7 @@ func TestTargetRecordScrapeHealth(t *testing.T) {
expected := &clientmodel.Sample{ expected := &clientmodel.Sample{
Metric: clientmodel.Metric{ Metric: clientmodel.Metric{
clientmodel.MetricNameLabel: scrapeHealthMetricName, clientmodel.MetricNameLabel: scrapeHealthMetricName,
InstanceLabel: "http://example.url", InstanceLabel: "example.url:80",
clientmodel.JobLabel: "testjob", clientmodel.JobLabel: "testjob",
}, },
Timestamp: now, Timestamp: now,
@ -121,7 +128,7 @@ func TestTargetRecordScrapeHealth(t *testing.T) {
expected = &clientmodel.Sample{ expected = &clientmodel.Sample{
Metric: clientmodel.Metric{ Metric: clientmodel.Metric{
clientmodel.MetricNameLabel: scrapeDurationMetricName, clientmodel.MetricNameLabel: scrapeDurationMetricName,
InstanceLabel: "http://example.url", InstanceLabel: "example.url:80",
clientmodel.JobLabel: "testjob", clientmodel.JobLabel: "testjob",
}, },
Timestamp: now, Timestamp: now,

View file

@ -42,7 +42,7 @@ func (t fakeTarget) URL() string {
return "fake" return "fake"
} }
func (t fakeTarget) PublicURL() string { func (t fakeTarget) InstanceIdentifier() string {
return "fake" return "fake"
} }