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

Support extra label selectors on mimir.rules.kubernetes #1773

Merged
merged 5 commits into from
Sep 30, 2024

Conversation

thampiotr
Copy link
Contributor

@thampiotr thampiotr commented Sep 26, 2024

PR Description

The goal is to allow mimir.rules.kubernetes to add extra matchers to queries, using the same code as the promtool does here.

Which issue(s) this PR fixes

Internal request.

Notes to the Reviewer

PR Checklist

  • CHANGELOG.md updated
  • Documentation added
  • Tests updated
  • Config converters updated

@thampiotr thampiotr changed the title support extra labels on mimir rules queries WIP: support extra labels on mimir rules queries Sep 26, 2024
Comment on lines +193 to +200
for i := range ruleGroup.Rules {
if ruleGroup.Rules[i].Labels == nil {
ruleGroup.Rules[i].Labels = make(map[string]string, len(e.externalLabels))
}
maps.Copy(ruleGroup.Rules[i].Labels, e.externalLabels)
}
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a rename of non-idiomatic rule_group variable here.

Comment on lines +202 to +210
if e.extraQueryMatchers != nil {
for _, ruleGroup := range groups {
for i := range ruleGroup.Rules {
query := ruleGroup.Rules[i].Expr.Value
newQuery, err := addMatchersToQuery(query, e.extraQueryMatchers.Matchers)
if err != nil {
level.Error(e.logger).Log("msg", "failed to add labels to PrometheusRule query", "query", query, "err", err)
}
maps.Copy(rule_group.Rules[i].Labels, e.externalLabels)
ruleGroup.Rules[i].Expr.Value = newQuery
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is new.


expectedRule := `- name: group1
rules:
- expr: "sum by (namespace) (rate(success{cluster=~\"prod-.*\",job=\"good\"}[10m]) / rate(total{cluster=~\"prod-.*\",job=\"good\"}[10m]))"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shows how for queries that refer multiple metrics, the extra matchers will be added to all metric selectors.

Comment on lines +51 to +72
extra_query_matchers {
matcher {
name = "job"
match_type = "!="
value = "bar"
}
matcher {
name = "namespace"
match_type = "="
value = "all"
}
matcher {
name = "namespace"
match_type = "!~"
value = ".+"
}
matcher {
name = "cluster"
match_type = "=~"
value = "prod-.*"
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example of user-facing config.

@thampiotr thampiotr changed the title WIP: support extra labels on mimir rules queries Support extra label selectors on mimir.rules.kubernetes Sep 27, 2024
@thampiotr thampiotr marked this pull request as ready for review September 27, 2024 16:21
Co-authored-by: Clayton Cornell <131809008+clayton-cornell@users.noreply.github.com>
@thampiotr thampiotr enabled auto-merge (squash) September 30, 2024 10:09
@thampiotr thampiotr merged commit 8f1be0e into main Sep 30, 2024
17 of 18 checks passed
@thampiotr thampiotr deleted the thampiotr/support-extra-labels-on-mimir-rule-queries branch September 30, 2024 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants