Skip to content

Introduce a regex tenant resolver #6713

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

Merged

Conversation

SungJin1212
Copy link
Member

@SungJin1212 SungJin1212 commented Apr 22, 2025

This PR introduces a regex tenant resolver to allow regex in the X-Scope-OrgID value when the user uses tenant-federation feature.
It introduces two flags, tenant-federation.regex-matcher-enabled and tenant-federation.user-sync-interval.

  • The tenant-federation.regex-matcher-enabled enables the regex resolver, which allows regex to the X-Scope-OrgID value.
  • The tenant-federation.user-sync-interval specifies how frequently to scan users. The scanned users are used to calculate matched tenantIDs.

The regex matching rule follows the Prometheus regex matcher (=~), See here.

For example, if there are 3 tenants, whose IDs are user-1, user-2, and user-3. We can set X-Scope-OrgID to user-.+ to query whole tenants.
Also, we can use an existing way like setting user-1|user-2|user-3 to X-Scope-OrgID.

It reuses userScanner to find considered tenant IDs. So, only tenants who uploaded blocks are subject to regex resolution.

Which issue(s) this PR fixes:
Fixes #6588

Checklist

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

@SungJin1212 SungJin1212 marked this pull request as draft April 22, 2025 01:29
@SungJin1212 SungJin1212 force-pushed the Support-regex-to-tenant-federation branch 8 times, most recently from 1b8963e to e6a4222 Compare April 22, 2025 06:02
@SungJin1212 SungJin1212 marked this pull request as ready for review April 22, 2025 06:27
@SungJin1212
Copy link
Member Author

@CharlieTLe
Could you take a look when you have time?

@SungJin1212 SungJin1212 force-pushed the Support-regex-to-tenant-federation branch from e6a4222 to 63f97d2 Compare April 24, 2025 06:46
Copy link
Member

@CharlieTLe CharlieTLe left a comment

Choose a reason for hiding this comment

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

This looks awesome!

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label May 3, 2025
@SungJin1212 SungJin1212 force-pushed the Support-regex-to-tenant-federation branch from 63f97d2 to 605f91a Compare May 7, 2025 02:31
@SungJin1212 SungJin1212 force-pushed the Support-regex-to-tenant-federation branch from 605f91a to d1b80e0 Compare May 15, 2025 09:00
@SungJin1212
Copy link
Member Author

@CharlieTLe
I changed not to attach __tenant_id__ label when the number of matched tenants is one (same behavior when the user uses the multi-resolver).

@SungJin1212 SungJin1212 force-pushed the Support-regex-to-tenant-federation branch 2 times, most recently from b7ef458 to 7321e4b Compare June 12, 2025 12:02
@SungJin1212 SungJin1212 force-pushed the Support-regex-to-tenant-federation branch 6 times, most recently from eaeb19e to 1135b06 Compare June 13, 2025 02:27
CHANGELOG.md Outdated
@@ -11,6 +11,9 @@
* [FEATURE] Ingester: Support out-of-order native histogram ingestion. It automatically enabled when `-ingester.out-of-order-time-window > 0` and `-blocks-storage.tsdb.enable-native-histograms=true`. #6626 #6663
* [FEATURE] Ruler: Add support for percentage based sharding for rulers. #6680
* [FEATURE] Ruler: Add support for group labels. #6665
* [FEATURE] Query federation: Introduce a regex tenant resolver to allow regex in `X-Scope-OrgID` value. #6713
- Add a `tenant-federation.regex-matcher-enabled` flag. If it enabled, user can input regex to `X-Scope-OrgId`, the matched tenantIDs are automatically involved.
- Add a `tenant-federation.user-sync-interval` flag, it specifies how frequently to scan users. The scanned users are used to calculate matched tenantIDs.
Copy link
Contributor

Choose a reason for hiding this comment

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

We should document it as experimental feature in the doc

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe we should document that the user discovery is based on scanning block storage so new users are only available every 2h (assuming blocks are only uploaded every 2h).

@SungJin1212 SungJin1212 force-pushed the Support-regex-to-tenant-federation branch from 1135b06 to 1a3d2f1 Compare June 13, 2025 11:50
@SungJin1212 SungJin1212 force-pushed the Support-regex-to-tenant-federation branch from 1a3d2f1 to 8737cf6 Compare June 13, 2025 12:07
@SungJin1212 SungJin1212 requested a review from yeya24 June 15, 2025 21:37
@SungJin1212
Copy link
Member Author

SungJin1212 commented Jun 16, 2025

@yeya24
I updated the PR. Can you take a look?

Copy link
Contributor

@yeya24 yeya24 left a comment

Choose a reason for hiding this comment

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

Thanks. I think this LGTM


_, err = labels.NewFastRegexMatcher(id)
if err != nil {
return "", errInvalidRegex
Copy link
Contributor

Choose a reason for hiding this comment

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

Just want to make sure, this error will be a 400?

Copy link
Member Author

Choose a reason for hiding this comment

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

All tenant validation errors are 500 now, maybe we should change the response code to 400.
The current responses are:

{
    "status": "error",
    "errorType": "server_error",
    "error": "expanding series: rpc error: code = Unknown desc = tenant ID is '.' or '..'"
}
{
    "status": "error",
    "errorType": "server_error",
    "error": "expanding series: invalid regex present"
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Umm I think this needs to be fixed. We can do it in next PR

@@ -497,6 +518,11 @@ func (t *Cortex) initQueryFrontendTripperware() (serv services.Service, err erro
shardedPrometheusCodec := queryrange.NewPrometheusCodec(true, t.Cfg.Querier.ResponseCompression, t.Cfg.API.QuerierDefaultCodec)
instantQueryCodec := instantquery.NewInstantQueryCodec(t.Cfg.Querier.ResponseCompression, t.Cfg.API.QuerierDefaultCodec)

if t.Cfg.TenantFederation.Enabled && t.Cfg.TenantFederation.RegexMatcherEnabled {
// If regex matcher enabled, we use regex validator to pass regex to the querier
tenant.WithDefaultResolver(tenantfederation.NewRegexValidator())
Copy link
Contributor

Choose a reason for hiding this comment

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

So the difference of regex validator and regex resolver are:

  • regex validator allows regex in tenant ID and it passes it as is
  • regex resolver is used in Querier and it is able to resolve the regex to tenant IDs?

Copy link
Member Author

Choose a reason for hiding this comment

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

yes, exactly

@yeya24 yeya24 merged commit bf88f67 into cortexproject:master Jun 22, 2025
31 of 32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/querier lgtm This PR has been approved by a maintainer size/XXL
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow for dynamic tenant selection in query federation
3 participants