Skip to content

Commit

Permalink
support template for prometheus label value
Browse files Browse the repository at this point in the history
  • Loading branch information
UlricQin committed Jun 10, 2022
1 parent 7263332 commit 8a4afb4
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 2 deletions.
3 changes: 3 additions & 0 deletions conf/input.prometheus/prometheus.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
# "http://localhost:9104/metrics"
# ]

url_label_key = "instance"
url_label_value = "{{.Host}}"

## Scrape Services available in Consul Catalog
[instances.consul]
enabled = false
Expand Down
2 changes: 1 addition & 1 deletion conf/logs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
## key 占位符
api_key = "ef4ahfbwzwwtlwfpbertgq1i6mq0ab1q"
## 是否开启日志采集
enable = true
enable = false
## 接受日志的server地址
send_to = "127.0.0.1:17878"
## 发送日志的协议 http/tcp
Expand Down
53 changes: 53 additions & 0 deletions config/urllabel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package config

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

type UrlLabel struct {
LabelKey string `toml:"url_label_key"`
LabelValue string `toml:"url_label_value"`
LabelValueTpl *template.Template
}

func (ul *UrlLabel) PrepareUrlTemplate() error {
if ul.LabelKey == "" {
ul.LabelKey = "instance"
}

if ul.LabelValue != "" {
var err error
ul.LabelValueTpl, err = template.New("v").Parse(ul.LabelValue)
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
}

dict := map[string]string{
"Scheme": u.Scheme,
"Host": u.Host,
"Hostname": u.Hostname(),
"Port": u.Port(),
"Path": u.Path,
"Query": u.RawQuery,
"Fragment": u.Fragment,
}

var buffer bytes.Buffer
err := ul.LabelValueTpl.Execute(&buffer, dict)
if err != nil {
return "", "", err
}

return ul.LabelKey, buffer.String(), nil
}
17 changes: 16 additions & 1 deletion inputs/prometheus/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ type Instance struct {
IgnoreLabelKeys []string `toml:"ignore_label_keys"`
Headers []string `toml:"headers"`

config.UrlLabel

ignoreMetricsFilter filter.Filter
ignoreLabelKeysFilter filter.Filter
tls.ClientConfig
Expand Down Expand Up @@ -81,6 +83,10 @@ func (ins *Instance) Init() error {
}
}

if err := ins.PrepareUrlTemplate(); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -206,7 +212,16 @@ func (p *Prometheus) gatherUrl(urlwg *sync.WaitGroup, slist *list.SafeList, ins

ins.setHeaders(req)

labels := map[string]string{"url": u.String()}
labels := map[string]string{}

urlKey, urlVal, err := ins.GenerateLabel(u)
if err != nil {
log.Println("E! failed to generate url label value:", err)
return
}

labels[urlKey] = urlVal

for key, val := range ins.Labels {
labels[key] = val
}
Expand Down

0 comments on commit 8a4afb4

Please sign in to comment.