Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve LabelSource parsing #1181

Merged
merged 3 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 7 additions & 37 deletions internal/checks/alerts_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,6 @@ import (
const (
TemplateCheckName = "alerts/template"
TemplateCheckSyntaxDetails = `Supported template syntax is documented [here](https://prometheus.io/docs/prometheus/latest/configuration/alerting_rules/#templating).`
TemplateCheckAbsentDetails = `The [absent()](https://prometheus.io/docs/prometheus/latest/querying/functions/#absent) function is used to check if provided query doesn't match any time series.
You will only get any results back if the metric selector you pass doesn't match anything.
Since there are no matching time series there are also no labels. If some time series is missing you cannot read its labels.
This means that the only labels you can get back from absent call are the ones you pass to it.
If you're hoping to get instance specific labels this way and alert when some target is down then that won't work, use the ` + "`up`" + ` metric instead.`
TemplateCheckLabelsDetails = `This query doesn't seem to be using any time series and so cannot have any labels.`
)

var (
Expand Down Expand Up @@ -464,37 +458,13 @@ func checkQueryLabels(query, labelName, labelValue string, src []utils.Source) (
}

func textForProblem(query, label, reasonLabel string, src utils.Source, severity Severity) exprProblem {
switch {
case src.Operation == "absent":
return exprProblem{
text: fmt.Sprintf("Template is using `%s` label but `%s` is not passing it.", label, src.Call.String()),
details: TemplateCheckAbsentDetails,
severity: severity,
}
case src.Operation == "vector":
return exprProblem{
text: fmt.Sprintf("Template is using `%s` label but `%s` doesn't produce any labels.", label, src.Call.String()),
details: TemplateCheckLabelsDetails,
severity: severity,
}
case src.Returns == promParser.ValueTypeScalar, src.Returns == promParser.ValueTypeString:
return exprProblem{
text: fmt.Sprintf("Template is using `%s` label but the query doesn't produce any labels.", label),
details: TemplateCheckLabelsDetails,
severity: severity,
}
default:
return exprProblem{
text: fmt.Sprintf("Template is using `%s` label but the query results won't have this label.", label),
details: maybeAddQueryFragment(query, src.ExcludeReason[reasonLabel].Fragment, src.ExcludeReason[reasonLabel].Reason),
severity: severity,
}
details := src.ExcludeReason[reasonLabel].Reason
if query != src.ExcludeReason[reasonLabel].Fragment {
details = fmt.Sprintf("%s\nQuery fragment causing this problem: `%s`.", details, src.ExcludeReason[reasonLabel].Fragment)
}
}

func maybeAddQueryFragment(query, fragment, msg string) string {
if fragment == query {
return msg
return exprProblem{
text: fmt.Sprintf("Template is using `%s` label but the query results won't have this label.", label),
details: details,
severity: severity,
}
return fmt.Sprintf("%s\nQuery fragment causing this problem: `%s`.", msg, fragment)
}
Loading