1
0

feat: 配置变量系统与 target id/name 双字段标识

- 新增顶层 variables 段支持 string/number/boolean 字面量
- target 字符串字段支持 、、{...} 转义语法
- 变量解析优先级: variables -> process.env -> 默认值 -> 报错
- 完整引用保留原始类型,部分引用拼接为字符串
- 变量替换在 YAML 解析后、AJV 校验前执行
- 替换仅作用于 targets,跳过 id/type 字段
- target 新增必填 id 字段作为唯一标识,name 改为可选展示名称
- 数据库存储/API/前端全面迁移到 id 标识
- 统一 checker 运行时类型检查为 es-toolkit predicates
- 同步 delta specs 到主 specs,归档 config-variables 变更
This commit is contained in:
2026-05-17 00:37:54 +08:00
parent 366b3211c8
commit 7926514986
53 changed files with 1538 additions and 333 deletions

View File

@@ -69,6 +69,12 @@ runtime:
maxConcurrentChecks: 20
retention: "7d"
variables:
env_name: "生产"
base_url: "https://api.example.com"
api_token: "Bearer demo-token"
sqlite_url: "sqlite:///path/to/db.sqlite"
defaults:
interval: "30s"
timeout: "10s"
@@ -78,7 +84,8 @@ defaults:
maxOutputBytes: "1MB"
targets:
- name: "Baidu"
- id: "baidu-home"
name: "Baidu"
type: http
http:
url: "https://www.baidu.com"
@@ -86,10 +93,13 @@ targets:
status: [200]
maxDurationMs: 10000
- name: "JSON API 示例"
- id: "json-api"
name: "${env_name} JSON API 示例"
type: http
http:
url: "https://httpbin.org/json"
url: "${base_url}/json"
headers:
Authorization: "${api_token|Bearer fallback-token}"
expect:
status: [200]
headers:
@@ -100,7 +110,8 @@ targets:
path: "$.slideshow.title"
equals: "Sample Slide Show"
- name: "Bun 脚本检查"
- id: "bun-script"
name: "Bun 脚本检查"
type: cmd
cmd:
exec: "bun"
@@ -110,10 +121,11 @@ targets:
stdout:
- contains: "ok"
- name: "SQLite 数据库检查"
- id: "sqlite-active-users"
name: "SQLite 数据库检查"
type: db
db:
url: "sqlite:///path/to/db.sqlite"
url: "${sqlite_url}"
query: "SELECT COUNT(*) as cnt FROM users WHERE status = 'active'"
expect:
maxDurationMs: 5000
@@ -150,17 +162,28 @@ targets:
| `cmd.maxOutputBytes` | 输出最大字节数 | `100MB` |
| `cmd.cwd` | 默认工作目录(相对于配置文件所在目录) | `.` |
#### variables — 配置变量(可省略)
`variables` 是顶层动态键值表key 必须符合 `[a-zA-Z_][a-zA-Z0-9_]*`value 仅支持 string、number、boolean。target 中的字符串值可引用变量:
- `${key}`:引用 variables 或环境变量
- `${key|default}`:变量和环境变量都不存在时使用默认值,第一个 `|` 后的内容为默认值
- `$${key}`:转义输出字面量 `${key}`
解析优先级为 `variables -> process.env -> 默认值`。字段值完整等于单个变量引用时会保留 number/boolean/string 类型;部分拼接时统一转为字符串。变量替换仅作用于 `targets`,且不会替换 `id``type` 字段。
#### targets — 拨测目标列表(必填)
每个 target 的通用字段:
| 字段 | 说明 | 必填 |
| ---------- | ----------------------------- | -------------------- |
| `name` | 目标名称(全局唯一) | 是 |
| `type` | 目标类型:`http``cmd``db` | |
| `group` | 分组名称 | 否,默认 `"default"` |
| `interval` | 覆盖全局拨测间隔 | 否 |
| `timeout` | 覆盖全局超时时间 | 否 |
| 字段 | 说明 | 必填 |
| ---------- | ---------------------------------------------------------- | -------------------- |
| `id` | 目标唯一标识,支持字母数字、下划线、连字符,不参与变量替换 | 是 |
| `name` | 展示名称,支持变量替换;省略时使用 `id` | |
| `type` | 目标类型:`http``cmd``db` | |
| `group` | 分组名称 | 否,默认 `"default"` |
| `interval` | 覆盖全局拨测间隔 | 否 |
| `timeout` | 覆盖全局超时时间 | 否 |
**HTTP 类型** (`type: http`)
@@ -218,7 +241,7 @@ targets:
**JSON Schema**:仓库根目录导出 `probe-config.schema.json`,在 YAML 文件顶部添加 `# yaml-language-server: $schema=./probe-config.schema.json` 即可在编辑器中获得提示和校验。
> **注意:** 配置校验在启动时执行,非法配置会阻止启动并输出错误信息。除动态键值表(`headers`、`env`)外,未知字段会导致启动失败,请使用 YAML 注释。
> **注意:** 配置校验在启动时执行,非法配置会阻止启动并输出错误信息。除动态键值表(`headers`、`env`、`variables`)外,未知字段会导致启动失败,请使用 YAML 注释。
## 目标状态判定