fix(vmagent): different behavior as how prometheus deal with labels. [Issue#453] (#454)

This commit is contained in:
DexterZhang 2020-05-03 21:51:03 +08:00 committed by Aliaksandr Valialkin
parent ab1e6a76bb
commit 317688f144
3 changed files with 19 additions and 7 deletions

View file

@ -45,10 +45,12 @@ func ApplyRelabelConfigs(labels []prompbmarshal.Label, labelsOffset int, prcs []
}
labels = tmp
}
labels = removeEmptyLabels(labels, labelsOffset)
if isFinalize {
labels = FinalizeLabels(labels[:labelsOffset], labels[labelsOffset:])
}
// remove empty empty labels after finalize, or we can't tell if the label value
// is null character or not set when finalize labels
labels = removeEmptyLabels(labels, labelsOffset)
SortLabels(labels[labelsOffset:])
return labels
}

View file

@ -426,11 +426,20 @@ func appendScrapeWork(dst []ScrapeWork, swc *scrapeWorkConfig, target string, ex
// Drop target without scrape address.
return dst, nil
}
targetRelabeled := addMissingPort(schemeRelabeled, addressRelabeled)
if strings.Contains(targetRelabeled, "/") {
if strings.Contains(addressRelabeled, "/") {
// Drop target with '/'
return dst, nil
}
instanceRelabeled := promrelabel.GetLabelValueByName(labels, "instance")
if instanceRelabeled == "" {
// After relabeling, the instance label is set to the value of __address__ by default
// if it was not set during relabeling.
labels = append(labels, prompbmarshal.Label{
Name: "instance",
Value: addressRelabeled,
})
}
metricsPathRelabeled := promrelabel.GetLabelValueByName(labels, "__metrics_path__")
if metricsPathRelabeled == "" {
metricsPathRelabeled = "/metrics"
@ -441,10 +450,10 @@ func appendScrapeWork(dst []ScrapeWork, swc *scrapeWorkConfig, target string, ex
optionalQuestion = ""
}
paramsStr := url.Values(paramsRelabeled).Encode()
scrapeURL := fmt.Sprintf("%s://%s%s%s%s", schemeRelabeled, targetRelabeled, metricsPathRelabeled, optionalQuestion, paramsStr)
scrapeURL := fmt.Sprintf("%s://%s%s%s%s", schemeRelabeled, addressRelabeled, metricsPathRelabeled, optionalQuestion, paramsStr)
if _, err := url.Parse(scrapeURL); err != nil {
return dst, fmt.Errorf("invalid url %q for scheme=%q (%q), target=%q (%q), metrics_path=%q (%q) for `job_name` %q: %s",
scrapeURL, swc.scheme, schemeRelabeled, target, targetRelabeled, swc.metricsPath, metricsPathRelabeled, swc.jobName, err)
scrapeURL, swc.scheme, schemeRelabeled, target, addressRelabeled, swc.metricsPath, metricsPathRelabeled, swc.jobName, err)
}
dst = append(dst, ScrapeWork{
ID: atomic.AddUint64(&nextScrapeWorkID, 1),
@ -489,7 +498,7 @@ func mergeLabels(job, scheme, target, metricsPath string, extraLabels, externalL
m[k] = v
}
m["job"] = job
m["__address__"] = target
m["__address__"] = addMissingPort(scheme, target)
m["__scheme__"] = scheme
m["__metrics_path__"] = metricsPath
for k, args := range params {

View file

@ -230,7 +230,8 @@ func unmarshalTags(dst []Tag, s string, noEscapes bool) (string, []Tag, error) {
}
s = s[n+1:]
}
if len(key) > 0 && len(value) > 0 {
// keep null character label value
if len(key) > 0 {
if cap(dst) > len(dst) {
dst = dst[:len(dst)+1]
} else {