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

Ruler: Implement filtering for /api/v1/rules endpoint. #5291

Merged
merged 2 commits into from
Jun 21, 2023

Conversation

stevesg
Copy link
Contributor

@stevesg stevesg commented Jun 20, 2023

What this PR does

Adds support to the Ruler rule status endpoint (/prometheus/api/v1/rules) to filter results by file, rule_group and rule_name.

Note that quite a few unit tests had to be fixed up to cater for the rules endpoint not returning a rule group if there are no rules in it. This is not a breaking change from a user perspective, as a rule group with zero rules fails validation, we just have some unit tests that bypass the validation when configuring rules.

The implementation could be made more optimal with more work:

  • For the special case of a single file and single rule_group, we could restrict the set of replicas that are contacted. This would not necessarily make the API faster, but it would reduce load on replicas which we know do not own the rule group.
  • We still have to copy the rules out of Prometheus and sort them. We could pass the filters into the RuleGroups() function. I consider this an iterative improvement we can make if deemed necessary.
    • Additionally, because of how Prometheus stores rule groups in a map, we could make rule-group lookup even faster still.

Which issue(s) this PR fixes or relates to

Fixes #4792

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]

@stevesg stevesg force-pushed the stevesg/implement-rule-status-filters branch 3 times, most recently from 5f92d2f to 7dbdc9d Compare June 20, 2023 18:57
@stevesg stevesg marked this pull request as ready for review June 20, 2023 19:08
@stevesg stevesg requested review from a team as code owners June 20, 2023 19:08
Copy link
Contributor

@treid314 treid314 left a comment

Choose a reason for hiding this comment

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

This is great for users with a lot a lot of rules! Thank you!

Copy link
Collaborator

@pracucci pracucci left a comment

Choose a reason for hiding this comment

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

Nice job! Simple and clean.

pkg/ruler/ruler.go Outdated Show resolved Hide resolved
@stevesg stevesg force-pushed the stevesg/implement-rule-status-filters branch from 41c51a8 to 192defe Compare June 21, 2023 11:15
@stevesg stevesg merged commit aff210a into main Jun 21, 2023
@stevesg stevesg deleted the stevesg/implement-rule-status-filters branch June 21, 2023 11:33
ashwanthgoli added a commit to grafana/loki that referenced this pull request Feb 5, 2024
**What this PR does / why we need it**:
Adds `rule_name`, `rule_group`, `file` and `type` query parameters for
filtering the response of `/prometheus/api/v1/rules` endpoint.
Replicates mimir's functionality:
grafana/mimir#5291

- all of them are optional.
- `type` paremeter accepts either `alert` or `record`
- `rule_name`, `rule_group`, `file` can accept multiple values and they
filter the response accordingly.




There is a minor change in behavior: `/prometheus/api/v1/rules` endpoint
will no longer return empty rule groups which is inline with both
[prometheus](https://github.com/prometheus/prometheus/pull/12270/files#diff-315f251cdd7e93fcec1e7e9505744da1d1828f30d2b61d1f4ce963fa26bf1909R1403)
and
[mimir](https://github.com/grafana/mimir/pull/5291/files#diff-e5424c21c0e827bd1c9d3f669ed605897696bdc27993bc8bfd7113eba787b49dR1120).
This is not a breaking change since rule groups with [no rules fail
validation](https://github.com/grafana/loki/blob/27fbd62505f4412e3cb9180b1a5a66518bba9752/pkg/ruler/base/manager.go#L295)
and cannot be created.

**Which issue(s) this PR fixes**:
Fixes #9295

**Special notes for your reviewer**:

**Checklist**
- [x] Reviewed the
[`CONTRIBUTING.md`](https://github.com/grafana/loki/blob/main/CONTRIBUTING.md)
guide (**required**)
- [x] Documentation added
- [x] Tests updated
- [x] `CHANGELOG.md` updated
- [ ] If the change is worth mentioning in the release notes, add
`add-to-release-notes` label
- [ ] Changes that require user attention or interaction to upgrade are
documented in `docs/sources/setup/upgrade/_index.md`
- [ ] For Helm chart changes bump the Helm chart version in
`production/helm/loki/Chart.yaml` and update
`production/helm/loki/CHANGELOG.md` and
`production/helm/loki/README.md`. [Example
PR](d10549e)
- [ ] If the change is deprecating or removing a configuration option,
update the `deprecated-config.yaml` and `deleted-config.yaml` files
respectively in the `tools/deprecated-config-checker` directory.
[Example
PR](0d4416a)
rhnasc pushed a commit to inloco/loki that referenced this pull request Apr 12, 2024
**What this PR does / why we need it**:
Adds `rule_name`, `rule_group`, `file` and `type` query parameters for
filtering the response of `/prometheus/api/v1/rules` endpoint.
Replicates mimir's functionality:
grafana/mimir#5291

- all of them are optional.
- `type` paremeter accepts either `alert` or `record`
- `rule_name`, `rule_group`, `file` can accept multiple values and they
filter the response accordingly.




There is a minor change in behavior: `/prometheus/api/v1/rules` endpoint
will no longer return empty rule groups which is inline with both
[prometheus](https://github.com/prometheus/prometheus/pull/12270/files#diff-315f251cdd7e93fcec1e7e9505744da1d1828f30d2b61d1f4ce963fa26bf1909R1403)
and
[mimir](https://github.com/grafana/mimir/pull/5291/files#diff-e5424c21c0e827bd1c9d3f669ed605897696bdc27993bc8bfd7113eba787b49dR1120).
This is not a breaking change since rule groups with [no rules fail
validation](https://github.com/grafana/loki/blob/27fbd62505f4412e3cb9180b1a5a66518bba9752/pkg/ruler/base/manager.go#L295)
and cannot be created.

**Which issue(s) this PR fixes**:
Fixes grafana#9295

**Special notes for your reviewer**:

**Checklist**
- [x] Reviewed the
[`CONTRIBUTING.md`](https://github.com/grafana/loki/blob/main/CONTRIBUTING.md)
guide (**required**)
- [x] Documentation added
- [x] Tests updated
- [x] `CHANGELOG.md` updated
- [ ] If the change is worth mentioning in the release notes, add
`add-to-release-notes` label
- [ ] Changes that require user attention or interaction to upgrade are
documented in `docs/sources/setup/upgrade/_index.md`
- [ ] For Helm chart changes bump the Helm chart version in
`production/helm/loki/Chart.yaml` and update
`production/helm/loki/CHANGELOG.md` and
`production/helm/loki/README.md`. [Example
PR](grafana@d10549e)
- [ ] If the change is deprecating or removing a configuration option,
update the `deprecated-config.yaml` and `deleted-config.yaml` files
respectively in the `tools/deprecated-config-checker` directory.
[Example
PR](grafana@0d4416a)
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.

Implement filters for the Ruler api/v1/rules prometheus compatible API
3 participants