-
Notifications
You must be signed in to change notification settings - Fork 263
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support template for prometheus label value
- Loading branch information
Showing
4 changed files
with
73 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters