retrieval: Add URL parameters to fullLabels too

Move all the special cases into one map, rather than
spreading the logic around.
This commit is contained in:
Brian Brazil 2015-09-25 21:51:49 +01:00 committed by Fabian Reinartz
parent a4abed1e01
commit 0f77856095
2 changed files with 41 additions and 19 deletions

View file

@ -164,6 +164,8 @@ type Target struct {
metaLabels model.LabelSet metaLabels model.LabelSet
// Any base labels that are added to this target and its metrics. // Any base labels that are added to this target and its metrics.
baseLabels model.LabelSet baseLabels model.LabelSet
// Internal labels, such as scheme.
internalLabels model.LabelSet
// What is the deadline for the HTTP or HTTPS against this endpoint. // What is the deadline for the HTTP or HTTPS against this endpoint.
deadline time.Duration deadline time.Duration
// The time between two scrapes. // The time between two scrapes.
@ -211,6 +213,11 @@ func (t *Target) Update(cfg *config.ScrapeConfig, baseLabels, metaLabels model.L
t.url.Scheme = string(baseLabels[model.SchemeLabel]) t.url.Scheme = string(baseLabels[model.SchemeLabel])
t.url.Path = string(baseLabels[model.MetricsPathLabel]) t.url.Path = string(baseLabels[model.MetricsPathLabel])
t.internalLabels = model.LabelSet{}
t.internalLabels[model.SchemeLabel] = baseLabels[model.SchemeLabel]
t.internalLabels[model.MetricsPathLabel] = baseLabels[model.MetricsPathLabel]
t.internalLabels[model.AddressLabel] = model.LabelValue(t.url.Host)
params := url.Values{} params := url.Values{}
for k, v := range cfg.Params { for k, v := range cfg.Params {
@ -224,6 +231,7 @@ func (t *Target) Update(cfg *config.ScrapeConfig, baseLabels, metaLabels model.L
} else { } else {
params[string(k[len(model.ParamLabelPrefix):])] = []string{string(v)} params[string(k[len(model.ParamLabelPrefix):])] = []string{string(v)}
} }
t.internalLabels[model.ParamLabelPrefix+k[len(model.ParamLabelPrefix):]] = v
} }
} }
t.url.RawQuery = params.Encode() t.url.RawQuery = params.Encode()
@ -561,13 +569,13 @@ func (t *Target) InstanceIdentifier() string {
func (t *Target) fullLabels() model.LabelSet { func (t *Target) fullLabels() model.LabelSet {
t.RLock() t.RLock()
defer t.RUnlock() defer t.RUnlock()
lset := make(model.LabelSet, len(t.baseLabels)+2) lset := make(model.LabelSet, len(t.baseLabels)+len(t.internalLabels))
for ln, lv := range t.baseLabels { for ln, lv := range t.baseLabels {
lset[ln] = lv lset[ln] = lv
} }
lset[model.MetricsPathLabel] = model.LabelValue(t.url.Path) for k, v := range t.internalLabels {
lset[model.AddressLabel] = model.LabelValue(t.url.Host) lset[k] = v
lset[model.SchemeLabel] = model.LabelValue(t.url.Scheme) }
return lset return lset
} }

View file

@ -287,34 +287,44 @@ func TestTargetManagerConfigUpdate(t *testing.T) {
scrapeConfigs: []*config.ScrapeConfig{testJob1}, scrapeConfigs: []*config.ScrapeConfig{testJob1},
expected: map[string][]model.LabelSet{ expected: map[string][]model.LabelSet{
"test_job1:static:0:0": { "test_job1:static:0:0": {
{model.JobLabel: "test_job1", model.InstanceLabel: "example.org:80", "testParam": "paramValue"}, {model.JobLabel: "test_job1", model.InstanceLabel: "example.org:80", "testParam": "paramValue",
{model.JobLabel: "test_job1", model.InstanceLabel: "example.com:80", "testParam": "paramValue"}, model.SchemeLabel: "", model.MetricsPathLabel: "", model.AddressLabel: "example.org:80", model.ParamLabelPrefix + "testParam": "paramValue"},
{model.JobLabel: "test_job1", model.InstanceLabel: "example.com:80", "testParam": "paramValue",
model.SchemeLabel: "", model.MetricsPathLabel: "", model.AddressLabel: "example.com:80", model.ParamLabelPrefix + "testParam": "paramValue"},
}, },
}, },
}, { }, {
scrapeConfigs: []*config.ScrapeConfig{testJob1}, scrapeConfigs: []*config.ScrapeConfig{testJob1},
expected: map[string][]model.LabelSet{ expected: map[string][]model.LabelSet{
"test_job1:static:0:0": { "test_job1:static:0:0": {
{model.JobLabel: "test_job1", model.InstanceLabel: "example.org:80", "testParam": "paramValue"}, {model.JobLabel: "test_job1", model.InstanceLabel: "example.org:80", "testParam": "paramValue",
{model.JobLabel: "test_job1", model.InstanceLabel: "example.com:80", "testParam": "paramValue"}, model.SchemeLabel: "", model.MetricsPathLabel: "", model.AddressLabel: "example.org:80", model.ParamLabelPrefix + "testParam": "paramValue"},
{model.JobLabel: "test_job1", model.InstanceLabel: "example.com:80", "testParam": "paramValue",
model.SchemeLabel: "", model.MetricsPathLabel: "", model.AddressLabel: "example.com:80", model.ParamLabelPrefix + "testParam": "paramValue"},
}, },
}, },
}, { }, {
scrapeConfigs: []*config.ScrapeConfig{testJob1, testJob2}, scrapeConfigs: []*config.ScrapeConfig{testJob1, testJob2},
expected: map[string][]model.LabelSet{ expected: map[string][]model.LabelSet{
"test_job1:static:0:0": { "test_job1:static:0:0": {
{model.JobLabel: "test_job1", model.InstanceLabel: "example.org:80", "testParam": "paramValue"}, {model.JobLabel: "test_job1", model.InstanceLabel: "example.org:80", "testParam": "paramValue",
{model.JobLabel: "test_job1", model.InstanceLabel: "example.com:80", "testParam": "paramValue"}, model.SchemeLabel: "", model.MetricsPathLabel: "", model.AddressLabel: "example.org:80", model.ParamLabelPrefix + "testParam": "paramValue"},
{model.JobLabel: "test_job1", model.InstanceLabel: "example.com:80", "testParam": "paramValue",
model.SchemeLabel: "", model.MetricsPathLabel: "", model.AddressLabel: "example.com:80", model.ParamLabelPrefix + "testParam": "paramValue"},
}, },
"test_job2:static:0:0": { "test_job2:static:0:0": {
{model.JobLabel: "test_job2", model.InstanceLabel: "example.org:8080", "foo": "bar", "new": "ox-ba"}, {model.JobLabel: "test_job2", model.InstanceLabel: "example.org:8080", "foo": "bar", "new": "ox-ba",
{model.JobLabel: "test_job2", model.InstanceLabel: "example.com:8081", "foo": "bar", "new": "ox-ba"}, model.SchemeLabel: "", model.MetricsPathLabel: "", model.AddressLabel: "example.org:8080"},
{model.JobLabel: "test_job2", model.InstanceLabel: "example.com:8081", "foo": "bar", "new": "ox-ba",
model.SchemeLabel: "", model.MetricsPathLabel: "", model.AddressLabel: "example.com:8081"},
}, },
"test_job2:static:0:1": { "test_job2:static:0:1": {
{model.JobLabel: "test_job2", model.InstanceLabel: "foo.com:1234"}, {model.JobLabel: "test_job2", model.InstanceLabel: "foo.com:1234",
model.SchemeLabel: "", model.MetricsPathLabel: "", model.AddressLabel: "foo.com:1234"},
}, },
"test_job2:static:0:2": { "test_job2:static:0:2": {
{model.JobLabel: "test_job2", model.InstanceLabel: "fixed"}, {model.JobLabel: "test_job2", model.InstanceLabel: "fixed",
model.SchemeLabel: "", model.MetricsPathLabel: "", model.AddressLabel: "foo.com:1235"},
}, },
}, },
}, { }, {
@ -324,14 +334,18 @@ func TestTargetManagerConfigUpdate(t *testing.T) {
scrapeConfigs: []*config.ScrapeConfig{testJob2}, scrapeConfigs: []*config.ScrapeConfig{testJob2},
expected: map[string][]model.LabelSet{ expected: map[string][]model.LabelSet{
"test_job2:static:0:0": { "test_job2:static:0:0": {
{model.JobLabel: "test_job2", model.InstanceLabel: "example.org:8080", "foo": "bar", "new": "ox-ba"}, {model.JobLabel: "test_job2", model.InstanceLabel: "example.org:8080", "foo": "bar", "new": "ox-ba",
{model.JobLabel: "test_job2", model.InstanceLabel: "example.com:8081", "foo": "bar", "new": "ox-ba"}, model.SchemeLabel: "", model.MetricsPathLabel: "", model.AddressLabel: "example.org:8080"},
{model.JobLabel: "test_job2", model.InstanceLabel: "example.com:8081", "foo": "bar", "new": "ox-ba",
model.SchemeLabel: "", model.MetricsPathLabel: "", model.AddressLabel: "example.com:8081"},
}, },
"test_job2:static:0:1": { "test_job2:static:0:1": {
{model.JobLabel: "test_job2", model.InstanceLabel: "foo.com:1234"}, {model.JobLabel: "test_job2", model.InstanceLabel: "foo.com:1234",
model.SchemeLabel: "", model.MetricsPathLabel: "", model.AddressLabel: "foo.com:1234"},
}, },
"test_job2:static:0:2": { "test_job2:static:0:2": {
{model.JobLabel: "test_job2", model.InstanceLabel: "fixed"}, {model.JobLabel: "test_job2", model.InstanceLabel: "fixed",
model.SchemeLabel: "", model.MetricsPathLabel: "", model.AddressLabel: "foo.com:1235"},
}, },
}, },
}, },
@ -363,7 +377,7 @@ func TestTargetManagerConfigUpdate(t *testing.T) {
for _, expt := range expTargets { for _, expt := range expTargets {
found := false found := false
for _, actt := range actTargets { for _, actt := range actTargets {
if reflect.DeepEqual(expt, actt.BaseLabels()) { if reflect.DeepEqual(expt, actt.fullLabels()) {
found = true found = true
break break
} }