feat(querier): add wildcard tenant query support#20690
feat(querier): add wildcard tenant query support#20690amogh-orolabs-dev wants to merge 1 commit intografana:mainfrom
Conversation
Add experimental support for wildcard tenant queries using "*" in the X-Scope-OrgID header. This allows administrators to query all tenants without explicitly listing each tenant ID. Features: - Support for "*" to query all tenants - Exclusion syntax: "*|!tenant1|!tenant2" to exclude specific tenants - Tenant discovery from index storage with configurable cache TTL - New config options: - querier.wildcard-tenant-queries-enabled (default: false) - querier.wildcard-tenant-cache-ttl (default: 5m) Closes grafana#7356
|
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 67afe0b524
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if q.discovery.IsWildcard(id) { | ||
| hasWildcard = true | ||
| } else if strings.HasPrefix(id, "!") { | ||
| // Tenant exclusion syntax: !tenant_id | ||
| excludeTenants[strings.TrimPrefix(id, "!")] = struct{}{} |
There was a problem hiding this comment.
Avoid treating valid tenant IDs starting with ! as exclusions
With wildcard queries enabled, any tenant ID that starts with ! is now parsed as an exclusion and triggers ErrExcludeWithoutWildcard unless * is present. Loki’s tenant validation allows ! in tenant IDs, so a tenant named like !prod would become unqueryable once this feature is turned on. That’s a regression for valid tenant IDs and means enabling wildcard queries can break existing tenants that start with !.
Useful? React with 👍 / 👎.
| // Use the active period config to determine the object store and index prefix | ||
| periodConfigs := t.Cfg.SchemaConfig.Configs | ||
| if len(periodConfigs) > 0 { | ||
| activePeriodIdx := config.ActivePeriodConfig(periodConfigs) | ||
| activePeriod := periodConfigs[activePeriodIdx] |
There was a problem hiding this comment.
Discover tenants from all schema configs, not just active
Tenant discovery is tied to only the active schema period’s object store and prefix. In deployments with multiple schema configs (e.g., a migration that changed index store or prefix), tenants that only have data in non-active periods won’t be discovered, so * queries will omit them even when the query time range includes their historical data. That violates the “all tenants” expectation for wildcard queries in multi-schema setups.
Useful? React with 👍 / 👎.
Summary
This PR adds experimental support for wildcard tenant queries in Loki, allowing administrators to query all tenants using
*in theX-Scope-OrgIDheader instead of explicitly listing each tenant ID.Closes #7356
Motivation
In multi-tenant Loki deployments with many tenants, administrators currently need to manually maintain lists of tenant IDs in their datasource configurations or query headers. This is error-prone and requires constant updates as tenants are added or removed.
This feature addresses the community request for a way to query "all tenants" or "all tenants except specific ones" without explicit enumeration.
Changes
New Files
pkg/querier/tenant_discovery.go- Core tenant discovery interface and storage-based implementationpkg/querier/wildcard_tenant_querier.go- Querier wrapper that handles wildcard resolutionpkg/querier/tenant_discovery_test.go- Unit tests for tenant discoverypkg/querier/wildcard_tenant_querier_test.go- Unit tests for wildcard querierintegration/wildcard_tenant_queries_test.go- Integration testsdocs/sources/configure/wildcard-tenant-queries.md- User documentationModified Files
pkg/querier/querier.go- Added configuration options:wildcard_tenant_queries_enabled(bool, default: false)wildcard_tenant_cache_ttl(duration, default: 5m)pkg/loki/modules.go- Wired wildcard querier into initializationDesign
Tenant Discovery
The feature uses a
TenantDiscoveryinterface:The
StorageTenantDiscoveryimplementation discovers tenants by listing them from the index storage (S3/GCS/etc). Results are cached with a configurable TTL to avoid expensive storage operations on every query.Query Flow
X-Scope-OrgID: *(or*|!excluded)WildcardTenantQuerier.resolveWildcardTenants()detects the wildcardStorageTenantDiscovery.GetAllTenants()retrieves all tenant IDs from storage (or cache)!tenantpatterns are presentMultiTenantQueriercode path__tenant_id__label on each log entrySupported Syntax
**|!tenant1*|!t1|!t2|!t3Configuration
Testing
Breaking Changes
None - this is an opt-in experimental feature.
Checklist