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

如何通过AWS SDK获取CloudWatchLogs:PutLogEvents的CallCount指标?

关于通过AWS PHP SDK获取CloudWatch PutLogEvents调用量指标的问题

背景

我在CloudWatch中配置了告警,用来获取AWS/Usage命名空间下,Logs(CloudWatchLogs)服务的PutLogEvents类型API的CallCount指标。将该指标和资源配额对比很实用:

  • PutLogEvents请求最大批量大小为1MB
  • 美国东部(弗吉尼亚北部)、美国西部(俄勒冈)和欧洲(爱尔兰)区域,每秒每账户配额为1500次事务;其他区域为每秒800次,可通过Service Quotas申请提额

问题

我能否通过SDK获取这些数据?用Aws\CloudWatch\CloudWatchClient的getMetricStatistics方法是正确的方式吗?有什么建议?

我的PHP代码

require __DIR__ . '/../vendor/autoload.php';

use Aws\CloudWatch\CloudWatchClient; 
use Aws\Exception\AwsException;

$cloudWatchClient = new CloudWatchClient([
    'version' => 'latest',
    'region' => 'eu-west-1',
    'credentials' => [
        'key'    => '*******************',
        'secret' => '*******************',
    ]
]);

$dimensions = [
    [
        'Name' => 'Service',
        'Value' => 'Logs'
    ],
    [
        'Name' => 'Resource',
        'Value'=> 'PutLogEvents'
    ],
    [
        'Name' => 'Type',
        'Value' => 'API'
    ]
];


$startTime = strtotime('-3 hours');
$endTime = strtotime('now');
$period = 300; // Seconds. (5 minutes = 300 seconds.)
$statistics = array('Average');
$unit = 'None';


$result = $cloudWatchClient->getMetricStatistics([
    'Namespace' => 'AWS/Usage',
    'MetricName' => 'CallCount',
    'Dimensions' => $dimensions,
    'StartTime' => $startTime,
    'EndTime' => $endTime,
    'Period' => $period,
    'Statistics' => $statistics,
    'Unit' => $unit
]);

echo $result;

返回结果

{
    "Label": "CallCount",
    "Datapoints": [
        {
            "Timestamp": "2023-07-13T14:12:00+00:00",
            "Sum": 8014,
            "Unit": "None"
        },
        {
            "Timestamp": "2023-07-13T14:16:00+00:00",
            "Sum": 7517,
            "Unit": "None"
        },
        {  
            "Timestamp": "2023-07-13T14:15:00+00:00",
            "Sum": 8045,
            "Unit": "None"
        },
        {
             "Timestamp": "2023-07-13T14:14:00+00:00",
             "Sum": 8044,
             "Unit": "None"
         },
         {
            "Timestamp": "2023-07-13T14:13:00+00:00",
            "Sum": 8012,
            "Unit": "None"
         }
    ],
    "@metadata": {
        "statusCode": 200,
        "effectiveUri": "https:\/\/monitoring.eu-west-1.amazonaws.com",
        "headers": {
            "x-amzn-requestid": "54ebf1ed-0d9d-4694-bbdc-50fcd509a2a3",
            "content-type": "text\/xml",
            "content-length": "332",
            "date": "Thu, 13 Jul 2023 13:58:19 GMT"
        },
        "transferStats": {
            "http": [
                []
            ]
        }
    }
}

解答

  1. 使用getMetricStatistics是正确方式:从返回结果来看,请求已成功获取到PutLogEvents的CallCount数据,说明方法的使用方向没问题。
  2. 优化建议:
    • 统计值调整:你请求的是Average,但返回的是Sum——因为CloudWatch对AWS/Usage下的CallCount默认记录Sum值。若要计算每秒调用量,用Sum除以周期(如300秒)就能得到平均每秒调用数,再和对应区域配额(eu-west-1为1500次/秒)对比。
    • 数据排序:返回的Datapoints是无序的,建议按Timestamp排序,方便后续分析。
    • 异常捕获:添加try-catch块捕获AwsException,处理权限不足、区域错误等请求失败场景。
    • 凭证安全:不要硬编码Access Key和Secret,改用IAM角色、环境变量或AWS凭证文件管理,提升安全性。
    • API版本固化:version设为latest存在兼容性风险,建议指定具体版本(如2010-08-01),保障代码稳定性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.16 13:07:43