mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2025-02-09 15:27:11 +00:00
lib/promscrape: add promrelabel.GetLabelValueByName helper function
This commit is contained in:
parent
3945bf9dec
commit
636e1578de
3 changed files with 21 additions and 20 deletions
|
@ -280,3 +280,14 @@ func GetLabelByName(labels []prompbmarshal.Label, name string) *prompbmarshal.La
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetLabelValueByName returns value for label with the given name from labels.
|
||||||
|
//
|
||||||
|
// It returns empty string for non-existing label.
|
||||||
|
func GetLabelValueByName(labels []prompbmarshal.Label, name string) string {
|
||||||
|
label := GetLabelByName(labels, name)
|
||||||
|
if label == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return label.Value
|
||||||
|
}
|
||||||
|
|
|
@ -191,11 +191,11 @@ func (cfg *Config) getFileSDScrapeWork(prev []ScrapeWork) []ScrapeWork {
|
||||||
swPrev := make(map[string][]ScrapeWork)
|
swPrev := make(map[string][]ScrapeWork)
|
||||||
for i := range prev {
|
for i := range prev {
|
||||||
sw := &prev[i]
|
sw := &prev[i]
|
||||||
label := promrelabel.GetLabelByName(sw.Labels, "__vm_filepath")
|
filepath := promrelabel.GetLabelValueByName(sw.Labels, "__vm_filepath")
|
||||||
if label == nil {
|
if len(filepath) == 0 {
|
||||||
logger.Panicf("BUG: missing `__vm_filepath` label")
|
logger.Panicf("BUG: missing `__vm_filepath` label")
|
||||||
} else {
|
} else {
|
||||||
swPrev[label.Value] = append(swPrev[label.Value], *sw)
|
swPrev[filepath] = append(swPrev[filepath], *sw)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var dst []ScrapeWork
|
var dst []ScrapeWork
|
||||||
|
@ -439,27 +439,21 @@ func appendScrapeWork(dst []ScrapeWork, swc *scrapeWorkConfig, target string, ex
|
||||||
return dst, nil
|
return dst, nil
|
||||||
}
|
}
|
||||||
// See https://www.robustperception.io/life-of-a-label
|
// See https://www.robustperception.io/life-of-a-label
|
||||||
schemeRelabeled := ""
|
schemeRelabeled := promrelabel.GetLabelValueByName(labels, "__scheme__")
|
||||||
if schemeLabel := promrelabel.GetLabelByName(labels, "__scheme__"); schemeLabel != nil {
|
if len(schemeRelabeled) == 0 {
|
||||||
schemeRelabeled = schemeLabel.Value
|
|
||||||
}
|
|
||||||
if schemeRelabeled == "" {
|
|
||||||
schemeRelabeled = "http"
|
schemeRelabeled = "http"
|
||||||
}
|
}
|
||||||
addressLabel := promrelabel.GetLabelByName(labels, "__address__")
|
addressRelabeled := promrelabel.GetLabelValueByName(labels, "__address__")
|
||||||
if addressLabel == nil || addressLabel.Name == "" {
|
if len(addressRelabeled) == 0 {
|
||||||
// Drop target without scrape address.
|
// Drop target without scrape address.
|
||||||
return dst, nil
|
return dst, nil
|
||||||
}
|
}
|
||||||
targetRelabeled := addMissingPort(schemeRelabeled, addressLabel.Value)
|
targetRelabeled := addMissingPort(schemeRelabeled, addressRelabeled)
|
||||||
if strings.Contains(targetRelabeled, "/") {
|
if strings.Contains(targetRelabeled, "/") {
|
||||||
// Drop target with '/'
|
// Drop target with '/'
|
||||||
return dst, nil
|
return dst, nil
|
||||||
}
|
}
|
||||||
metricsPathRelabeled := ""
|
metricsPathRelabeled := promrelabel.GetLabelValueByName(labels, "__metrics_path__")
|
||||||
if metricsPathLabel := promrelabel.GetLabelByName(labels, "__metrics_path__"); metricsPathLabel != nil {
|
|
||||||
metricsPathRelabeled = metricsPathLabel.Value
|
|
||||||
}
|
|
||||||
if metricsPathRelabeled == "" {
|
if metricsPathRelabeled == "" {
|
||||||
metricsPathRelabeled = "/metrics"
|
metricsPathRelabeled = "/metrics"
|
||||||
}
|
}
|
||||||
|
|
|
@ -62,11 +62,7 @@ type ScrapeWork struct {
|
||||||
|
|
||||||
// Job returns job for the ScrapeWork
|
// Job returns job for the ScrapeWork
|
||||||
func (sw *ScrapeWork) Job() string {
|
func (sw *ScrapeWork) Job() string {
|
||||||
label := promrelabel.GetLabelByName(sw.Labels, "job")
|
return promrelabel.GetLabelValueByName(sw.Labels, "job")
|
||||||
if label == nil {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
return label.Value
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type scrapeWork struct {
|
type scrapeWork struct {
|
||||||
|
|
Loading…
Reference in a new issue