-
Notifications
You must be signed in to change notification settings - Fork 19
Add Bugsnag error grouping with stable normalized keys #234
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
Open
morgan-wowk
wants to merge
1
commit into
bugsnag/orchestrator-integration
Choose a base branch
from
bugsnag/error-grouping
base: bugsnag/orchestrator-integration
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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
100 changes: 100 additions & 0 deletions
100
cloud_pipelines_backend/instrumentation/error_normalization.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,100 @@ | ||
| """ | ||
| Normalizes exception messages into stable strings for error grouping. | ||
|
|
||
| Strips instance-specific values (pod names, IDs, memory addresses, byte | ||
| offsets) so that structurally identical errors produce the same key | ||
| regardless of which specific resource was involved. | ||
| """ | ||
|
|
||
| import json | ||
| import re | ||
|
|
||
| _POD_NAME_PATTERN = re.compile(r"(?:task|tangle(?:-ce)?)-[a-zA-Z0-9]+-[a-zA-Z0-9]+") | ||
| _OBJECT_REPR_PATTERN = re.compile(r"<[^>]+ object at 0x[0-9a-fA-F]+>") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Eventually, we might want to fix such address strings (if they are not informative). |
||
| _HEX_ADDRESS_PATTERN = re.compile(r"\b0x[0-9a-fA-F]+\b") | ||
| _UUID_PATTERN = re.compile( | ||
| r"[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", re.IGNORECASE | ||
| ) | ||
| _LONG_ALNUM_ID_PATTERN = re.compile(r"\b[a-zA-Z0-9]{16,}\b") | ||
|
|
||
|
|
||
| def _strip_generic(*, message: str) -> str: | ||
| message = _OBJECT_REPR_PATTERN.sub("{object}", message) | ||
| message = _HEX_ADDRESS_PATTERN.sub("{addr}", message) | ||
| message = _UUID_PATTERN.sub("{uuid}", message) | ||
| message = _LONG_ALNUM_ID_PATTERN.sub("{id}", message) | ||
| return message.strip() | ||
|
|
||
|
|
||
| def _normalize_k8s_api_exception(*, exception: BaseException) -> str | None: | ||
| try: | ||
| from kubernetes.client import exceptions as k8s_exceptions | ||
|
|
||
| if not isinstance(exception, k8s_exceptions.ApiException): | ||
| return None | ||
| except ImportError: | ||
| return None | ||
|
|
||
| status_code = exception.status | ||
| try: | ||
| body = json.loads(exception.body) | ||
| reason = body.get("reason", "") | ||
| message = body.get("message", "") | ||
| except (json.JSONDecodeError, TypeError): | ||
| reason = "" | ||
| message = str(exception) | ||
|
|
||
| message = _POD_NAME_PATTERN.sub("{pod}", message) | ||
| parts = [f"kubernetes ApiException ({status_code})"] | ||
| if reason: | ||
| parts.append(reason) | ||
| if message: | ||
| parts.append(message) | ||
| return ": ".join(parts) | ||
|
|
||
|
|
||
| def _normalize_max_retry_error(*, exception: BaseException) -> str | None: | ||
| try: | ||
| from urllib3.exceptions import MaxRetryError | ||
|
|
||
| if not isinstance(exception, MaxRetryError): | ||
| return None | ||
| except ImportError: | ||
| return None | ||
|
|
||
| cause = type(exception.reason).__name__ if exception.reason else "unknown" | ||
| return f"MaxRetryError: k8s connection pool max retries exceeded ({cause})" | ||
|
|
||
|
|
||
| def _normalize_unicode_decode_error(*, exception: BaseException) -> str | None: | ||
| if not isinstance(exception, UnicodeDecodeError): | ||
| return None | ||
| return f"UnicodeDecodeError: '{exception.encoding}' codec can't decode byte at position {{n}}" | ||
|
|
||
|
|
||
| def _normalize_orchestrator_error(*, exception: BaseException) -> str | None: | ||
| try: | ||
| from ..orchestrator_sql import OrchestratorError | ||
|
|
||
| if not isinstance(exception, OrchestratorError): | ||
| return None | ||
| except ImportError: | ||
| return None | ||
|
|
||
| message = _OBJECT_REPR_PATTERN.sub("{object}", str(exception)) | ||
| return f"OrchestratorError: {message}" | ||
|
|
||
|
|
||
| def normalize_error_message(*, exception: BaseException) -> str: | ||
| """Return a stable normalized string for error grouping.""" | ||
| for normalizer in ( | ||
| _normalize_k8s_api_exception, | ||
| _normalize_max_retry_error, | ||
| _normalize_unicode_decode_error, | ||
| _normalize_orchestrator_error, | ||
| ): | ||
| result = normalizer(exception=exception) | ||
| if result is not None: | ||
| return result | ||
|
|
||
| return f"{type(exception).__name__}: {_strip_generic(message=str(exception))}" | ||
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
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
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.
For extra safety against potential package changes in the future, this is wrapped in a try catch with a fallback to no prefixing.