Skip to content

Commit

Permalink
Fix label_replace handling in alerts/template
Browse files Browse the repository at this point in the history
  • Loading branch information
prymitive committed Mar 14, 2023
1 parent bcabc8f commit 7850599
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
7 changes: 7 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## v0.42.2

### Fixed

- `alerts/template` check didn't correctly handle `label_replace()` calls
in queries - #568.

## v0.42.1

### Fixed
Expand Down
27 changes: 23 additions & 4 deletions internal/checks/alerts_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ func (c TemplateCheck) Check(ctx context.Context, path string, rule parser.Rule,
safeLabels = append(safeLabels, be.VectorMatching.Include...)
}
}
for _, cl := range calls(rule.AlertingRule.Expr.Query, "label_replace") {
for i, v := range cl.Args {
if i == 1 {
if s, ok := v.(*promParser.StringLiteral); ok {
safeLabels = append(safeLabels, s.Val)
}
break
}
}
}

data := promTemplate.AlertTemplateData(map[string]string{}, map[string]string{}, "", 0)

Expand Down Expand Up @@ -477,11 +487,8 @@ func checkMetricLabels(msg, name, text string, metricLabels []string, excludeLab
found = true
}
}
if found && slices.Contains(safeLabels, v[1]) {
found = !excludeLabels
}
if found == excludeLabels {
if _, ok := done[v[1]]; !ok {
if _, ok := done[v[1]]; !ok && !slices.Contains(safeLabels, v[1]) {
msgs = append(msgs, fmt.Sprintf(msg, v[1]))
done[v[1]] = struct{}{}
}
Expand Down Expand Up @@ -539,3 +546,15 @@ func binaryExprs(node *parser.PromQLNode) (be []*promParser.BinaryExpr) {

return be
}

func calls(node *parser.PromQLNode, name string) (cl []*promParser.Call) {
if n, ok := node.Node.(*promParser.Call); ok && n.Func.Name == name {
cl = append(cl, n)
}

for _, child := range node.Children {
cl = append(cl, calls(child, name)...)
}

return cl
}
16 changes: 16 additions & 0 deletions internal/checks/alerts_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,22 @@ func TestTemplateCheck(t *testing.T) {
}
},
},
{
description: "don't trigger for label_replace() provided labels",
content: `
- alert: label_replace_not_checked_correctly
expr: |
label_replace(
sum by (pod) (pod_status) > 0
,"cluster", "$1", "pod", "(.*)"
)
annotations:
summary: "Some error found in {{ $labels.cluster }}"
`,
checker: newTemplateCheck,
prometheus: noProm,
problems: noProblems,
},
{
description: "annotation label present on metrics (absent)",
content: `
Expand Down

0 comments on commit 7850599

Please # to comment.