-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Add matcher support to Query Rules endpoint #5111
Conversation
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
pkg/rules/rules.go
Outdated
} | ||
|
||
for _, g := range ruleGroups { | ||
filteredRules := []*rulespb.Rule{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A small advice: use filteredRules := g.Rules[:0]
avoid allocate.
https://github.com/golang/go/wiki/SliceTricks#filtering-without-allocating
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
CHANGELOG.md
Outdated
@@ -24,6 +24,7 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re | |||
- [#4974](https://github.com/thanos-io/thanos/pull/4974) Store: Support tls_config configuration for connecting with Azure storage. | |||
- [#4999](https://github.com/thanos-io/thanos/pull/4999) COS: Support `endpoint` configuration for vpc internal endpoint. | |||
- [#5059](https://github.com/thanos-io/thanos/pull/5059) Compactor: Adding minimum retention flag validation for downsampling retention. | |||
- [#5111](https://github.com/thanos-io/thanos/pull/5111) Add matcher support to Query Rules endpoint |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A small nit: add dot at the line end.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! Also, have added tests! 🙂
It will be better to add some unit tests (: |
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
Added tests pass here! |
pkg/rules/rulespb/rpc.proto
Outdated
@@ -40,6 +40,7 @@ message RulesRequest { | |||
} | |||
Type type = 1; | |||
PartialResponseStrategy partial_response_strategy = 2; | |||
repeated string MatcherString = 3; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we keep this field name snake_case
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, let me change!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
pkg/rules/rules.go
Outdated
for _, s := range req.MatcherString { | ||
matchers, err := parser.ParseMetricSelector(s) | ||
if err != nil { | ||
return nil, nil, errors.Wrap(err, "proxy Rules") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Should make the error description just a tiny bit clearer?
return nil, nil, errors.Wrap(err, "proxy Rules") | |
return nil, nil, errors.Wrap(err, "parser ParseMetricSelector") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah! Nice catch, will change this!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
pkg/rules/rules.go
Outdated
@@ -58,6 +61,16 @@ func (rr *GRPCClient) Rules(ctx context.Context, req *rulespb.RulesRequest) (*ru | |||
return nil, nil, errors.Wrap(err, "proxy Rules") | |||
} | |||
|
|||
var matcherSets [][]*labels.Matcher |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we pre-allocate here? We know the final slice's length, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, will pre-allocate!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice job @saswatamcode!
Re regex approach, to me doing this node check seems more straightforward (and possibly also better performance-wise).
I'm wondering how long before this is supported in the downstream in Prometheus, since the linked PR seem to be well in progress, but I guess this is a good solution for the meantime 👍.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cool! @saswatamcode 🎉
@@ -58,6 +61,16 @@ func (rr *GRPCClient) Rules(ctx context.Context, req *rulespb.RulesRequest) (*ru | |||
return nil, nil, errors.Wrap(err, "proxy Rules") | |||
} | |||
|
|||
var err error | |||
matcherSets := make([][]*labels.Matcher, len(req.MatcherString)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we check if len(req.MatcherString) > 0
or this is already covered from here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the length is zero, matcherSets
would be an empty array of type [][]*labels.Matcher
and the loop below this won't run. filterRules
gets called right after and it checks the length of matcherSets
here and if it is 0, returns all the rules. So we kind of do check for the length of MatcherString
, just not directly. 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sounds good!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Neat work! I have a couple of non-blocking comments, but this looks ready to merge. 🚀
Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Great work!
* Add matcher support to Query Rules endpoint Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> * Add CHANGELOG entry Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> * Add tests and implement suggestions Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> * Pre-allocate matcherSets Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> * Add other matchtype testcase Signed-off-by: Saswata Mukherjee <saswataminsta@yahoo.com> Signed-off-by: Nicholaswang <wzhever@gmail.com>
This PR adds support for label matchers as URL query params to the
api/v1/rules
endpoint of Thanos Query API. This will not break existing functionality.It filters both recording and alerting rules via their non-templated labels and doesn't take the rendered labels of active alerts into account.
Currently, this PR only implements this functionality in Thanos, but this can be changed to a Prometheus-based implementation in the future (relevant PR: prometheus#10194)!
Addresses #4812.
Open question: Templated labels are identified by parsing the label values as templates and checking the number and type of node. So if it is parsed as a single node of type
parser.NodeText
it is treated as a non-templated label. Would a regex-based approach be better suited here?Changes
match[]
URL param inapi/v1/rules
of Query API.MatcherString
toRulesRequest
in rules protoVerification
Tested locally.