Skip to content

fix(encryption): Fix encrypted field metrics table_name tag#106957

Merged
vgrozdanic merged 2 commits intomasterfrom
fix-encrypted-field-metrics
Jan 28, 2026
Merged

fix(encryption): Fix encrypted field metrics table_name tag#106957
vgrozdanic merged 2 commits intomasterfrom
fix-encrypted-field-metrics

Conversation

@vgrozdanic
Copy link
Member

@vgrozdanic vgrozdanic commented Jan 26, 2026

Fix encrypted field metrics to properly send the table name in metric tags.

The original code implemented __set_name__() to capture the model name, expecting Python's descriptor protocol to call it. However, Django's ModelBase metaclass removes fields from the class dict and stores them in _meta before Python can call __set_name__(). Django uses contribute_to_class() instead, which the encrypted field wasn't implementing.

Previously the model name was not being sent in metrics because:
- Used getattr(self, '_model_name', 'unknown') but _model_name was never set
- Field had __set_name__ but Django's ModelBase metaclass never calls it
- Django removes fields from class dict before Python can call __set_name__

Changes:
- Initialize _model_name in __init__ with 'unknown' default
- Implement contribute_to_class to capture actual model name
- Change metric tag from 'model' to 'table_name' for clarity
- Add tests to verify metrics are sent with correct table_name

All 68 encryption field tests pass.
@github-actions github-actions bot added the Scope: Backend Automatically applied to PRs that change backend components label Jan 26, 2026
@vgrozdanic vgrozdanic changed the title fix(db): Fix encrypted field metrics table_name tag fix(encryption): Fix encrypted field metrics table_name tag Jan 28, 2026
"method": encryption_method,
"field_type": self.__class__.__name__,
"model": getattr(self, "_model_name", "unknown"),
"table_name": self._model_name,
Copy link
Member Author

Choose a reason for hiding this comment

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

changed also the tag name because model is too generic tag

@vgrozdanic vgrozdanic requested a review from a team January 28, 2026 09:45
Copy link
Member

@armenzg armenzg left a comment

Choose a reason for hiding this comment

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

🎉

@vgrozdanic vgrozdanic merged commit caf1f31 into master Jan 28, 2026
68 checks passed
@vgrozdanic vgrozdanic deleted the fix-encrypted-field-metrics branch January 28, 2026 13:43
priscilawebdev pushed a commit that referenced this pull request Feb 2, 2026
Fix encrypted field metrics to properly send the table name in metric
tags.

The original code implemented `__set_name__()` to capture the model
name, expecting Python's descriptor protocol to call it. However,
Django's `ModelBase` metaclass removes fields from the class dict and
stores them in `_meta` before Python can call `__set_name__()`. Django
uses `contribute_to_class()` instead, which the encrypted field wasn't
implementing.
@github-actions github-actions bot locked and limited conversation to collaborators Feb 13, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Scope: Backend Automatically applied to PRs that change backend components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants