-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
ref(billing): Add quota_config_mapping for proto/sentry QuotaConfig conversion #119133
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
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b60c36c
ref(billing): Add quota_config_mapping for proto/sentry QuotaConfig c…
volokluev f1a1a04
fix(billing): Handle unknown proto categories and scopes in quota con…
volokluev ba82e25
fix(billing): Return None when all proto categories are unmapped
volokluev 1b0453c
fix(billing): Fix mypy type errors in quota config mapping tests
volokluev 95678ce
fix log
volokluev 201c7b3
ref(billing): Move quota_config_mapping into quota/ subdirectory
volokluev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
src/sentry/billing/platform/services/quota_config_mapping.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
|
|
||
| from sentry_protos.billing.v1.quota_config_pb2 import QuotaConfig as ProtoQuotaConfig | ||
| from sentry_protos.billing.v1.quota_config_pb2 import QuotaScope as ProtoQuotaScope | ||
|
|
||
| from sentry.billing.platform.services.category_mapping import ( | ||
| proto_to_sentry_category, | ||
| sentry_to_proto_category, | ||
| ) | ||
| from sentry.constants import DataCategory | ||
| from sentry.quotas.base import QuotaConfig, QuotaScope | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def sentry_to_proto_quota_config(quota: QuotaConfig) -> ProtoQuotaConfig: | ||
| """Convert a sentry QuotaConfig to its proto equivalent.""" | ||
| kwargs: dict = { | ||
| "id": quota.id or "", | ||
| "categories": sorted(sentry_to_proto_category(int(c)) for c in quota.categories), | ||
| "scope": ProtoQuotaScope.ValueType(int(quota.scope)) | ||
| if quota.scope is not None | ||
| else ProtoQuotaScope.QUOTA_SCOPE_UNSPECIFIED, | ||
| } | ||
| if quota.scope_id is not None: | ||
| kwargs["scope_id"] = quota.scope_id | ||
| if quota.limit is not None: | ||
| kwargs["limit"] = quota.limit | ||
| if quota.window is not None: | ||
| kwargs["window"] = quota.window | ||
| if quota.reason_code is not None: | ||
| kwargs["reason_code"] = quota.reason_code | ||
|
|
||
| return ProtoQuotaConfig(**kwargs) | ||
|
|
||
|
|
||
| def proto_to_sentry_quota_config(proto_quota: ProtoQuotaConfig) -> QuotaConfig: | ||
| """Convert a proto QuotaConfig to its sentry equivalent.""" | ||
| categories: list[DataCategory] = [] | ||
| for c in proto_quota.categories: | ||
| sentry_cat = proto_to_sentry_category(c) | ||
| try: | ||
| categories.append(DataCategory(sentry_cat)) | ||
| except ValueError: | ||
| logger.warning( | ||
| "quota_config_mapping.unknown_category", | ||
| extra={"proto_category": c, "mapped_value": sentry_cat}, | ||
| ) | ||
|
|
||
| try: | ||
| scope = ( | ||
| QuotaScope(proto_quota.scope) | ||
| if proto_quota.scope != ProtoQuotaScope.QUOTA_SCOPE_UNSPECIFIED | ||
| else QuotaScope.ORGANIZATION | ||
| ) | ||
| except ValueError: | ||
| logger.warning( | ||
| "quota_config_mapping.unknown_scope", | ||
| extra={"proto_scope": proto_quota.scope}, | ||
| ) | ||
| scope = QuotaScope.ORGANIZATION | ||
|
|
||
| return QuotaConfig( | ||
| id=proto_quota.id or None, | ||
| categories=categories, | ||
| scope=scope, | ||
| scope_id=proto_quota.scope_id if proto_quota.HasField("scope_id") else None, | ||
| limit=proto_quota.limit if proto_quota.HasField("limit") else None, | ||
| window=proto_quota.window if proto_quota.HasField("window") else None, | ||
| reason_code=proto_quota.reason_code if proto_quota.HasField("reason_code") else None, | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should log an exception