promql: Fix annotations conflated with labels
When converting `AlertStmt` to a string, the alert rule labels were printed as `ANNOTATIONS` instead of the annotations themselves. Fix and add a test to catch future regressions.
This commit is contained in:
parent
33759dddd2
commit
cc98e164d3
|
@ -109,7 +109,7 @@ func (node *AlertStmt) String() string {
|
|||
s += fmt.Sprintf("\n\tLABELS %s", node.Labels)
|
||||
}
|
||||
if len(node.Annotations) > 0 {
|
||||
s += fmt.Sprintf("\n\tANNOTATIONS %s", node.Labels)
|
||||
s += fmt.Sprintf("\n\tANNOTATIONS %s", node.Annotations)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
|
|
@ -15,8 +15,43 @@ package promql
|
|||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/prometheus/common/model"
|
||||
"github.com/prometheus/prometheus/storage/metric"
|
||||
)
|
||||
|
||||
func TestStatementString(t *testing.T) {
|
||||
in := &AlertStmt{
|
||||
Name: "FooAlert",
|
||||
Expr: &BinaryExpr{
|
||||
Op: itemGTR,
|
||||
LHS: &VectorSelector{
|
||||
Name: "foo",
|
||||
LabelMatchers: metric.LabelMatchers{
|
||||
{Type: metric.Equal, Name: model.MetricNameLabel, Value: "bar"},
|
||||
},
|
||||
},
|
||||
RHS: &NumberLiteral{10},
|
||||
},
|
||||
Duration: 5 * time.Minute,
|
||||
Labels: model.LabelSet{"foo": "bar"},
|
||||
Annotations: model.LabelSet{
|
||||
"notify": "team-a",
|
||||
},
|
||||
}
|
||||
|
||||
expected := `ALERT FooAlert
|
||||
IF foo > 10
|
||||
FOR 5m
|
||||
LABELS {foo="bar"}
|
||||
ANNOTATIONS {notify="team-a"}`
|
||||
|
||||
if in.String() != expected {
|
||||
t.Fatalf("expected:\n%s\ngot:\n%s\n", expected, in.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestExprString(t *testing.T) {
|
||||
// A list of valid expressions that are expected to be
|
||||
// returned as out when calling String(). If out is empty the output
|
||||
|
|
Loading…
Reference in New Issue