perses-variable-manage
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChinesePerses Variable Management
Perses 变量管理
Create and manage variables with chains and interpolation.
创建并管理带有依赖链和插值配置的变量。
Operator Context
操作上下文
This skill operates as the lifecycle manager for Perses variables, handling creation, chaining, and interpolation configuration across scopes.
本技能作为Perses变量的生命周期管理器,处理跨范围的变量创建、依赖链配置和插值设置。
Hardcoded Behaviors (Always Apply)
硬编码行为(始终生效)
- Chain ordering: Variables must be ordered so dependencies come first — Perses evaluates variables in array order, so a variable referencing must appear after the cluster variable
$cluster - MCP-first: Use Perses MCP tools when available, percli as fallback
- Interpolation format: Document which format is used and why — wrong format causes query syntax errors (e.g., regex format for Prometheus matchers, csv for multi-select labels)
- 依赖链顺序:变量必须按依赖关系排序,依赖项在前——Perses按数组顺序评估变量,因此引用的变量必须出现在cluster变量之后
$cluster - 优先使用MCP:优先使用Perses MCP工具,无可用时回退到percli
- 插值格式:记录所使用的格式及其原因——错误的格式会导致查询语法错误(例如,Prometheus匹配器使用regex格式,多选择标签使用csv格式)
Default Behaviors (ON unless disabled)
默认行为(启用状态,除非手动关闭)
- ListVariable: Default to ListVariable with PrometheusLabelValuesVariable plugin
- Dashboard scope: Create variables at dashboard scope unless otherwise specified
- Multi-select: Enable allowMultiple and allowAllValue by default for filter variables
- ListVariable:默认使用带PrometheusLabelValuesVariable插件的ListVariable
- 仪表板范围:除非另有指定,否则在仪表板范围创建变量
- 多选择:默认启用allowMultiple和allowAllValue选项用于过滤器变量
Optional Behaviors (OFF unless enabled)
可选行为(关闭状态,除非手动启用)
- Global/project variables: Create at global or project scope for reuse across dashboards
- TextVariable: Use TextVariable for free-form user input fields
- 全局/项目变量:在全局或项目范围创建变量,以便跨仪表板复用
- TextVariable:使用TextVariable作为自由格式用户输入字段
What This Skill CAN Do
本技能可完成的操作
- Create TextVariable and ListVariable at any scope (global, project, dashboard)
- Set up variable chains with cascading dependencies
- Configure interpolation formats (csv, regex, json, lucene, pipe, glob, etc.)
- Use all 4 variable plugin types
- 在任意范围(全局、项目、仪表板)创建TextVariable和ListVariable
- 设置带有级联依赖的变量链
- 配置插值格式(csv、regex、json、lucene、pipe、glob等)
- 使用全部4种变量插件类型
What This Skill CANNOT Do
本技能不可完成的操作
- Create custom variable plugins (use perses-plugin-create)
- Create dashboards (use perses-dashboard-create)
- Manage datasources (use perses-datasource-manage)
- 创建自定义变量插件(请使用perses-plugin-create)
- 创建仪表板(请使用perses-dashboard-create)
- 管理数据源(请使用perses-datasource-manage)
Instructions
操作步骤
Phase 1: IDENTIFY
阶段1:识别
Goal: Determine variable type, scope, and dependencies.
Variable types:
- TextVariable: Static text input — user types a value. Use for free-form filters like custom regex or label values not available via query.
- ListVariable: Dynamic dropdown populated by a plugin. Use for most filter/selector use cases.
Variable plugins (for ListVariable):
| Plugin Kind | Source | Use Case |
|---|---|---|
| PrometheusLabelValuesVariable | Label values query | Filter by namespace, pod, job |
| PrometheusPromQLVariable | PromQL query results | Dynamic values from expressions |
| StaticListVariable | Hardcoded list | Fixed options (env, region) |
| DatasourceVariable | Available datasources | Switch between datasource instances |
Variable scopes:
| Scope | Resource Kind | Use Case |
|---|---|---|
| Global | GlobalVariable | Shared filters across all projects and dashboards |
| Project | Variable (in project) | Shared filters across dashboards within a project |
| Dashboard | variables[] in dashboard spec | Dashboard-specific filters |
Interpolation formats ():
${var:format}| Format | Output Example | Use Case |
|---|---|---|
| csv | | Multi-value in most contexts |
| json | | JSON-compatible contexts |
| regex | | Prometheus label matchers with |
| pipe | | Pipe-delimited lists |
| glob | | Glob-style matching |
| lucene | | Loki/Elasticsearch queries |
| values | | URL query parameter encoding |
| singlevariablevalue | Single value | Extracts one value from multi-select |
| doublequote | | Quoted CSV |
| singlequote | | Single-quoted CSV |
| raw | | Single value extraction |
Gate: Variable type, plugin, scope, and dependencies identified. Proceed to Phase 2.
目标:确定变量类型、范围和依赖关系。
变量类型:
- TextVariable:静态文本输入——用户手动输入值。适用于自由格式过滤器,例如自定义正则表达式或无法通过查询获取的标签值。
- ListVariable:由插件填充的动态下拉菜单。适用于大多数过滤器/选择器场景。
变量插件(适用于ListVariable):
| 插件类型 | 数据源 | 使用场景 |
|---|---|---|
| PrometheusLabelValuesVariable | 标签值查询 | 按命名空间、Pod、Job过滤 |
| PrometheusPromQLVariable | PromQL查询结果 | 从表达式中获取动态值 |
| StaticListVariable | 硬编码列表 | 固定选项(环境、区域) |
| DatasourceVariable | 可用数据源 | 在数据源实例之间切换 |
变量范围:
| 范围 | 资源类型 | 使用场景 |
|---|---|---|
| 全局 | GlobalVariable | 跨所有项目和仪表板共享过滤器 |
| 项目 | Variable(项目内) | 跨项目内的仪表板共享过滤器 |
| 仪表板 | dashboard spec中的variables[] | 仪表板专属过滤器 |
插值格式 ():
${var:format}| 格式 | 输出示例 | 使用场景 |
|---|---|---|
| csv | | 大多数场景下的多值格式 |
| json | | 兼容JSON的场景 |
| regex | | Prometheus的=~匹配器 |
| pipe | | 竖线分隔的列表 |
| glob | | Glob风格匹配 |
| lucene | | Loki/Elasticsearch查询 |
| values | | URL查询参数编码 |
| singlevariablevalue | 单个值 | 从多选择中提取单个值 |
| doublequote | | 带双引号的CSV |
| singlequote | | 带单引号的CSV |
| raw | | 单个值提取 |
准入条件:已确定变量类型、插件、范围和依赖关系。进入阶段2。
Phase 2: CREATE
阶段2:创建
Goal: Create the variable resource(s).
Single variable (global scope):
bash
percli apply -f - <<EOF
kind: GlobalVariable
metadata:
name: namespace
spec:
kind: ListVariable
spec:
name: namespace
display:
name: Namespace
hidden: false
allowAllValue: true
allowMultiple: true
plugin:
kind: PrometheusLabelValuesVariable
spec:
labelName: namespace
datasource:
kind: PrometheusDatasource
name: prometheus
EOFVia MCP (project scope):
perses_create_project_variable(
project="my-project",
name="namespace",
kind="ListVariable",
plugin_kind="PrometheusLabelValuesVariable",
plugin_spec={"labelName": "namespace", "datasource": {"kind": "PrometheusDatasource", "name": "prometheus"}},
allow_multiple=true,
allow_all_value=true
)Variable chain (dashboard scope — cluster -> namespace -> pod):
Variables must be ordered with dependencies first. Each subsequent variable uses matchers that reference the previous variables:
yaml
variables:
- kind: ListVariable
spec:
name: cluster
display:
name: Cluster
allowAllValue: false
allowMultiple: false
plugin:
kind: PrometheusLabelValuesVariable
spec:
labelName: cluster
datasource:
kind: PrometheusDatasource
name: prometheus
- kind: ListVariable
spec:
name: namespace
display:
name: Namespace
allowAllValue: true
allowMultiple: true
plugin:
kind: PrometheusLabelValuesVariable
spec:
labelName: namespace
datasource:
kind: PrometheusDatasource
name: prometheus
matchers:
- "cluster=\"$cluster\""
- kind: ListVariable
spec:
name: pod
display:
name: Pod
allowAllValue: true
allowMultiple: true
plugin:
kind: PrometheusLabelValuesVariable
spec:
labelName: pod
datasource:
kind: PrometheusDatasource
name: prometheus
matchers:
- "cluster=\"$cluster\""
- "namespace=\"$namespace\""Gate: Variables created without errors. Proceed to Phase 3.
目标:创建变量资源。
单个变量(全局范围):
bash
percli apply -f - <<EOF
kind: GlobalVariable
metadata:
name: namespace
spec:
kind: ListVariable
spec:
name: namespace
display:
name: Namespace
hidden: false
allowAllValue: true
allowMultiple: true
plugin:
kind: PrometheusLabelValuesVariable
spec:
labelName: namespace
datasource:
kind: PrometheusDatasource
name: prometheus
EOF通过MCP创建(项目范围):
perses_create_project_variable(
project="my-project",
name="namespace",
kind="ListVariable",
plugin_kind="PrometheusLabelValuesVariable",
plugin_spec={"labelName": "namespace", "datasource": {"kind": "PrometheusDatasource", "name": "prometheus"}},
allow_multiple=true,
allow_all_value=true
)变量链(仪表板范围 — cluster -> namespace -> pod):
变量必须按依赖关系排序,依赖项在前。后续每个变量使用引用前序变量的匹配器:
yaml
variables:
- kind: ListVariable
spec:
name: cluster
display:
name: Cluster
allowAllValue: false
allowMultiple: false
plugin:
kind: PrometheusLabelValuesVariable
spec:
labelName: cluster
datasource:
kind: PrometheusDatasource
name: prometheus
- kind: ListVariable
spec:
name: namespace
display:
name: Namespace
allowAllValue: true
allowMultiple: true
plugin:
kind: PrometheusLabelValuesVariable
spec:
labelName: namespace
datasource:
kind: PrometheusDatasource
name: prometheus
matchers:
- "cluster=\"$cluster\""
- kind: ListVariable
spec:
name: pod
display:
name: Pod
allowAllValue: true
allowMultiple: true
plugin:
kind: PrometheusLabelValuesVariable
spec:
labelName: pod
datasource:
kind: PrometheusDatasource
name: prometheus
matchers:
- "cluster=\"$cluster\""
- "namespace=\"$namespace\""准入条件:变量创建无错误。进入阶段3。
Phase 3: VERIFY
阶段3:验证
Goal: Confirm variables exist and chains resolve correctly.
bash
undefined目标:确认变量存在且依赖链解析正确。
bash
undefinedList variables in project
列出项目中的变量
percli get variable --project <project>
percli get variable --project <project>
List global variables
列出全局变量
percli get globalvariable
percli get globalvariable
Describe specific variable
查看特定变量详情
percli describe variable <name> --project <project>
Or via MCP:perses_list_variables(project="<project>")
perses_list_global_variables()
Verify chain behavior by checking that dependent variables correctly filter when parent values change (requires UI or API query testing).
**Gate**: Variables listed and chain dependencies confirmed. Task complete.
---percli describe variable <name> --project <project>
或通过MCP:perses_list_variables(project="<project>")
perses_list_global_variables()
通过检查父变量值变更时,依赖变量是否正确过滤来验证链行为(需要UI或API查询测试)。
**准入条件**:变量已列出且依赖链关系确认无误。任务完成。
---Error Handling
错误处理
| Cause | Symptom | Solution |
|---|---|---|
| Variable chain break: dependent variable defined before its parent in the array | Child variable shows all values instead of filtering by parent selection; Perses evaluates variables in array order, so the parent has no value yet when the child resolves | Reorder the variables array so parents appear before children — cluster before namespace before pod. Each variable can only reference variables that appear earlier in the array |
Wrong interpolation format: using | Prometheus query with | Use |
| PrometheusLabelValuesVariable returns empty dropdown | Variable renders with no selectable options despite metrics existing in Prometheus | Check: (1) |
MCP | Error returned from MCP tool call — variable not created | Check: (1) variable name does not already exist in the project (names must be unique per scope), (2) the target project exists (create it first with |
| Matcher syntax error in child variable | Child variable returns empty results or Perses logs show query parse errors | Matchers must be exact PromQL label matcher syntax: |
| 原因 | 症状 | 解决方案 |
|---|---|---|
| 变量链断裂:依赖变量在数组中定义于父变量之前 | 子变量显示所有值,而非按父变量选择过滤;Perses按数组顺序评估变量,因此父变量在子变量解析时尚无值 | 重新排序变量数组,父变量在前,子变量在后 — cluster在前,然后是namespace,最后是pod。每个变量只能引用数组中出现在它之前的变量 |
插值格式错误:在需要 | 带有 | 对于任何Prometheus的 |
| PrometheusLabelValuesVariable返回空下拉菜单 | 尽管Prometheus中存在指标,但变量渲染后无可选选项 | 检查:(1) |
MCP | MCP工具调用返回错误 — 变量未创建 | 检查:(1) 变量名称在项目中已存在(每个范围的名称必须唯一),(2) 目标项目已存在(先使用 |
| 子变量中的匹配器语法错误 | 子变量返回空结果或Perses日志显示查询解析错误 | 匹配器必须符合PromQL标签匹配器的精确语法: |
Anti-Patterns
反模式
| Anti-Pattern | Why It Fails | Correct Approach |
|---|---|---|
| Wrong dependency order in variable array (child before parent) | Perses evaluates variables top-to-bottom. A child variable referencing | Always order variables by dependency chain: root variables first, then each level of dependents in order |
Using | | Use |
Setting | The variable will return multiple values, but the query uses bare | When |
| Creating GlobalVariable for project-specific filters | Global variables apply to all projects and dashboards, polluting the variable namespace and confusing users who see irrelevant filters | Use project-scoped variables (kind: Variable with project reference) for filters specific to a team or service. Reserve GlobalVariable for truly universal filters like environment or region |
| Duplicating variables across dashboards instead of using project/global scope | Changes must be made in every dashboard individually; variable definitions drift over time | Promote shared variables to project or global scope and reference them consistently across dashboards |
| Hardcoding datasource name without checking available datasources | Variable queries fail silently when the datasource name does not match any configured datasource | List available datasources first ( |
| 反模式 | 失败原因 | 正确做法 |
|---|---|---|
| 变量数组中的依赖顺序错误(子变量在前,父变量在后) | Perses从上到下评估变量。出现在cluster变量之前且引用 | 始终按依赖链顺序排列变量:根变量在前,然后依次是每一级依赖变量 |
在Prometheus | | 使用 |
设置 | 变量会返回多个值,但查询使用裸 | 当 |
| 为项目专属过滤器创建GlobalVariable | 全局变量适用于所有项目和仪表板,会污染变量命名空间,让用户看到无关过滤器而产生困惑 | 针对团队或服务专属的过滤器,使用项目范围变量(kind: Variable,带项目引用)。仅将GlobalVariable用于真正通用的过滤器,例如环境或区域 |
| 跨仪表板重复创建变量,而非使用项目/全局范围 | 变更需在每个仪表板中单独进行;变量定义会随时间产生差异 | 将共享变量升级到项目或全局范围,并在所有仪表板中一致引用 |
| 未检查可用数据源就硬编码数据源名称 | 当数据源名称与已配置的数据源不匹配时,变量查询会静默失败 | 先列出可用数据源( |
Anti-Rationalization
反合理化(错误的自我说服)
These are shortcuts that seem reasonable but cause real failures:
| Rationalization | Why It's Wrong | Required Action |
|---|---|---|
| "The variable order doesn't matter, they all resolve eventually" | Perses evaluates variables strictly in array order, not by dependency graph. A child variable that appears before its parent will resolve against an empty parent value on first load | Map the full dependency chain and verify array order matches |
"I'll just use | Bare | Always specify the interpolation format explicitly when the variable is multi-select |
| "The variable works in the UI so the interpolation must be correct" | It may work with a single selection but break with multiple selections. The default interpolation for single values happens to work, masking the missing format specification | Test with multiple values selected to verify the interpolation format produces valid query syntax |
| "I'll create the variable and fix the chain order later" | Variables that appear to work in isolation will return wrong results when chaining is broken, and the bug is subtle — dashboards show data, just unfiltered data | Get the dependency order right before creating any variables |
这些看似合理的捷径会导致实际故障:
| 错误的自我说服 | 错误原因 | 必要操作 |
|---|---|---|
| “变量顺序无关紧要,最终都会解析” | Perses严格按数组顺序评估变量,而非依赖图。出现在父变量之前的子变量在首次加载时会解析为空父值 | 映射完整的依赖链并确认数组顺序与依赖链一致 |
“我直接使用 | 裸 | 当变量为多选择时,始终显式指定插值格式 |
| “变量在UI中可用,说明插值配置正确” | 单选择时可能正常工作,但多选择时会失效。单值的默认插值恰好能工作,掩盖了格式缺失的问题 | 选择多个值进行测试,验证插值格式生成的查询语法有效 |
| “我先创建变量,之后再修复依赖顺序” | 单独使用时看似正常的变量,在依赖链断裂时会返回错误结果,且bug非常隐蔽 — 仪表板显示数据,但数据未过滤 | 在创建任何变量之前,先确保依赖顺序正确 |
FORBIDDEN Patterns
禁止模式
These patterns MUST NOT appear in any variable configuration produced by this skill:
- NEVER define a child variable before its parent in the variables array — this silently breaks filtering
- NEVER use in a Prometheus
${var:csv}or=~matcher — use!~instead${var:regex} - NEVER hardcode label values in a ListVariable when the values come from Prometheus — use PrometheusLabelValuesVariable or PrometheusPromQLVariable instead
- NEVER create a variable with without verifying that all consuming queries use an appropriate multi-value interpolation format
allowMultiple: true - NEVER omit the field in a Prometheus variable plugin — Perses will not infer it and the variable will fail to resolve
datasource
本技能生成的任何变量配置中严禁出现以下模式:
- 绝对禁止在变量数组中将子变量定义于父变量之前 — 这会静默破坏过滤功能
- 绝对禁止在Prometheus 或
=~匹配器中使用!~— 请改用${var:csv}${var:regex} - 绝对禁止当值来自Prometheus时,在ListVariable中硬编码标签值 — 请改用PrometheusLabelValuesVariable或PrometheusPromQLVariable
- 绝对禁止创建的变量时,未验证所有消费查询使用了合适的多值插值格式
allowMultiple: true - 绝对禁止在Prometheus变量插件中省略字段 — Perses不会自动推断,变量将无法解析
datasource
Blocker Criteria
阻塞条件
Do NOT proceed past each phase gate if any of these conditions exist:
Phase 1 Blockers:
- Variable dependency chain is circular (A -> B -> A) — restructure the chain
- Requested label does not exist in the target Prometheus — verify with
curl -s http://<prometheus>/api/v1/labels - No datasource of the required type is configured in the target project or globally
Phase 2 Blockers:
- or MCP tool returns an error — fix before proceeding
percli apply - Variable name conflicts with an existing variable at the same scope
- Matchers reference a parent variable that does not exist or is misspelled
Phase 3 Blockers:
- Variable list command does not show the created variable — creation failed silently
- Variable chain produces unfiltered results when parent is selected — dependency order or matcher syntax is wrong
- Variable dropdown is empty — plugin configuration, datasource, or label name is incorrect
若存在以下任一情况,请勿进入下一阶段:
阶段1阻塞条件:
- 变量依赖链存在循环(A -> B -> A) — 重构依赖链
- 请求的标签在目标Prometheus中不存在 — 使用验证
curl -s http://<prometheus>/api/v1/labels - 目标项目或全局未配置所需类型的数据源
阶段2阻塞条件:
- 或MCP工具返回错误 — 修复后再继续
percli apply - 变量名称与同一范围的现有变量冲突
- 匹配器引用了不存在或拼写错误的父变量
阶段3阻塞条件:
- 变量列表命令未显示已创建的变量 — 创建已静默失败
- 选择父变量时,变量链返回未过滤的结果 — 依赖顺序或匹配器语法错误
- 变量下拉菜单为空 — 插件配置、数据源或标签名称不正确
References
参考资料
| Resource | URL | Use For |
|---|---|---|
| Perses Variable Documentation | https://perses.dev/docs/user-guides/variables/ | Variable types, scopes, and configuration |
| Perses Variable Spec (Go types) | https://github.com/perses/perses/tree/main/pkg/model/api/v1/variable | Authoritative field definitions for variable specs |
| Perses Plugin List | https://github.com/perses/plugins | Available variable plugins and their spec schemas |
| Perses MCP Server | https://github.com/perses/perses-mcp-server | MCP tool reference for variable CRUD operations |
| Interpolation Formats Source | https://github.com/perses/perses/tree/main/internal/api/variable | Implementation of all interpolation format handlers |
| percli CLI Reference | https://perses.dev/docs/user-guides/percli/ | CLI commands for variable management |
| 资源 | URL | 用途 |
|---|---|---|
| Perses 变量文档 | https://perses.dev/docs/user-guides/variables/ | 变量类型、范围和配置 |
| Perses 变量规范(Go类型) | https://github.com/perses/perses/tree/main/pkg/model/api/v1/variable | 变量规范的权威字段定义 |
| Perses 插件列表 | https://github.com/perses/plugins | 可用的变量插件及其规范 schema |
| Perses MCP 服务器 | https://github.com/perses/perses-mcp-server | 变量CRUD操作的MCP工具参考 |
| 插值格式源码 | https://github.com/perses/perses/tree/main/internal/api/variable | 所有插值格式处理器的实现 |
| percli CLI 参考 | https://perses.dev/docs/user-guides/percli/ | 变量管理的CLI命令 |