Skip to content

fix(metrics-generator): prevent WAL deletion when tenant is empty - #5586

Merged
joe-elliott merged 5 commits into
grafana:mainfrom
sienna011022:fix/metrics-generator-empty-tenant-wal
Sep 2, 2025
Merged

fix(metrics-generator): prevent WAL deletion when tenant is empty#5586
joe-elliott merged 5 commits into
grafana:mainfrom
sienna011022:fix/metrics-generator-empty-tenant-wal

Conversation

@sienna011022

@sienna011022 sienna011022 commented Aug 28, 2025

Copy link
Copy Markdown
Contributor

What this PR does:
Prevents metrics-generator from deleting the entire WAL root directory when an empty tenant is encountered. This issue could cause data loss for all tenants

Root Cause:
• Empty tenant ("") causes filepath.Join(cfg.Path, tenant) to resolve to the WAL root directory
• os.RemoveAll(walDir) then deletes the entire root, affecting all tenant WAL data
• This occurs when X-Scope-OrgID header is missing or internal metrics lack tenant labels

Solution:
• Throw Error when tenant is empty when multi tenancy environment enabled

Testing:
• Added TestInstance_emptyTenantValidation with 2 test cases
• All existing tests pass
• Verified WAL directory structure remains intact for existing tenants

Which issue(s) this PR fixes:
Fixes potential WAL data loss in multi-tenant environments (no existing issue number)

Checklist

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

CLAassistant commented Aug 28, 2025

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

// TODO move this to the generator.go

// Validate empty tenant to prevent WAL directory deletion
if tenant == "" {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

i like this fix b/c of how catastrophic the result is if it occurs, but technically this scenario should never happen.

wdyt about just returning an error instead of using "single-tenant"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@joe-elliott
I agree thx

Finally, I used RemoteWriteAddOrgIDHeader as the decision flag because it already maps directly to the deployment mode:

  • true (multi-tenant) → tenant ID is required for isolation, so we throw an error if it’s missing
  • false (single-tenant) → no isolation needed, so we allow empty tenant IDs to pass through

This way we don’t introduce new config knobs or break existing single-tenant flows.
The validation logic stays aligned with the original flag’s semantics, making the behavior both clear and consistent.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

RemoteWriteOrgIDHeader doesn't indicate whether or not Tempo is in multitenant mode. Honestly an empty tenant here is always an error whether it's running as single or multi tenant. I'd just change the code to always return an error if tenant is an empty string.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@joe-elliott I understood I changed my code always return error when tenant comes empty string

@joe-elliott joe-elliott left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

nice find and thx for the contribution!

@joe-elliott
joe-elliott merged commit 822337d into grafana:main Sep 2, 2025
37 of 38 checks passed
mattdurham pushed a commit to mattdurham/tempo that referenced this pull request Jun 18, 2026
…afana#5586)

* fix(metrics-generator): prevent WAL deletion when tenant is empty

* fix(metrics-generator): updateCHANGELOG.md

* fix : empty tenant should return error in multi-tenant

* fix : empty tenant should return error in multi-tenant

* fix : change condition from test

---------

Co-authored-by: backend-sungyoon <kim.sungyoon@cashwalk.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

4 participants