diff --git a/docs/changelog.md b/docs/changelog.md index d53deb21..13667b62 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -1,5 +1,11 @@ # Changelog +## v0.61.0 + +### Fixed + +- Don't suggest using `humanize` when alert template is already using printf on format the `$value`. + ## v0.60.0 ### Fixed diff --git a/internal/checks/alerts_template.go b/internal/checks/alerts_template.go index 420e6cc3..0b97f7aa 100644 --- a/internal/checks/alerts_template.go +++ b/internal/checks/alerts_template.go @@ -470,13 +470,10 @@ func hasHumanize(name, text string) bool { continue } if n, ok := node.(*parse.ActionNode); ok { - if len(n.Pipe.Cmds) <= 1 { - continue - } for _, cmd := range n.Pipe.Cmds { for _, arg := range cmd.Args { if m, ok := arg.(*parse.IdentifierNode); ok { - for _, f := range []string{"humanize", "humanize1024", "humanizePercentage", "humanizeDuration"} { + for _, f := range []string{"humanize", "humanize1024", "humanizePercentage", "humanizeDuration", "printf"} { for _, a := range aliases.varAliases(f) { if m.Ident == a { return true diff --git a/internal/checks/alerts_template_test.go b/internal/checks/alerts_template_test.go index f40d6c92..3925881b 100644 --- a/internal/checks/alerts_template_test.go +++ b/internal/checks/alerts_template_test.go @@ -870,6 +870,54 @@ func TestTemplateCheck(t *testing.T) { prometheus: noProm, problems: noProblems, }, + { + description: "humanize not needed on wjen using printf %.2f", + content: ` +- alert: Foo + expr: rate(errors_total[5m]) > 0 + annotations: + summary: Seeing {{ printf "%.2f" $value }} instances with errors +`, + checker: newTemplateCheck, + prometheus: noProm, + problems: noProblems, + }, + { + description: "humanize not needed on wjen using printf %f", + content: ` +- alert: Foo + expr: rate(errors_total[5m]) > 0 + annotations: + summary: Seeing {{ printf "%f" $value }} instances with errors +`, + checker: newTemplateCheck, + prometheus: noProm, + problems: noProblems, + }, + { + description: "humanize still needed for printf on another value", + content: ` +- alert: Foo + expr: rate(errors_total[5m]) > 0 + annotations: + summary: Seeing {{ printf "%f" 2 }}{{ $value }} instances with errors +`, + checker: newTemplateCheck, + prometheus: noProm, + problems: func(_ string) []checks.Problem { + return []checks.Problem{ + { + Lines: parser.LineRange{ + First: 5, + Last: 5, + }, + Reporter: checks.TemplateCheckName, + Text: humanizeText("rate(errors_total[5m])"), + Severity: checks.Information, + }, + } + }, + }, { description: "toTime", content: `