本文介绍告警资源数据的数据结构。

简介

告警资源数据包括用户、用户组、值班组、Webhook集成、告警策略(路由合并、抑制、静默)、行动策略、内容模板、自定义的其它资源数据等。

告警资源数据分为资源(Resource)和记录(Record)两类。例如用户本身属于一种资源,但是配置的每一条用户信息都是一条记录。

分类属性说明
资源(Resource)resource_name标识资源的名称,例如用户的资源名称为sls.common.user。
记录(Record)id该记录的唯一ID。字符串类型。
tag为该记录打标签。字符串类型。
value该记录完整的值。JSON字符串类型。

告警资源数据的结构

资源类型resource_name字段record_id字段record_tag字段示例备注
用户sls.common.useruser_iduser_name
{
    "user_id": "alex",
    "user_name": "Alex",
    "email": [
        "***@example.com"
    ],
    "country_code": "86",
    "phone": "133****3333",
    "enabled": true,
    "sms_enabled": true,
    "voice_enabled": true
}
用户组sls.common.user_groupuser_group_iduser_group_name
{
    "user_group_id": "devops",
    "user_group_name": "DevOps Team",
    "enabled": true,
    "members": [
        "alex"
    ]
}
值班组sls.alert.oncall_grouponcall_idoncall_name
{
    "oncall_id": "default_oncall",
    "oncall_name": "Default Oncall",
    "enabled": true,
    "overrides": [],
    "rotations": [
        {
            "targets": [
                {
                    "type": "user",
                    "target_id": "alex"
                },
                {
                    "type": "user_group",
                    "target_id": "devops"
                }
            ],
            "end_time": 0,
            "shift_day": "",
            "shift_time": "12:00",
            "shift_type": "day",
            "start_time": 1633017600,
            "shift_minute": 0,
            "end_time_type": "none",
            "shift_interval": 1,
            "shift_week_custom": null,
            "restriction_date_type": "workday",
            "restriction_time_type": "allday",
            "restriction_week_range": null,
            "restriction_time_custom_range": null
        }
    ],
    "calendar_id": "default_calendar"
}
配置复杂,建议通过控制台完成配置。
Webhook集成sls.alert.action_webhookidname
{
    "id": "custom-webhook",
    "name": "Custom Webhook",
    "type": "custom",
    "url": "http://localhost:9099/data/webhook",
    "method": "POST",
    "headers": [
        {
            "key": "Content-Type",
            "value": "application/json"
        },
        {
            "key": "Foo",
            "value": "bar"
        }
    ]
}
{
    "id": "dingtalk",
    "name": "Dingtalk Webhook",
    "type": "dingtalk",
    "url": "https://oapi.dingtalk.com/robot/send?access_token=**********",
    "method": "POST",
    "secret": "SEC**********",
    "headers": []
}
  • type为Webhook类型,可选值如下:
    • dingtalk:钉钉。
    • wechat:企业微信。
    • lark:飞书。
    • slack:Slack。
    • custom:自定义Webhook。
  • 除了custom类型外,其余类型所涉及的method固定为POST,headers为空数组。
  • type为钉钉或飞书,且在钉钉或飞书侧设置安全校验方式为签名校验时,需要添加secret字段。

    您可以在钉钉或飞书机器人管理界面获取加签密钥。

告警策略sls.alert.alert_policypolicy_idpolicy_name
{
    "policy_id": "sls.builtin",
    "policy_name": "SLS Builtin Alert Policy",
    "parent_id": "sls.root",
    "is_default": false,
    "group_script": "fire(action_policy=\"sls.builtin\", group={\"project\": \"__a__\", \"uid\": alert.aliuid}, group_wait=\"5s\", group_interval=\"2m\", repeat_interval=\"2m\")\nstop()\nfire(action_policy=\"sls.builtin\", group={\"alert_id\": alert.alert_id}, group_wait=\"5s\", group_interval=\"10s\", repeat_interval=\"2m\")\nif alert.labels.name ~= \"^\\\\w+s$\":\n\tfire(action_policy=\"sls.builtin\", group={\"product\": \"xxs\"}, group_wait=\"5s\", group_interval=\"10s\", repeat_interval=\"2m\")\n\tstop()\nstop()\nfire(action_policy=\"sls.builtin\", group={\"label_name\": alert.labels.name}, group_wait=\"10s\", group_interval=\"10s\", repeat_interval=\"2m\")",
    "inhibit_script": "if alert.severity >= 8:\n    silence alert.severity < 6",
    "silence_script": ""
}
  • is_default固定设置为false即可。
  • group_script为路由合并策略。
  • inhibit_script为抑制策略。
  • silence_script为静默策略。
  • 通过SDK配置时,group_scriptinhibit_scriptsilence_script只包含DSL脚本信息,无UI配置信息,因此控制台上无法看到相应的图形结构。
行动策略sls.alert.action_policyaction_policy_idaction_policy_name
{
    "action_policy_id": "sls.builtin",
    "action_policy_name": "SLS Builtin Action Policy",
    "labels": {},
    "is_default": false,
    "primary_policy_script": "fire(type=\"webhook_integration\", integration_type=\"dingtalk\", webhook_id=\"dingtalk-test\", template_id=\"default-template\", period=\"any\")",
    "secondary_policy_script": "fire(type=\"voice\", users=[\"jizhi\"], groups=[\"group-jizhi\"], template_id=\"default-template\")",
    "escalation_start_enabled": false,
    "escalation_start_timeout": "10s",
    "escalation_inprogress_enabled": false,
    "escalation_inprogress_timeout": "10s",
    "escalation_enabled": false,
    "escalation_timeout": "4h0m0s"
}
  • is_default固定设置为false即可。
  • labels为保留参数,固定为空对象即可。
  • primary_policy_script为第一行动策略。
  • secondary_policy_script为第二行动策略。
  • escalation_*为控制第二行动策略是否开启的配置,可参见控制台中的配置项。
  • 通过SDK配置时,primary_policy_scriptsecondary_policy_script只包含DSL脚本信息,无UI配置信息,因此控制台上无法看到相应的图形结构。
内容模板sls.alert.content_templatetemplate_idtemplate_name
{
    "template_id": "default-template",
    "template_name": "Default Template",
    "is_default": false,
    "templates": {
        "fc": {
            "limit": 0,
            "locale": "zh-CN",
            "content": "",
            "send_type": "merged"
        },
        "sms": {
            "locale": "zh-CN",
            "content": ""
        },
        "lark": {
            "title": "Alerthub告警测试 ${alert_name}",
            "locale": "zh-CN",
            "content": ""
        },
        "email": {
            "locale": "zh-CN",
            "content": "",
            "subject": "SLS告警测试-jizhi-test"
        },
        "slack": {
            "title": "Alerthub告警测试 ${alert_name}",
            "locale": "zh-CN",
            "content": ""
        },
        "voice": {
            "locale": "zh-CN",
            "content": ""
        },
        "wechat": {
            "title": "Alerthub告警测试 ${alert_name}",
            "locale": "zh-CN",
            "content": ""
        },
        "webhook": {
            "limit": 0,
            "locale": "zh-CN",
            "content": "",
            "send_type": "batch"
        },
        "dingtalk": {
            "title": "Alerthub告警测试 ${alert_name}",
            "locale": "zh-CN",
            "content": ""
        },
        "event_bridge": {
            "locale": "zh-CN",
            "content": "",
            "subject": "wkb-test"
        },
        "message_center": {
            "locale": "zh-CN",
            "content": ""
        }
    }
}
  • is_default固定设置为false即可。
  • templates中包含每个渠道的模板配置,如果某个渠道的content值为空,则表示使用系统默认的模板。更多信,请参见默认内容模板
  • locale取值为zh-CN或en-US。
  • webhook和fc渠道的send_type取值为single或者batch,表示逐条通知或者合并通知。
默认日历sls.common.calendarcalendar_idcalendar_name
{
    "calendar_id": "default_calendar",
    "calendar_name": "Default Calendar",
    "timezone": "Asia/Shanghai",
    "workdays": [
        1,
        2,
        3,
        4,
        5
    ],
    "worktime": [
        {
            "end_time": "21:00",
            "start_time": "09:00"
        }
    ],
    "reset_days": [],
    "holiday_sync": "china"
}
  • 默认日历的ID固定为 default_calendar。
  • 配置复杂,建议通过控制台完成配置。

DSL语法说明

简单的DSL配置示例如下所示。

重要 DSL配置复杂,建议您先在控制台完成配置,然后查看Network请求所发送的Payload内容。
设置路由合并策略
  • 控制台配置告警策略
  • DSL配置
    if alert.severity >= 8:
        fire(action_policy="sls.builtin", group={"alert.project": alert.project, "alert.alert_id": alert.alert_id}, group_by_all_labels=true, group_wait="15s", group_interval="5m", repeat_interval="1h")
        stop()
    fire(action_policy="sls.builtin", group={"alert.project": alert.project}, group_by_all_labels=false, group_wait="15s", group_interval="5m", repeat_interval="1h")

    基本语法说明如下:

    • if alert.severity >= 8为条件节点。
    • fire( )为路由合并节点。
      • action_policy为行动策略ID。
      • group为合并基准,dict类型。
      • group_by_all_labels为是否根据所有标签合并,取值为true、false。
      • group_wait为首次等待时间。
      • group_interval为变化等待时间。
      • repeat_interval为重复等待时间。
    • stop()为结束节点。
设置抑制策略
  • 控制台配置告警策略
  • DSL配置
    if alert.severity >= 8:
        silence alert.severity < 8
        stop()
    基本语法说明如下:
    • if alert.severity >= 8为条件节点。
    • silence alert.severity < 8为抑制节点。
    • stop()为结束节点。
设置静默策略
  • 控制台配置告警策略
  • DSL配置
    drop(end=1638189177) alert.alert_name == "test-alert"
    accept alert.labels.env == "prod"
    基本语法说明如下:
    • drop表示静默。
    • accept表示不静默。
    • startend表示时间范围,Unix时间戳(单位为秒)。
      • 特定时间范围:需设置startend
      • 特定时间为止:只需设置end
      • 一段时间:只需设置end
      • 持续:不需要设置startend
设置行动策略
  • 控制台配置行动策略
  • DSL配置
    if alert.severity >= 8:
        fire(type="sms", users=[], groups=[], oncall_groups=["alert-test-oncall"], template_id="sls.builtin.cn", check_quota="true", period="any")
        stop()
    if alerts.severity < 8:
        fire(type="webhook_integration", integration_type="dingtalk", webhook_id="jizhi-test", template_id="sls.builtin.cn", period="any")
        stop()
    重要
    • if alert.xxx表示任意告警满足条件。
    • if alerts.xxx表示所有告警满足条件。
其中,每个渠道的配置示例如下:
  • 控制台配置行动策略
  • DSL配置
    fire(type="sms", users=["jizhi"], groups=[], oncall_groups=[], template_id="sls.builtin.cn", check_quota="true", period="workday")
    fire(type="voice", users=[], groups=[], oncall_groups=["alert-test-oncall"], template_id="sls.builtin.cn", check_quota="true", period="worktime")
    fire(type="email", users=[], groups=["alert-test"], oncall_groups=[], template_id="sls.builtin.cn", check_quota="true", period="any")
    fire(type="webhook_integration", integration_type="dingtalk", webhook_id="jizhi-test", template_id="sls.builtin.cn", period="any")
    fire(type="webhook_integration", integration_type="wechat", webhook_id="wechat-test", template_id="sls.builtin.cn", period="any")
    fire(type="message_center", template_id="sls.builtin.cn", check_quota="true", period="any")
    fire(type="event_bridge", region="cn-hangzhou", bus_name="bus-demo", template_id="sls.builtin.cn", period="any")
    fire(type="fc", region="cn-shanghai", service="jizhi-test", qualifier="LATEST", function="sls-ops-1", template_id="sls.builtin.cn", period="any")
    fire(type="set_ticket_owner", choice="random", users=[], groups=[], oncall_groups=["alert-test-oncall"])

    基本语法说明如下:

    通过fire(),设置通知渠道。更多信息,请参见通知渠道说明