---
main.go | 32 +++++++++++++++++++++++++++++++-
1 file changed, 31 insertions(+), 1 deletion(-)
diff --git a/main.go b/main.go
index 12c5491..69fcca1 100644
--- a/main.go
+++ b/main.go
@@ -1,6 +1,7 @@
package main
import (
+ "bytes"
"encoding/json"
"fmt"
"image/color"
@@ -11,6 +12,7 @@ import (
"sort"
"strconv"
"strings"
+ "text/template"
"time"
"github.com/dustin/go-humanize"
@@ -166,6 +168,12 @@ func registerExtension(router chi.Router, extension string, mime string) {
legend = l[0]
}
+ // Label template
+ var label string
+ if l, ok := args["label"]; ok {
+ label = l[0]
+ }
+
// Set step so that there's approximately 25 data points per inch
step := int(end.Sub(start).Seconds() / (25 * float64(width / vg.Inch)))
if s, ok := args["step"]; ok {
@@ -240,7 +248,29 @@ func registerExtension(router chi.Router, extension string, mime string) {
nextColor = 0
}
plotters[i] = l
- if legend != "" {
+ if label != "" && len(res.Metric) > 2 && res.Metric[0] == '{' && res.Metric[len(res.Metric)-1] == '}' {
+ raw := res.Metric[1 : len(res.Metric)-1]
+ raw_tags := strings.Split(raw, ",")
+ tags := make(map[string]string)
+ for _, v := range raw_tags {
+ tag := strings.Split(v, "=")
+ if len(tag) != 2 {
+ log.Printf("Expected tag format: 'name=value'!")
+ continue
+ }
+ if len(tag[1]) > 2 && tag[1][0] == '"' && tag[1][len(tag[1])-1] == '"' {
+ tags[tag[0]] = tag[1][1 : len(tag[1])-1]
+ }
+ }
+ tmpl, err := template.New("label").Parse(label)
+ if err != nil {
+ log.Printf("Failed to parse label template: ", err)
+ } else {
+ var label_out bytes.Buffer
+ tmpl.Execute(&label_out, tags)
+ p.Legend.Add(label_out.String(), l)
+ }
+ } else if legend != "" {
p.Legend.Add(legend, l)
} else {
p.Legend.Add(res.Metric, l)
--
2.28.0
Can you move the bulk of this to a separate function?
Can you verify that any SVG markup is correctly escaped if the user
specifies a template with SVG elements in it?