Merge pull request #544 from q3k/feature/hide-instance-auth

Hide HTTP auth parts from instance label
This commit is contained in:
Julius Volz 2015-02-20 17:34:04 +01:00
commit 79834edcb5
3 changed files with 54 additions and 5 deletions

View File

@ -17,6 +17,7 @@ import (
"fmt" "fmt"
"math/rand" "math/rand"
"net/http" "net/http"
"net/url"
"os" "os"
"strings" "strings"
"sync" "sync"
@ -114,6 +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
// Used to populate the `instance` label in metrics.
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
@ -186,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.URL()) healthMetric[InstanceLabel] = clientmodel.LabelValue(t.InstanceIdentifier())
durationMetric[InstanceLabel] = clientmodel.LabelValue(t.URL()) durationMetric[InstanceLabel] = clientmodel.LabelValue(t.InstanceIdentifier())
healthValue := clientmodel.SampleValue(0) healthValue := clientmodel.SampleValue(0)
if healthy { if healthy {
@ -320,7 +323,7 @@ func (t *target) scrape(ingester extraction.Ingester) (err error) {
return err return err
} }
baseLabels := clientmodel.LabelSet{InstanceLabel: clientmodel.LabelValue(t.URL())} 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
} }
@ -363,6 +366,28 @@ func (t *target) URL() string {
return t.url return t.url
} }
// InstanceIdentifier implements Target.
func (t *target) InstanceIdentifier() string {
u, err := url.Parse(t.url)
if err != nil {
glog.Warningf("Could not parse instance URL when generating identifier, using raw URL: %s", err)
return t.url
}
// If we are given a port in the host port, use that.
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.
func (t *target) GlobalURL() string { func (t *target) GlobalURL() string {
url := t.url url := t.url

View File

@ -34,6 +34,26 @@ func (i *collectResultIngester) Ingest(s clientmodel.Samples) error {
return nil return nil
} }
func TestTargetHidesURLAuth(t *testing.T) {
testVectors := []string{"http://secret:data@host.com/query?args#fragment", "https://example.net/foo", "http://foo.com:31337/bar"}
testResults := []string{"host.com:80", "example.net:443", "foo.com:31337"}
if len(testVectors) != len(testResults) {
t.Errorf("Test vector length does not match test result length.")
}
for i := 0; i < len(testVectors); i++ {
testTarget := target{
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)
}
}
}
func TestTargetScrapeUpdatesState(t *testing.T) { func TestTargetScrapeUpdatesState(t *testing.T) {
testTarget := target{ testTarget := target{
state: Unknown, state: Unknown,
@ -93,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,
@ -108,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,6 +42,10 @@ func (t fakeTarget) URL() string {
return "fake" return "fake"
} }
func (t fakeTarget) InstanceIdentifier() string {
return "fake"
}
func (t fakeTarget) GlobalURL() string { func (t fakeTarget) GlobalURL() string {
return t.URL() return t.URL()
} }