Skip to content

Commit 9895441

Browse files
authored
Merge pull request #4 from KIBruh/fix/expand-alias
fix: execute template in expandAlias for -a flag
2 parents 7975b0b + bf23ac6 commit 9895441

4 files changed

Lines changed: 85 additions & 4 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ build-darwin-aarch64: vendor
9292
done
9393

9494
test: vendor
95-
$(GO) test -short -v $(TEST_FLAGS) ./pkg/* ./cmd/*
95+
$(GO) test -short -v $(TEST_FLAGS) ./pkg/* ./cmd/* ./internal/mode/
9696
if grep -Irn TODO: ./cmd/ ./pkg/; then exit 1; fi
9797

9898
# test with filter

internal/mode/query.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func Query(ctx context.Context, address *url.URL, query, warning, critical, alia
124124
}
125125

126126
func expandAlias(alias string, labels model.Metric, value float64) string {
127-
_, err := template.New("Output").Parse(alias)
127+
tmpl, err := template.New("Output").Parse(alias)
128128
var output string
129129
if err != nil {
130130
output = alias
@@ -137,7 +137,11 @@ func expandAlias(alias string, labels model.Metric, value float64) string {
137137
}
138138
labelMap["xvalue"] = fmt.Sprintf("%v", value)
139139
var rendered bytes.Buffer
140-
output = rendered.String()
140+
if err := tmpl.Execute(&rendered, labelMap); err != nil {
141+
output = alias
142+
} else {
143+
output = rendered.String()
144+
}
141145
}
142146

143147
return output

internal/mode/query_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package mode
2+
3+
import (
4+
"testing"
5+
6+
"github.com/prometheus/common/model"
7+
)
8+
9+
func TestExpandAlias(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
alias string
13+
labels model.Metric
14+
value float64
15+
expected string
16+
}{
17+
{
18+
name: "simple template with label",
19+
alias: "Hostname: {{.hostname}}",
20+
labels: model.Metric{"hostname": "server01"},
21+
value: 42.0,
22+
expected: "Hostname: server01",
23+
},
24+
{
25+
name: "template with multiple labels",
26+
alias: "Host: {{.hostname}} Job: {{.job}}",
27+
labels: model.Metric{"hostname": "server01", "job": "prometheus"},
28+
value: 1.0,
29+
expected: "Host: server01 Job: prometheus",
30+
},
31+
{
32+
name: "template with xvalue",
33+
alias: "Value: {{.xvalue}}",
34+
labels: model.Metric{"hostname": "server01"},
35+
value: 123.45,
36+
expected: "Value: 123.45",
37+
},
38+
{
39+
name: "template with if-else condition",
40+
alias: "Status: {{if eq .xvalue \"1\"}}UP{{else}}DOWN{{end}}",
41+
labels: model.Metric{"hostname": "server01"},
42+
value: 1.0,
43+
expected: "Status: UP",
44+
},
45+
{
46+
name: "template with if-else condition false",
47+
alias: "Status: {{if eq .xvalue \"1\"}}UP{{else}}DOWN{{end}}",
48+
labels: model.Metric{"hostname": "server01"},
49+
value: 0.0,
50+
expected: "Status: DOWN",
51+
},
52+
{
53+
name: "invalid template fallback",
54+
alias: "{{invalid template",
55+
labels: model.Metric{"hostname": "server01"},
56+
value: 42.0,
57+
expected: "{{invalid template",
58+
},
59+
{
60+
name: "empty alias",
61+
alias: "",
62+
labels: model.Metric{"hostname": "server01"},
63+
value: 42.0,
64+
expected: "",
65+
},
66+
}
67+
68+
for _, tt := range tests {
69+
t.Run(tt.name, func(t *testing.T) {
70+
result := expandAlias(tt.alias, tt.labels, tt.value)
71+
if result != tt.expected {
72+
t.Errorf("expandAlias(%q, ...) = %q, want %q", tt.alias, result, tt.expected)
73+
}
74+
})
75+
}
76+
}
77+

pkg/checker/checker.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func Check(args []string) (check_x.State, string, *check_x.PerformanceDataCollec
7070
cmd := &cli.Command{
7171
Name: "check_prometheus",
7272
Usage: "Checks different prometheus stats as well the data itself",
73-
Version: "0.0.5",
73+
Version: "0.0.6",
7474
Flags: []cli.Flag{
7575
&cli.Int64Flag{
7676
Name: "timeout",

0 commit comments

Comments
 (0)