Skip to content

Commit

Permalink
Merge pull request #626 from kongfei605/multi_url_label
Browse files Browse the repository at this point in the history
support multiple url label template
  • Loading branch information
kongfei605 authored Aug 29, 2023
2 parents 48139f8 + 714e039 commit 64cc5ee
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 36 deletions.
72 changes: 65 additions & 7 deletions config/urllabel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@ package config

import (
"bytes"
"log"
"net/url"
"strings"
"text/template"
)

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"
}
Expand All @@ -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{
Expand All @@ -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
}
16 changes: 7 additions & 9 deletions inputs/appdynamics/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 4 additions & 10 deletions inputs/cadvisor/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 4 additions & 10 deletions inputs/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 64cc5ee

Please sign in to comment.