From 658a8742ac7b1ce50d68d6a3ecaa8947ec605715 Mon Sep 17 00:00:00 2001 From: DexterZhang <1105367836@qq.com> Date: Sun, 3 May 2020 21:51:03 +0800 Subject: [PATCH] fix(vmagent): different behavior as how prometheus deal with labels. [Issue#453] (#454) --- lib/promrelabel/relabel.go | 4 +++- lib/promscrape/config.go | 19 ++++++++++++++----- lib/protoparser/prometheus/parser.go | 3 ++- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/lib/promrelabel/relabel.go b/lib/promrelabel/relabel.go index 474f46361..72cf8bd3a 100644 --- a/lib/promrelabel/relabel.go +++ b/lib/promrelabel/relabel.go @@ -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 } diff --git a/lib/promscrape/config.go b/lib/promscrape/config.go index 1b335103a..a316d62c0 100644 --- a/lib/promscrape/config.go +++ b/lib/promscrape/config.go @@ -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 { diff --git a/lib/protoparser/prometheus/parser.go b/lib/protoparser/prometheus/parser.go index 27cd4a637..e2e576747 100644 --- a/lib/protoparser/prometheus/parser.go +++ b/lib/protoparser/prometheus/parser.go @@ -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 {