用途:这是 Sentry 内部 NotificationPlatform 的开发指南。该平台是基于 provider 的通知系统,支持 Email、Slack、Discord 和 MS Teams,开发者定义数据与模板并注册后,由平台按 provider 完成渲染与投递。
主要能力:新增通知(最常见)、为已有 provider 添加自定义渲染器、接入全新 provider。核心概念包括 NotificationData(冻结 dataclass,携带单条通知载荷,须声明 source 类变量)、NotificationTemplate(将数据转为与 provider 无关的 NotificationRenderedTemplate)、NotificationProvider、NotificationRenderer、NotificationTarget 与 NotificationService(入口,提供 has_access、notify_target、notify_async、notify_sync)。
使用方式:新增通知需在 types.py 添加 NotificationSource 枚举值并映射到 NotificationCategory;在 templates/<name>.py 定义冻结 dataclass 数据类与模板,用 @template_registry.register 注册,并在 templates/init.py 导入;随后在 sentry-options-automator 配置灰度选项(internal-testing、is-sentry、early-adopter、general-access,值为 source 到 0.0-1.0 的映射);发送前用 NotificationService.has_access 做灰度判断,再调用 notify_async 等。自定义渲染器通过覆盖 provider 的 get_renderer() 实现,文件放在 {provider}/renderers/。新 provider 需实现协议、默认渲染器并注册,且用 feature flag 控制可用性。
测试与验证:测试目录为 tests/sentry/notifications/platform/,运行 pytest -svv --reuse-db tests/sentry/notifications/platform/,并完成来源枚举、映射、注册、导入、example_data、灰度配置、has_access 守卫等检查清单。
限制:灰度选项在外部仓库配置,不在本仓库;新 provider 仅应在接入新集成时进行;资料未提供具体收费、价格或平台支持范围等信息。