Skip to content

关于本页

本页介绍更夫所基于的开源内核 Keep 的通用能力。更夫在其上提供国内化增强:钉钉/企业微信/飞书通知、国产大模型接入、LDAP 认证与值班管理。部署与上手请看快速上手。

Keep 工作流中的上下文允许您跨工作流的不同部分动态引用和利用数据。上下文变量使您能够访问运行时数据,例如告警详情先前步骤或动作的结果,以及在工作流中定义的常量。

此功能使工作流具有灵活性、可重用性,能够动态处理复杂场景。


访问上下文 ​

上下文变量可以使用大括号({{ }})访问。您可以直接在触发器、步骤和动作中使用这些变量。上下文包括:

  1. 告警数据:访问触发工作流的告警中的数据。
  2. 事件数据:如果工作流是基于事件的,您可以访问事件的属性。
  3. 步骤和动作结果:使用其唯一 ID 检索先前步骤或动作产生的数据。

告警数据 ​

您可以在工作流中的任何位置访问告警的属性:

yaml
message: "Alert triggered: {{ alert.name }} - Severity: {{ alert.severity }}"

事件数据 ​

对于事件工作流,访问事件相关的上下文:

yaml
if: "{{ incident.current_tier == 1 }}"

步骤结果 ​

访问先前步骤的结果:

yaml
message: "Query results: {{ steps.get-max-datetime.results }}"

动作结果 ​

从已完成的动作中检索数据:

yaml
if: "{{ actions.trigger-email.results.success }}"

常量 ​

在工作流中定义可重用的值并访问它们:

yaml
consts:
  alert_message: "Critical system alert!"
  escalation_policy: "tier-1"
  slack_channels:
    sre_team: CH00001
    payments_team: CH00002
actions:
  - name: notify-slack
    if: "{{alert.source}} == 'datadog'"
    provider:
      type: slack
      config: "{{ providers.slack }}"
      with:
        channel: "{{ consts.slack_channels.sre_team }}"
        message: "{{ consts.alert_message }}"

在循环中使用上下文 ​

当在 foreach 循环中迭代数据时,上下文提供 foreach.value 作为当前迭代。

例如:

yaml
steps:
  - name: get-alerts
    provider:
      type: keep
      with:
        query: "status == 'firing'"

actions:
  - name: notify-on-alerts
    foreach: "{{ steps.get-alerts.results }}"
    provider:
      type: slack
      with:
        message: "Alert: {{ foreach.value.name }} is firing!"

上下文使用示例 ​

动态动作执行 ​

使用上下文有条件地触发动作:

yaml
actions:
  - name: escalate-alert
    if: "{{ alert.severity == 'critical' }}"
    provider:
      type: slack
      with:
        message: "Critical alert: {{ alert.name }}"

增强告警 ​

您可以使用步骤的结果来增强告警

yaml
steps:
  - name: fetch-customer-details
    provider:
      type: mysql
      with:
        query: "SELECT * FROM customers WHERE id = '{{ alert.customer_id }}'"
        single_row: true

actions:
  - name: enrich-alert
    provider:
      type: mock
      with:
        enrich_alert:
          - key: customer_name
            value: "{{ steps.fetch-customer-details.results.name }}"

基于步骤结果的条件逻辑 ​

yaml
actions:
  - name: trigger-slack
    if: "{{ steps.get-pods.results.0.status.phase == 'Running' }}"
    provider:
      type: slack
      with:
        message: "Pod is running: {{ steps.get-pods.results.0.metadata.name }}"

基于 MIT 协议开源的 Keep · 由更夫社区维护