-
Notifications
You must be signed in to change notification settings - Fork 4
feat(workflow_engine): Add in hook for producing occurrences from the stateful detector #1
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| from sentry.issues.producer import PayloadType, produce_occurrence_to_kafka | ||
| from sentry.issues.status_change_message import StatusChangeMessage | ||
| from sentry.models.group import GroupStatus | ||
| from sentry.types.group import PriorityLevel | ||
| from sentry.utils import metrics, redis | ||
| from sentry.utils.function_cache import cache_func_for_models | ||
| from sentry.utils.iterators import chunked | ||
|
|
@@ -45,7 +46,7 @@ class DetectorEvaluationResult: | |
|
|
||
| def process_detectors( | ||
| data_packet: DataPacket, detectors: list[Detector] | ||
| ) -> list[tuple[Detector, list[DetectorEvaluationResult]]]: | ||
| ) -> list[tuple[Detector, dict[DetectorGroupKey, DetectorEvaluationResult]]]: | ||
| results = [] | ||
|
|
||
| for detector in detectors: | ||
|
|
@@ -55,25 +56,11 @@ def process_detectors( | |
| continue | ||
|
|
||
| detector_results = handler.evaluate(data_packet) | ||
| detector_group_keys = set() | ||
|
|
||
| for result in detector_results: | ||
| if result.group_key in detector_group_keys: | ||
| # This shouldn't happen - log an error and continue on, but we should investigate this. | ||
| logger.error( | ||
| "Duplicate detector state group keys found", | ||
| extra={ | ||
| "detector_id": detector.id, | ||
| "group_key": result.group_key, | ||
| }, | ||
| ) | ||
| continue | ||
|
|
||
| for result in detector_results.values(): | ||
| if result.result is not None: | ||
| create_issue_occurrence_from_result(result) | ||
|
|
||
| detector_group_keys.add(result.group_key) | ||
|
|
||
| if detector_results: | ||
| results.append((detector, detector_results)) | ||
|
|
||
|
|
@@ -136,7 +123,9 @@ def __init__(self, detector: Detector): | |
| self.conditions = [] | ||
|
|
||
| @abc.abstractmethod | ||
| def evaluate(self, data_packet: DataPacket[T]) -> list[DetectorEvaluationResult]: | ||
| def evaluate( | ||
| self, data_packet: DataPacket[T] | ||
| ) -> dict[DetectorGroupKey, DetectorEvaluationResult]: | ||
| pass | ||
|
|
||
| def commit_state_updates(self): | ||
|
|
@@ -174,6 +163,12 @@ def get_group_key_values(self, data_packet: DataPacket[T]) -> dict[str, int]: | |
| """ | ||
| pass | ||
|
|
||
| @abc.abstractmethod | ||
| def build_occurrence_and_event_data( | ||
| self, group_key: DetectorGroupKey, value: int, new_status: PriorityLevel | ||
| ) -> tuple[IssueOccurrence, dict[str, Any]]: | ||
| pass | ||
|
|
||
| def build_fingerprint(self, group_key) -> list[str]: | ||
| """ | ||
| Builds a fingerprint to uniquely identify a detected issue | ||
|
|
@@ -228,7 +223,9 @@ def get_state_data( | |
| ) | ||
| return results | ||
|
|
||
| def evaluate(self, data_packet: DataPacket[T]) -> list[DetectorEvaluationResult]: | ||
| def evaluate( | ||
| self, data_packet: DataPacket[T] | ||
| ) -> dict[DetectorGroupKey, DetectorEvaluationResult]: | ||
| """ | ||
| Evaluates a given data packet and returns a list of `DetectorEvaluationResult`. | ||
| There will be one result for each group key result in the packet, unless the | ||
|
|
@@ -237,13 +234,13 @@ def evaluate(self, data_packet: DataPacket[T]) -> list[DetectorEvaluationResult] | |
| dedupe_value = self.get_dedupe_value(data_packet) | ||
| group_values = self.get_group_key_values(data_packet) | ||
| all_state_data = self.get_state_data(list(group_values.keys())) | ||
| results = [] | ||
| results = {} | ||
| for group_key, group_value in group_values.items(): | ||
| result = self.evaluate_group_key_value( | ||
| group_key, group_value, all_state_data[group_key], dedupe_value | ||
| ) | ||
| if result: | ||
| results.append(result) | ||
| results[result.group_key] = result | ||
| return results | ||
|
|
||
| def evaluate_group_key_value( | ||
|
|
@@ -289,7 +286,7 @@ def evaluate_group_key_value( | |
| is_active = new_status != DetectorPriorityLevel.OK | ||
| self.enqueue_state_update(group_key, is_active, new_status) | ||
| event_data = None | ||
| result = None | ||
| result: StatusChangeMessage | IssueOccurrence | ||
| if new_status == DetectorPriorityLevel.OK: | ||
| # If we've determined that we're now ok, we just want to resolve the issue | ||
| result = StatusChangeMessage( | ||
|
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. 🟡 AI 代码审查发现问题 📋 问题概述
line 292 处将 📍 问题详情🟡 问题 1 | 严重程度:
|
||
|
|
@@ -298,8 +295,10 @@ def evaluate_group_key_value( | |
| new_status=GroupStatus.RESOLVED, | ||
| new_substatus=None, | ||
| ) | ||
|
|
||
| # TODO: Add hook here for generating occurrence | ||
| else: | ||
| result, event_data = self.build_occurrence_and_event_data( | ||
| group_key, value, PriorityLevel(new_status) | ||
| ) | ||
| return DetectorEvaluationResult( | ||
| group_key=group_key, | ||
| is_active=is_active, | ||
|
|
||
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.
🟡 AI 代码审查发现问题
📋 问题概述
line 164 处
build_fingerprint方法的group_key参数缺少类型注解。根据上下文,该参数应为DetectorGroupKey类型(即str | None)。缺少类型注解会导致静态类型检查器无法验证调用时的类型安全性,且与项目中其他方法的类型注解风格不一致。📍 问题详情
🟡 问题 1 | 严重程度:
MEDIUM| 行号:164💬 详细说明:
build_fingerprint方法的group_key参数缺少类型注解。根据上下文,该参数应为DetectorGroupKey类型(即str | None)。缺少类型注解会导致静态类型检查器无法验证调用时的类型安全性,且与项目中其他方法的类型注解风格不一致。📝 问题代码:
💡 修复建议:
✅ 修复示例:
🔗 参考链接
无