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

GCP监控控制台创建MQL告警策略保存失败,提示无效参数

GCP监控MQL告警策略保存报错解决方案

问题描述

在GCP监控控制台创建基于MQL的告警策略时,保存持续报错:Error: Unable to save alerting policy. Request contains an invalid argument.,但MQL查询在编辑器中无报错且能正常展示预期图表,对应的策略JSON配置如下:

{
  "displayName": "kube_deployment_replicas_mismatch",
  "documentation": {
    "content": "The expected number of replicas have not been available for 15 minutes or longer",
    "mimeType": "text/markdown"
  },
  "userLabels": {
    "type": "application"
  },
  "conditions": [
    {
      "displayName": "kube_deployment_replicas_mismatch",
      "conditionMonitoringQueryLanguage": {
        "duration": "900s",
        "trigger": {
          "count": 1
        },
        "evaluationMissingData": "EVALUATION_MISSING_DATA_INACTIVE",
        "query": "{ kubernetes.io/anthos/kube_deployment_spec_replicas
; kubernetes.io/anthos/kube_deployment_status_replicas_available }
| [metric.deployment]
| ratio
| condition val() != 1"
      }
    }
  ],
  "alertStrategy": {
    "autoClose": "604800s"
  },
  "combiner": "OR",
  "enabled": true,
  "notificationChannels": [
    "projects/xxxxxxxxx/notificationChannels/xxxxxxxxxxx"
  ]
}

解决步骤

1. 修复MQL查询的JSON格式问题

JSON字符串中的换行符必须转义为\n,否则会导致请求参数格式无效。原配置中query字段的多行写法未做转义,这是核心问题。修改后的query有两种写法:

  • 单行精简版:
    "query": "{kubernetes.io/anthos/kube_deployment_spec_replicas;kubernetes.io/anthos/kube_deployment_status_replicas_available}|[metric.deployment]|ratio|condition val() != 1"
    
  • 转义换行的多行版:
    "query": "{ kubernetes.io/anthos/kube_deployment_spec_replicas\n; kubernetes.io/anthos/kube_deployment_status_replicas_available }\n| [metric.deployment]\n| ratio\n| condition val() != 1"
    

2. 完整修改后的策略JSON

{
  "displayName": "kube_deployment_replicas_mismatch",
  "documentation": {
    "content": "预期副本数未正常可用已达15分钟或更久",
    "mimeType": "text/markdown"
  },
  "userLabels": {
    "type": "application"
  },
  "conditions": [
    {
      "displayName": "kube_deployment_replicas_mismatch",
      "conditionMonitoringQueryLanguage": {
        "duration": "900s",
        "trigger": {
          "count": 1
        },
        "evaluationMissingData": "EVALUATION_MISSING_DATA_INACTIVE",
        "query": "{kubernetes.io/anthos/kube_deployment_spec_replicas;kubernetes.io/anthos/kube_deployment_status_replicas_available}|[metric.deployment]|ratio|condition val() != 1"
      }
    }
  ],
  "alertStrategy": {
    "autoClose": "604800s"
  },
  "combiner": "OR",
  "enabled": true,
  "notificationChannels": [
    "projects/xxxxxxxxx/notificationChannels/xxxxxxxxxxx"
  ]
}

3. 额外检查项

  • 确认notificationChannels中的通道ID有效,可在GCP监控的通知通道列表中核实
  • 若问题仍存在,可尝试在MQL编辑器中重新生成查询后,直接通过控制台保存,避免手动修改JSON时引入格式错误

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.26 12:53:34