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

AWS OpenSearch集群CloudWatch监控3xx等指标Id格式报错求助

问题

通过cURL调用CloudWatch获取AWS OpenSearch集群监控指标时,大部分指标正常,但3xx:Count、4xx:Count、5xx:Count三个指标报错,提示MetricDataQueries.member.xx.Id的值不符合正则模式^[a-z][a-zA-Z0-9]*$。

脚本示例

var metrics_list = [
    'CPUUtilization:Percent',
    'MasterCPUUtilization:Percent',
    'ClusterStatus.green:Count',
    'ClusterStatus.yellow:Count',
    'ClusterStatus.red:Count',
    'SearchableDocuments:Count',
    'DiskQueueDepth:Count',
    'WriteThroughput:Bytes/Second',
    'ReadThroughput:Bytes/Second',
    'SearchLatency:Milliseconds',
    'WriteLatency:Seconds',
    'ReadLatency:Seconds',
    'IndexingLatency:Milliseconds',
    'SearchRate:Count',
    'WriteIOPS:Count/Second',
    'ReadIOPS:Count/Second',
    'FreeStorageSpace:Megabytes',
    '3xx:Count',
    '4xx:Count',
    '5xx:Count'
];

var metric_payload = [];
metrics_list.forEach(function(metric) {
    var parts = metric.split(':', 2);
    var name = parts[0].replace(/[^a-zA-Z0-9]/g, '');
    metric_payload.push({
        'Id': name.charAt(0).toLowerCase() + name.slice(1),
        'MetricStat': {
            'Metric': {
                'MetricName': parts[0],
                'Namespace': 'AWS/ES',
                'Dimensions': [{
                        'Name': 'ClientId',
                        'Value': 'xxxxxxxxxxx'
                    },
                    {
                        'Name': 'DomainName',
                        'Value': os_instance_id
                    }
                ]
            },
            'Period': period,
            'Stat': 'Average',
            'Unit': parts[1]
        }
    });
});

错误信息

{
  "error": "Request failed with status code 400",
  "type": "com.amazon.coral.validate#ValidationException",
  "message": "The value 3xx for parameter MetricDataQueries.member.18.Id is not matching the expected pattern ^[a-z][a-zA-Z0-9]*$.
The value 4xx for parameter MetricDataQueries.member.19.Id is not matching the expected pattern ^[a-z][a-zA-Z0-9]$.
The value 5xx for parameter MetricDataQueries.member.20.Id is not matching the expected pattern ^[a-z][a-zA-Z0-9_]$."
}
解决方案

错误原因

CloudWatch要求MetricDataQueries中的Id必须满足:以小写字母开头,后续只能包含大小写字母或数字。而3xx、4xx、5xx处理后得到的名称仍以数字开头,违反了正则规则。

修改后的脚本

只需在生成Id前,将以数字开头的指标名称替换为字母开头的标识即可,比如把数字映射为对应英文小写:

var metrics_list = [
    'CPUUtilization:Percent',
    'MasterCPUUtilization:Percent',
    'ClusterStatus.green:Count',
    'ClusterStatus.yellow:Count',
    'ClusterStatus.red:Count',
    'SearchableDocuments:Count',
    'DiskQueueDepth:Count',
    'WriteThroughput:Bytes/Second',
    'ReadThroughput:Bytes/Second',
    'SearchLatency:Milliseconds',
    'WriteLatency:Seconds',
    'ReadLatency:Seconds',
    'IndexingLatency:Milliseconds',
    'SearchRate:Count',
    'WriteIOPS:Count/Second',
    'ReadIOPS:Count/Second',
    'FreeStorageSpace:Megabytes',
    '3xx:Count',
    '4xx:Count',
    '5xx:Count'
];

var metric_payload = [];
// 映射数字到对应英文小写,确保Id以字母开头
var numToWord = {
    '3': 'three',
    '4': 'four',
    '5': 'five'
};

metrics_list.forEach(function(metric) {
    var parts = metric.split(':', 2);
    var name = parts[0].replace(/[^a-zA-Z0-9]/g, '');
    
    // 处理以数字开头的情况
    if (numToWord[name[0]]) {
        name = numToWord[name[0]] + name.slice(1);
    }
    
    metric_payload.push({
        'Id': name.charAt(0).toLowerCase() + name.slice(1),
        'MetricStat': {
            'Metric': {
                'MetricName': parts[0],
                'Namespace': 'AWS/ES',
                'Dimensions': [{
                        'Name': 'ClientId',
                        'Value': 'xxxxxxxxxxx'
                    },
                    {
                        'Name': 'DomainName',
                        'Value': os_instance_id
                    }
                ]
            },
            'Period': period,
            'Stat': 'Average',
            'Unit': parts[1]
        }
    });
});

说明

修改后,三个指标的Id会被转换为threexx、fourxx、fivexx,完全符合CloudWatch的正则要求,同时MetricName仍保留原始的3xx/4xx/5xx,不会影响指标的正常获取。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 09:01:25