Make route metric names configurable#154
Conversation
|
That sounds like a good idea. However, I would like to use a different templating mechanism also because I need something similar for the access log configuration from #80. I think we should just use Then this could look like
|
|
This could potentially solve another issue for us that we are currently working around in
Having support for templates and template options in fabio would be a great feature. |
|
@leprechau Where would this be used? Lets try not to morph this PR into a multi-issue discussion. If there is another use-case for templates then lets open a separate ticket and collect thoughts there. Could you do that @leprechau ? |
|
Since template.Execute() can return an error, what are your thoughts on what TargetName should return? An error or have a backup default action if the template execution fails? Or, are you thinking about putting together a more generic set of template handling helper functions? package main
import (
"bytes"
"fmt"
"log"
"net/url"
"strings"
"text/template"
)
var routeMetricNameTemplate *template.Template
type routeMetricNameAttributes struct {
Service, Host, Path string
TargetURL *url.URL
}
func clean(s string) string {
if s == "" {
return "_"
}
s = strings.Replace(s, ".", "_", -1)
s = strings.Replace(s, ":", "_", -1)
return strings.ToLower(s)
}
// TargetName blah
func TargetName(service string, host string, path string, targetURL *url.URL) (string, error) {
var metricName bytes.Buffer
data := &routeMetricNameAttributes{service, host, path, targetURL}
err := routeMetricNameTemplate.Execute(&metricName, data)
if err != nil {
return "", err
}
return metricName.String(), nil
}
func main() {
funcMap := template.FuncMap{
"clean": clean,
}
nametemplate := "{{clean .Service}}.{{clean .Host}}.{{clean .Path}}.{{clean .TargetURL.Host}}"
routeMetricNameTemplate = template.Must(template.New("routeMetricName").Funcs(funcMap).Parse(nametemplate))
service := "hashiapp"
host := "hashiapp.com"
path := "/"
target, err := url.Parse("http://10.1.2.3:12345")
if err != nil {
log.Fatalln(err)
}
metricName, err := TargetName(service, host, path, target)
if err != nil {
log.Fatalln(err)
}
fmt.Println(metricName)
nametemplate = "{{clean .Service}}.{{clean .Host}}.{{clean .Path}}.latency_ns"
routeMetricNameTemplate = template.Must(template.New("routeMetricNameCustom").Funcs(funcMap).Parse(nametemplate))
metricName, err = TargetName(service, host, path, target)
if err != nil {
log.Fatalln(err)
}
fmt.Println(metricName)
} |
|
An empty string since I can check the templates during startup. |
|
(first think, then type): yes, |
|
cool, yeah, agree on the pollution, so |
|
@magiconair Yes, sorry I'll get my thoughts together and submit another PR for that use case. It really just occurred to me when you mentioned text/template in this discussion. |
|
@leprechau no need to apologize. See what you can come up with. |
|
Updated to use text/template. Hoping it might be close enough to make it easier for you to shuffle it around the way you'd prefer. The main things I wasn't sure about are handling of the return from TargetName in route.go and where I put the template verification. |
|
I think this looks good enough. I'll probably add a log line for the error you've skipped in Thanks for the work. Really appreciate it. |
This patch makes the metric names for the service routes configurable by using a template to generate them.
|
@maier I've taken your changed some smaller parts since my tests were failing. Also changed |
|
Yeah, that's good, cleaner. |
This patch makes the metric names for the service routes configurable by using a template to generate them.
|
Merged to master. |
Had request for the ability to alter the route metric names coming out of the clusters with nomad/consul testing. e.g.