Commit
Author: Krystian ChachuĊ‚a [krystian@krystianch.com]
Committer: Drew DeVault [sir@cmpwn.com] Thu, 09 Dec 2021 23:03:42 +0000
Hash: e07b34469d74c3bd02f221e8187f63a70ed0e6aa
Timestamp: Thu, 09 Dec 2021 23:03:42 +0000 (3 years ago)

+6 -19 +/-1 browse
Fix label when metric starts with name
1diff --git a/main.go b/main.go
2index 2ba02e8..6d90470 100644
3--- a/main.go
4+++ b/main.go
5 @@ -46,7 +46,7 @@ type Datapoint struct {
6 }
7
8 type PromResult struct {
9- Metric string
10+ Metric map[string]string
11 Values []Datapoint
12 }
13
14 @@ -84,7 +84,7 @@ func Query(q string, start time.Time, end time.Time, step int) ([]PromResult, er
15 var results []PromResult
16 for _, res := range data.Data.Result {
17 r := PromResult{}
18- r.Metric = metricName(res.Metric)
19+ r.Metric = res.Metric
20
21 var values []Datapoint
22 isValid := true
23 @@ -136,26 +136,13 @@ func metricName(metric map[string]string) string {
24 return out + "{" + strings.Join(inner, ",") + "}"
25 }
26
27- func handleLabel(p *plot.Plot, l *plotter.Line, label string, metric string) {
28- raw := metric[1 : len(metric)-1]
29- raw_tags := strings.Split(raw, ",")
30- tags := make(map[string]string)
31- for _, v := range raw_tags {
32- tag := strings.Split(v, "=")
33- if len(tag) != 2 {
34- log.Printf("Expected tag format: 'name=value'!")
35- continue
36- }
37- if len(tag[1]) > 2 && tag[1][0] == '"' && tag[1][len(tag[1])-1] == '"' {
38- tags[tag[0]] = tag[1][1 : len(tag[1])-1]
39- }
40- }
41+ func handleLabel(p *plot.Plot, l *plotter.Line, label string, metric map[string]string) {
42 tmpl, err := template.New("label").Parse(label)
43 if err != nil {
44 log.Printf("Failed to parse label template: %v", err)
45 } else {
46 var label_out bytes.Buffer
47- tmpl.Execute(&label_out, tags)
48+ tmpl.Execute(&label_out, metric)
49 p.Legend.Add(label_out.String(), l)
50 }
51 }
52 @@ -274,10 +261,10 @@ func registerExtension(router chi.Router, extension string, mime string) {
53 nextColor = 0
54 }
55 plotters[i] = l
56- if label != "" && len(res.Metric) > 2 && res.Metric[0] == '{' && res.Metric[len(res.Metric)-1] == '}' {
57+ if label != "" {
58 handleLabel(p, l, label, res.Metric)
59 } else {
60- p.Legend.Add(res.Metric, l)
61+ p.Legend.Add(metricName(res.Metric), l)
62 }
63 }
64 for i := len(plotters) - 1; i >= 0; i-- {