diff --git a/config/urllabel.go b/config/urllabel.go index b6fa7e35..2f927dfb 100644 --- a/config/urllabel.go +++ b/config/urllabel.go @@ -2,7 +2,9 @@ package config import ( "bytes" + "log" "net/url" + "strings" "text/template" ) @@ -10,9 +12,15 @@ type UrlLabel struct { LabelKey string `toml:"url_label_key"` LabelValue string `toml:"url_label_value"` LabelValueTpl *template.Template + + LabelPair map[string]string `toml:"url_label_pair"` + LabelPairTpl *template.Template `toml:"-"` } func (ul *UrlLabel) PrepareUrlTemplate() error { + if ul.LabelKey == "-" && len(ul.LabelPair) == 0 { + return nil + } if ul.LabelKey == "" { ul.LabelKey = "instance" } @@ -24,13 +32,36 @@ func (ul *UrlLabel) PrepareUrlTemplate() error { return err } } + if len(ul.LabelPair) > 0 { + var err error + value := "" + for k, v := range ul.LabelPair { + if v == "" || v == " " { + delete(ul.LabelPair, k) + continue + } + if len(value) != 0 { + value = value + "||" + k + "=" + v + } else { + value = k + "=" + v + } + } + if Config.DebugMode { + log.Printf("D! label pair tpl:%s", value) + } + ul.LabelPairTpl, err = template.New("pair").Parse(value) + if err != nil { + return err + } + } return nil } -func (ul *UrlLabel) GenerateLabel(u *url.URL) (string, string, error) { - if ul.LabelValue == "" { - return ul.LabelKey, u.String(), nil +func (ul *UrlLabel) GenerateLabel(u *url.URL) (map[string]string, error) { + ret := make(map[string]string) + if ul.LabelKey == "-" && len(ul.LabelPair) == 0 { + return ret, nil } dict := map[string]string{ @@ -44,10 +75,37 @@ func (ul *UrlLabel) GenerateLabel(u *url.URL) (string, string, error) { } var buffer bytes.Buffer - err := ul.LabelValueTpl.Execute(&buffer, dict) - if err != nil { - return "", "", err + if ul.LabelKey != "-" { + if ul.LabelValue != "" { + err := ul.LabelValueTpl.Execute(&buffer, dict) + if err != nil { + return ret, err + } + ret[ul.LabelKey] = buffer.String() + } else { + ret[ul.LabelKey] = u.String() + } + buffer.Reset() + } + if len(ul.LabelPair) > 0 { + var buffer bytes.Buffer + err := ul.LabelPairTpl.Execute(&buffer, dict) + if err != nil { + return ret, err + } + pairs := strings.Split(buffer.String(), "||") + for idx := range pairs { + kvs := strings.SplitN(pairs[idx], "=", 2) + if len(kvs) != 2 { + continue + } + if Config.DebugMode { + log.Printf("D! label pairs after rendering: %s=%s", kvs[0], kvs[1]) + } + ret[kvs[0]] = kvs[1] + } + buffer.Reset() } - return ul.LabelKey, buffer.String(), nil + return ret, nil } diff --git a/inputs/appdynamics/instances.go b/inputs/appdynamics/instances.go index f8f02ef1..cd08a837 100644 --- a/inputs/appdynamics/instances.go +++ b/inputs/appdynamics/instances.go @@ -253,15 +253,13 @@ func (ins *Instance) gather(slist *types.SampleList, link string, labels map[str ins.setHeaders(req) - if ins.LabelKey != "-" { - if ins.LabelValue != "" { - urlKey, urlVal, err := ins.GenerateLabel(u) - if err != nil { - log.Println("E! failed to generate url label value:", err) - return - } - labels[urlKey] = urlVal - } + gTags, err := ins.GenerateLabel(u) + if err != nil { + log.Println("E! failed to generate url label value:", err) + return + } + for k, v := range gTags { + labels[k] = v } res, err := ins.client.Do(req) diff --git a/inputs/cadvisor/instances.go b/inputs/cadvisor/instances.go index b7e4acf5..e1780504 100644 --- a/inputs/cadvisor/instances.go +++ b/inputs/cadvisor/instances.go @@ -216,16 +216,10 @@ func (ins *Instance) Gather(slist *types.SampleList) { ins.setHeaders(req) - labels := map[string]string{} - - if ins.UrlLabel.LabelKey != "-" { - urlKey, urlVal, err := ins.GenerateLabel(ins.u) - if err != nil { - log.Println("E! failed to generate url label value:", err) - return - } - - labels[urlKey] = urlVal + labels, err := ins.GenerateLabel(ins.u) + if err != nil { + log.Println("E! failed to generate url label value:", err) + return } res, err := ins.client.Do(req) diff --git a/inputs/prometheus/prometheus.go b/inputs/prometheus/prometheus.go index 3bbd1bd5..fe7b06fd 100644 --- a/inputs/prometheus/prometheus.go +++ b/inputs/prometheus/prometheus.go @@ -194,16 +194,10 @@ func (ins *Instance) gatherUrl(urlwg *sync.WaitGroup, slist *types.SampleList, u ins.setHeaders(req) - labels := map[string]string{} - - if ins.UrlLabel.LabelKey != "-" { - urlKey, urlVal, err := ins.GenerateLabel(u) - if err != nil { - log.Println("E! failed to generate url label value:", err) - return - } - - labels[urlKey] = urlVal + labels, err := ins.GenerateLabel(u) + if err != nil { + log.Println("E! failed to generate url label value:", err) + return } for key, val := range uri.Tags {