Skip to content

Commit f3fced0

Browse files
committed
templated legend
1 parent 720dba8 commit f3fced0

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

pkg/hander.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package pkg
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/json"
67
"fmt"
@@ -40,7 +41,15 @@ func NewPNG(promAddr string, queryRangePath string, defaultTimeout time.Duration
4041
}
4142
}
4243

43-
func formatLegend(nameMap map[string]string) string {
44+
func formatLegend(nameMap map[string]string, tpl *template.Template) string {
45+
if tpl != nil {
46+
var b bytes.Buffer
47+
err := tpl.Execute(&b, nameMap)
48+
if err != nil {
49+
return err.Error()
50+
}
51+
return b.String()
52+
}
4453
kv := make([]string, 0, len(nameMap))
4554
for k, v := range nameMap {
4655
if k == "__name__" {
@@ -191,7 +200,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
191200
}
192201
md := &types.MetricData{
193202
FetchResponse: pb.FetchResponse{
194-
Name: formatLegend(r.Metric),
203+
Name: formatLegend(r.Metric, graphData.Template),
195204
StartTime: r.Values[0].Timestamp,
196205
StopTime: r.Values[len(r.Values)-1].Timestamp,
197206
StepTime: step,

0 commit comments

Comments
 (0)