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

Implement RequiredNumberLabels query limit #8918

Merged
merged 8 commits into from
Mar 29, 2023

Conversation

salvacorts
Copy link
Contributor

@salvacorts salvacorts commented Mar 28, 2023

What this PR does / why we need it:
As pointed out in #8851, some queries can impose a great workload on a cluster by selecting too many streams.

Similarly to the RequiredLabels limit introduced at #8851, here we add a new limit RequiredNumberLabels to require queries to specify at least N label. For example, if the limit is set to 2, then the query should contain at least 2 label matchers.

This limit can be configured per tenant and at query time.

image

Which issue(s) this PR fixes:
Fixes https://github.com/grafana/loki-private/issues/699

Special notes for your reviewer:

Checklist

  • Reviewed the CONTRIBUTING.md guide (required)
  • Documentation added
  • Tests updated
  • CHANGELOG.md updated
  • Changes that require user attention or interaction to upgrade are documented in docs/sources/upgrading/_index.md

@github-actions github-actions bot added the type/docs Issues related to technical documentation; the Docs Squad uses this label across many repositories label Mar 28, 2023
@salvacorts salvacorts marked this pull request as ready for review March 28, 2023 14:29
@salvacorts salvacorts requested review from JStickler and a team as code owners March 28, 2023 14:29
@salvacorts salvacorts changed the title Salvacorts/require n labels Implement RequiredNumberLabels limit Mar 28, 2023
@salvacorts salvacorts changed the title Implement RequiredNumberLabels limit Implement RequiredNumberLabels query limit Mar 28, 2023
Copy link
Contributor

@JStickler JStickler left a comment

Choose a reason for hiding this comment

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

[docs team] LGTM

Copy link
Member

@owen-d owen-d left a comment

Choose a reason for hiding this comment

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

one nit, but LGTM

original := l.CombinedLimits.RequiredNumberLabels(ctx, userID)
requestLimits := ExtractQueryLimitsContext(ctx)
if requestLimits == nil || requestLimits.RequiredNumberLabels == 0 || requestLimits.RequiredNumberLabels > original {
level.Debug(logutil.WithContext(ctx, l.logger)).Log("msg", "using original limit")
Copy link
Member

@owen-d owen-d Mar 28, 2023

Choose a reason for hiding this comment

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

This should log the tenant, original & requested limits we compared.
edit: I think all the other limits in this file should do the same, but I'm fine if that's all left for another PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed with doing that on a separate PR.

Copy link
Contributor

@DylanGuedes DylanGuedes left a comment

Choose a reason for hiding this comment

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

a few nits

CHANGELOG.md Outdated Show resolved Hide resolved
pkg/util/querylimits/limiter.go Show resolved Hide resolved
pkg/util/querylimits/limiter.go Outdated Show resolved Hide resolved
MaxEntriesLimitPerQuery int `json:"maxEntriesLimitPerQuery,omitempty"`
QueryTimeout model.Duration `json:"queryTimeout,omitempty"`
RequiredLabels []string `json:"requiredLabels,omitempty"`
RequiredNumberLabels int `json:"requiredNumberLabels,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

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

double checking: shouldn't the json here be "requiredNumberLabelMatchers"?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, agree with this as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

Copy link
Member

@owen-d owen-d left a comment

Choose a reason for hiding this comment

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

@DylanGuedes gave a great review, much better than mine!

@salvacorts
Copy link
Contributor Author

@DylanGuedes @owen-d thanks for the review! I believe I've addressed all the pending comments.
I've created #8932 to discuss/track the changes to the logging messages in this file.

Copy link
Contributor

@DylanGuedes DylanGuedes left a comment

Choose a reason for hiding this comment

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

one more nit and then LGTM!

pkg/util/querylimits/propagation.go Outdated Show resolved Hide resolved
Co-authored-by: Dylan Guedes <djmgguedes@gmail.com>
Copy link
Contributor

@DylanGuedes DylanGuedes left a comment

Choose a reason for hiding this comment

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

lgtm!

@salvacorts salvacorts merged commit 45775c8 into main Mar 29, 2023
@salvacorts salvacorts deleted the salvacorts/require_n_labels branch March 29, 2023 10:32
Copy link
Contributor

@jeschkies jeschkies left a comment

Choose a reason for hiding this comment

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

Looks good to me but o have a request for the naming.

@@ -119,13 +128,17 @@ func TestLimiter_RejectHighLimits(t *testing.T) {
MaxEntriesLimitPerQuery: 10,
QueryTimeout: model.Duration(30 * time.Second),
MaxQueryBytesRead: 10,
// In this case, the higher it is, the more restrictive.
// Therefore, we return the query-time limit as it's more restrictive than the original.
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this comment correct here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. See #8918 (comment)

MaxEntriesLimitPerQuery int `json:"maxEntriesLimitPerQuery,omitempty"`
QueryTimeout model.Duration `json:"queryTimeout,omitempty"`
RequiredLabels []string `json:"requiredLabels,omitempty"`
RequiredNumberLabels int `json:"requiredNumberLabelMatchers,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

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

I've discussed the banking with Steven. We should omit "Matchers" and use minimumLabelsCount or minimumLabelsNumner.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

salvacorts added a commit that referenced this pull request Mar 30, 2023
**What this PR does / why we need it**:
Followup PR for #8918 renaming
config. See https://github.com/grafana/loki/pull/8918/files#r1151820792.

**Which issue(s) this PR fixes**:
Fixes grafana/loki-private#699

**Special notes for your reviewer**:

**Checklist**
- [ ] Reviewed the
[`CONTRIBUTING.md`](https://github.com/grafana/loki/blob/main/CONTRIBUTING.md)
guide (**required**)
- [x] Documentation added
- [ ] Tests updated
- [ ] `CHANGELOG.md` updated
- [ ] Changes that require user attention or interaction to upgrade are
documented in `docs/sources/upgrading/_index.md`

---------

Co-authored-by: Dylan Guedes <djmgguedes@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/L type/docs Issues related to technical documentation; the Docs Squad uses this label across many repositories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants