You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Azure Monitor Java SDK创建Alert Processing Rule

解决Azure Monitor Java SDK创建Alert Processing Rule的实现路径问题

目前Azure官方的Management SDK for Java暂未提供Alert Processing Rule的专属操作类,可通过调用Azure REST API的方式实现编程创建,步骤如下:

1. 复用现有身份验证逻辑

已配置的Azure AD凭据(如ClientSecretCredential)可直接用于获取请求令牌,调用getToken方法获取针对https://management.azure.com/范围的Bearer Token,示例代码:

ClientSecretCredential credential = new ClientSecretCredentialBuilder()
    .tenantId(tenantId)
    .clientId(clientId)
    .clientSecret(clientSecret)
    .build();

AccessToken token = credential.getToken(new TokenRequestContext().addScopes("https://management.azure.com/.default")).block();
String bearerToken = token.getToken();

2. 构造Alert Processing Rule请求体

按照Azure REST API规范构造JSON请求体,需包含规则作用范围、筛选条件、执行动作等核心字段,示例结构:

{
  "properties": {
    "scopes": ["/subscriptions/{你的订阅ID}/resourceGroups/{你的资源组名}"],
    "filter": {
      "alertRuleIds": ["/subscriptions/{你的订阅ID}/resourceGroups/{你的资源组名}/providers/Microsoft.AlertsManagement/alertRules/{目标警报规则名}"],
      "severities": ["Sev1", "Sev2"],
      "monitorService": "Platform"
    },
    "actions": [
      {
        "actionType": "SuppressActions",
        "suppressionConfig": {
          "recurrenceType": "Daily",
          "schedule": {
            "startTime": "00:00:00",
            "endTime": "08:00:00",
            "timeZone": "UTC"
          }
        }
      }
    ],
    "enabled": true
  },
  "location": "global"
}

3. 发送REST API请求

使用Java HTTP客户端(如OkHttp、JDK HttpClient)发送PUT请求到指定API端点:

  • 请求URL:https://management.azure.com/subscriptions/{你的订阅ID}/resourceGroups/{你的资源组名}/providers/Microsoft.AlertsManagement/actionRules/{你的规则名}?api-version=2021-08-01
  • 请求头需包含:
    • Authorization: Bearer {获取到的Bearer Token}
    • Content-Type: application/json

4. 响应处理

检查HTTP响应状态码,201 Created或200 OK表示规则创建/更新成功,可解析响应体确认规则详情。

注意事项

  • 确保用于身份验证的服务主体拥有Microsoft.AlertsManagement/actionRules/write权限(可分配「Alert Processing Rule Contributor」角色)
  • 需使用官方文档指定的稳定版API版本,避免使用预览版API
  • Alert Processing Rule属于全局资源,请求体中location必须设置为global

内容的提问来源于stack exchange,提问作者lpdevlop

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 20:52:15