feat(workflow_engine): Add in hook for producing occurrences from the stateful detector - #1
feat(workflow_engine): Add in hook for producing occurrences from the stateful detector#1linxia0415 wants to merge 2 commits into
Conversation
… stateful detector This adds a hook that can be implemented to produce an occurrence specific to the detector that is subclassing the StatefulDetector. Also change the signature of evaluate to return a dict keyed by groupkey instead of a list. This helps avoid the chance of duplicate results for the same group key.
There was a problem hiding this comment.
AI代码审查报告
变更概览
本次 PR 涉及 4 个文件,新增 +249 行,删除 -151 行。
功能变更摘要
本 PR 重构了工作流引擎的检测器处理逻辑,简化了事件组类型定义,并为检测器模型添加了新属性。同时大幅扩充了检测器处理器的单元测试用例,以覆盖更多执行场景。
文件变更摘要
| 文件 | 变更 | 行数 | 摘要 | 发现问题 |
|---|---|---|---|---|
src/sentry/workflow_engine/processors/detector.py |
修改 | +22/-23 | 重构检测器处理逻辑,优化异常处理机制 | 3 个 |
tests/sentry/workflow_engine/processors/test_detector.py |
修改 | +217/-119 | 扩充检测器处理器单元测试,覆盖更多场景 | — |
src/sentry/incidents/grouptype.py |
修改 | +3/-8 | 精简事件组类型枚举定义,移除未使用类型 | — |
src/sentry/workflow_engine/models/detector.py |
修改 | +7/-1 | 为检测器模型添加新属性并更新导入语句 | — |
问题严重级别分布
| 级别 | 数量 | 占比 |
|---|---|---|
| 🟡 中危 | 2 | 100% |
代表性问题(至多 10 条,按严重级别优先)
- 🟡 中危
src/sentry/workflow_engine/processors/detector.pyL164: 类型注解缺失
line 164 处 build_fingerprint 方法的 group_key 参数缺少类型注解。根据上下文,该参数应为 DetectorGroupKey 类型(即 …
2. 🟡 中危 src/sentry/workflow_engine/processors/detector.py L292: 潜在的类型转换风险
line 292 处将 DetectorPriorityLevel 类型的 new_status 直接转换为 PriorityLevel。`DetectorPrior…
由 CodeHawk 提供支持 · nuwa
| @@ -174,6 +163,12 @@ def get_group_key_values(self, data_packet: DataPacket[T]) -> dict[str, int]: | |||
| """ | |||
| pass | |||
There was a problem hiding this comment.
🟡 AI 代码审查发现问题
📋 问题概述
类型注解缺失
line 164 处 build_fingerprint 方法的 group_key 参数缺少类型注解。根据上下文,该参数应为 DetectorGroupKey 类型(即 str | None)。缺少类型注解会导致静态类型检查器无法验证调用时的类型安全性,且与项目中其他方法的类型注解风格不一致。
📍 问题详情
🟡 问题 1 | 严重程度: MEDIUM | 行号: 164
💬 详细说明:
- 类型注解缺失
- line 164 处
build_fingerprint方法的group_key参数缺少类型注解。根据上下文,该参数应为DetectorGroupKey类型(即str | None)。缺少类型注解会导致静态类型检查器无法验证调用时的类型安全性,且与项目中其他方法的类型注解风格不一致。
📝 问题代码:
def build_fingerprint(self, group_key) -> list[str]:
💡 修复建议:
为
group_key参数添加类型注解DetectorGroupKey。根据文件顶部导入,DetectorGroupKey已从sentry.workflow_engine.types导入,可直接使用。
✅ 修复示例:
def build_fingerprint(self, group_key: DetectorGroupKey) -> list[str]:
"""
Builds a fingerprint to uniquely identify a detected issue
"""
return [f"{self.detector.id}{':' + group_key if group_key is not None else ''}"]
🔗 参考链接
无
| 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.
🟡 AI 代码审查发现问题
📋 问题概述
潜在的类型转换风险
line 292 处将 DetectorPriorityLevel 类型的 new_status 直接转换为 PriorityLevel。DetectorPriorityLevel(来自 sentry.workflow_engine.types)和 PriorityLevel(来自 sentry.types.group)是两个不同的枚举类型,尽管它们可能有意保持数值对应关系,但直接构造转换缺乏显式映射验证。若两个枚举的数值定义不一致,会导致错误的优先级级别被传递。
📍 问题详情
🟡 问题 1 | 严重程度: MEDIUM | 行号: 292
💬 详细说明:
- 潜在的类型转换风险
- line 292 处将
DetectorPriorityLevel类型的new_status直接转换为PriorityLevel。DetectorPriorityLevel(来自sentry.workflow_engine.types)和PriorityLevel(来自sentry.types.group)是两个不同的枚举类型,尽管它们可能有意保持数值对应关系,但直接构造转换缺乏显式映射验证。若两个枚举的数值定义不一致,会导致错误的优先级级别被传递。
📝 问题代码:
result, event_data = self.build_occurrence_and_event_data(
group_key, value, PriorityLevel(new_status)
)
💡 修复建议:
添加显式的枚举值映射,或定义转换方法确保类型安全。例如,在类中定义一个映射字典或方法,将
DetectorPriorityLevel安全转换为PriorityLevel,并在转换时进行有效性校验。
✅ 修复示例:
else:
# 显式映射 DetectorPriorityLevel 到 PriorityLevel
priority_mapping = {
DetectorPriorityLevel.OK: PriorityLevel.OK,
DetectorPriorityLevel.LOW: PriorityLevel.LOW,
DetectorPriorityLevel.MEDIUM: PriorityLevel.MEDIUM,
DetectorPriorityLevel.HIGH: PriorityLevel.HIGH,
}
sentry_priority = priority_mapping.get(new_status)
if sentry_priority is None:
raise ValueError(f"Unknown priority level: {new_status}")
result, event_data = self.build_occurrence_and_event_data(
group_key, value, sentry_priority
)
🔗 参考链接
无
This adds a hook that can be implemented to produce an occurrence specific to the detector that is subclassing the StatefulDetector.
Also change the signature of evaluate to return a dict keyed by groupkey instead of a list. This helps avoid the chance of duplicate results for the same group key.