Prefect
概览¶
OpenSRE 会查询 Prefect 以检索最近的 flow run(包括失败运行的日志)和 worker 健康状况——帮助诊断流水线故障,并识别停滞或崩溃的编排任务。
前置条件¶
- Prefect Cloud 账户或自托管 Prefect Server
- API 密钥(Prefect Cloud)或可访问的 API URL(自托管)
配置¶
目前没有专用的 opensre integrations setup prefect 向导。请通过持久化存储进行配置(OpenSRE 未定义产品级 PREFECT_* 环境变量),然后验证。
选项 1:持久化存储¶
添加到 ~/.opensre/integrations.json:
{
"version": 1,
"integrations": [
{
"id": "prefect-prod",
"service": "prefect",
"status": "active",
"credentials": {
"api_url": "https://api.prefect.cloud/api",
"api_key": "your-prefect-api-key",
"account_id": "your-account-id",
"workspace_id": "your-workspace-id"
}
}
]
}
| 字段 | 默认值 | 描述 |
|---|---|---|
api_url |
https://api.prefect.cloud/api |
Prefect API URL(自托管时覆盖) |
api_key |
— | Prefect Cloud API 密钥 |
account_id |
— | Prefect Cloud 账户 ID |
workspace_id |
— | Prefect Cloud 工作区 ID |
凭据¶
Prefect Cloud¶
- 在 Prefect Cloud 中,转到你的 个人资料图标 → API 密钥
- 点击 创建 API 密钥
- 从 URL 中复制密钥、账户 ID 和工作区 ID:
https://app.prefect.cloud/account/<account-id>/workspace/<workspace-id>/
自托管 Prefect Server¶
将 api_url 设置为你的服务器的 API 端点(如果未启用身份验证,则无需 API 密钥):
工具¶
| 工具 | 功能 |
|---|---|
prefect_flow_runs |
列出按状态(FAILED、CRASHED 等)筛选的最近 flow run;可通过 fetch_logs_for_run_id 获取特定运行的日志 |
prefect_worker_health |
跨 work pool 的 worker 健康状况与心跳状态 |
验证¶
成功时的预期输出(自托管,已设置 api_url):
对于 Prefect Cloud(仅设置 api_key,未设置 api_url):
这是一项配置存在性检查(已设置 api_url 或 api_key)——它不会调用 Prefect API。
故障排查¶
| 症状 | 修复方法 |
|---|---|
| 401 未授权 | 检查你的 API 密钥和账户/工作区 ID |
| 连接被拒绝 | 验证 api_url 是否可达——检查自托管的防火墙规则 |
| 未返回任何 flow run | flow run 可能已被清除——检查保留设置 |
| 验证通过但工具失败 | 如果凭据存在但错误,则属于预期情况——确认 Cloud 与自托管的字段 |
安全¶
- 使用服务账户 API 密钥,而不是个人密钥。
- 对于自托管,将 Prefect API 的网络访问限制为受信任的 IP。
- 将凭据存储在
~/.opensre/integrations.json中,而不是源代码中。
本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 op/opensre