Add label-matcher support to Rules API#10194
Conversation
|
While it makes sense to filter active alerts, it does not make sense to filter alert and rules definition as the label value is a templated string. I am against adding this feature. |
|
I have also commented on the issue. |
bwplotka
left a comment
There was a problem hiding this comment.
Thanks @saswatamcode for this PR and @roidelapluie for review.
Let's start with Rules API first as this is the API that matters the most.
And as per your point @roidelapluie I see this being weird from the first glance, but what if we provide the most straightforward mechanism that will be expected by everyone. Rules API is all about loaded configured, so to me we should filter by non-rendered, configured labels. This is also how it is implemented right now in this PR. I proposed a doc entry. What's wrong with that @roidelapluie 🤔
|
What is wrong is the semantic. I'd rather have match[] only match labels, and not templates in one API and label values in another API. I'd be okay if we would make this available in a different way, but not via a "promql-like" selector, because it does not make sense to apply regex on golang templates like if they were metrics, it's mixing apples and oranges. |
|
As @bwplotka suggested then, maybe we can keep it in the simplest form and match only non-templated labels in rules (with a change in docs to highlight this)? That way match[] doesn't behave differently from other APIs and we don't apply regex on templates. So match[] in |
|
I trust Bartek's judgment, let's document this properly. |
|
I have updated this PR with a doc change. Now only Also, test failure seems unrelated, |
|
Is there anything else that seems to be missing or is blocking this PR, @roidelapluie? Would love to progress on this! 🙂 |
|
Regarding to only match non-templated labels in rules. From user usage perspective, i should be able to filter based on what i configured? For example as in the test cases, if my rule have a label |
168395b to
15f8133
Compare
|
Hmm, not sure why the test/build fails here...maybe due to stringlabels? |
I agree with this. While I understand @roidelapluie view about the semantic of For
I am against this because it's not intuitive. Sure we can document it, but I think it's simpler for the the suer to understand "the filter works against template string" than "if the value is a template string then ... otherwise ...". Math only non-templated labels seems like an inconsistent behaviour to me. |
|
Okay. |
Love the conciseness :D |
The error log says: It was never a great idea to assume You could use code like this: prometheus/model/labels/test_utils.go Lines 34 to 41 in 617bee6 |
|
Thanks @bboreham! @alvinlin123 / @roidelapluie let me know, if we should remove the templated labels check here then. |
|
Yes please |
|
Done! |
rules/manager.go
Outdated
| var ok bool | ||
| for _, matchers := range matcherSets { | ||
| if matches(lbls, matchers...) { | ||
| ok = true |
There was a problem hiding this comment.
we should do the opposite. I expect that ALL matchers should match
There was a problem hiding this comment.
the other query parameters for example
rule_name[], rule_group[], and file[] are doing
"If the parameter is repeated, rules with any of the provided rule group names are returned." should we follow this behavior as well for the new match parameter?
Reference:
https://github.com/prometheus/prometheus/pull/12270/files
There was a problem hiding this comment.
My expectation is that all the Matchers in each []*labels.Matcher will be ANDed, and then the set of those will be ORed. I think this is what we do elsewhere.
It could be made clearer in comments.
There was a problem hiding this comment.
Yup, I think this is what we are doing here. matches ANDs all the matchers in each set i.e return false if any of them don't match, and then matchesMatcherSet takes the set of those results and ORs them i.e returns true if any of them are true. Added a comment for this.
rules/manager.go
Outdated
| return true | ||
| } | ||
|
|
||
| var ok bool |
8de11dd to
d31e47a
Compare
|
@roidelapluie could you please take a look at this pr again? Thanks |
There was a problem hiding this comment.
Code looks broadly fine; I made some small notes in places.
A major use case for this is multi-tenant applications built on Prometheus
Having worked on Cortex and Mimir, I don't understand this part. Both of them take a tenant ID in the request, and return rules only for that tenant.
Can you plese update the description to match what you want to get merged?
rules/manager.go
Outdated
| var ok bool | ||
| for _, matchers := range matcherSets { | ||
| if matches(lbls, matchers...) { | ||
| ok = true |
There was a problem hiding this comment.
My expectation is that all the Matchers in each []*labels.Matcher will be ANDed, and then the set of those will be ORed. I think this is what we do elsewhere.
It could be made clearer in comments.
Hi @bboreham! Long time no "see"! Yea initially I found this use case head scratching too. But I think the original author is implementing some kind of "poor man's multi-tenancy" on top of just Prometheus using just labelling convention for metrics and rules. |
That's correct, this is what the PR aims to do! We already kind of have this in Thanos, and can just use a proxy in front to ensure rules with a particular tenant label get returned, without having to maintain the notion of tenancy internally. This isn't a new pattern I guess and can extend beyond tenancy. Even projects like prom-label-proxy need to modify rules response and filter currently, when just setting a
The original description would still be valid I think, but I'm happy to change it to something more suitable. Open to any suggestions cc: @qinxx108 🙂 |
I say this statement is not true, given Cortex etc. manage to do it:
|
|
(During Prometheus bug scrub meeting) Just to make sure it's clear: Sounds like maintainers would accept such a change, looks like there are two use cases (workable UI and label-based multi-tenancy). |
|
Thanks! Have updated it to:
|
cfba634 to
13665b3
Compare
|
@saswatamcode @roidelapluie @bboreham I've addressed the comments. Please take another look if you have time! Ty! |
|
Hi Prometheus maintainers @bboreham @roidelapluie @bwplotka, it looks like there is a concensus to accept this change. It would be great help if any of the maintainer could take a look. Thanks! |
|
(https://cloud-native.slack.com/archives/C01AUBA4PFE/p1720546903110739) discussion why it's not merged. TL;DR: Comments not adresseed (plus now needs rebase). But generally looks good, just let's agree on implementation and defaults. |
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Yijie Qin <qinyijie@amazon.com>
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Yijie Qin <qinyijie@amazon.com>
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Yijie Qin <qinyijie@amazon.com>
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Yijie Qin <qinyijie@amazon.com>
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Yijie Qin <qinyijie@amazon.com>
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Yijie Qin <qinyijie@amazon.com>
Signed-off-by: Yijie Qin <qinyijie@amazon.com>
Signed-off-by: Yijie Qin <qinyijie@amazon.com>
13665b3 to
e313ab1
Compare
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
e313ab1 to
398b17f
Compare
* Add label-matcher support to Rules API Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Yijie Qin <qinyijie@amazon.com> * Implement suggestions Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Yijie Qin <qinyijie@amazon.com> * Match any matcherSet instead of all Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Yijie Qin <qinyijie@amazon.com> * Don't treat labels.Labels as slice Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Yijie Qin <qinyijie@amazon.com> * Remove non-templated check and fix tests Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Yijie Qin <qinyijie@amazon.com> * Update docs Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Yijie Qin <qinyijie@amazon.com> * fix comments Signed-off-by: Yijie Qin <qinyijie@amazon.com> * fix comment Signed-off-by: Yijie Qin <qinyijie@amazon.com> * Add comment for matching logic, fix tests after rebase Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> --------- Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Yijie Qin <qinyijie@amazon.com> Co-authored-by: Yijie Qin <qinyijie@amazon.com> Signed-off-by: kushagra Shukla <kushalshukla110@gmail.com>
* Add label-matcher support to Rules API Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Yijie Qin <qinyijie@amazon.com> * Implement suggestions Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Yijie Qin <qinyijie@amazon.com> * Match any matcherSet instead of all Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Yijie Qin <qinyijie@amazon.com> * Don't treat labels.Labels as slice Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Yijie Qin <qinyijie@amazon.com> * Remove non-templated check and fix tests Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Yijie Qin <qinyijie@amazon.com> * Update docs Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Yijie Qin <qinyijie@amazon.com> * fix comments Signed-off-by: Yijie Qin <qinyijie@amazon.com> * fix comment Signed-off-by: Yijie Qin <qinyijie@amazon.com> * Add comment for matching logic, fix tests after rebase Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> --------- Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Yijie Qin <qinyijie@amazon.com> Co-authored-by: Yijie Qin <qinyijie@amazon.com>
This PR adds support for label matchers as URL query params to the
api/v1/rulesendpoint of Prometheus API. This will not break existing functionality.It filters both recording and alerting rules via their labels (both templated and non-templated).
Open question on whether active alerts should be handled differently?
A major use case for this is multi-tenant applications built on Prometheus which require a way to isolate tenant rules. Currently, one possible way of doing this is by filtering the API response. But, for endpoints like
api/v1/labels, only a matcher needs to be added to URL params. So this PR aims to bring the same convenience to rules.Fixes #9627.