AWS SDK:获取CloudWatchLogs:PutLogEvents API使用的调用次数

huangapple go评论57阅读模式
英文:

AWS SDK: Get CallCount of CloudWatchLogs:PutLogEvents API Usage

问题

我在CloudWatch中有一个警报,它获取了资源类型为API和服务Logs(CloudWatchLogs)的CallCount指标,并且命名空间为AWS/Usage。

可以将其与此资源的限制进行比较(https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/cloudwatch_limits_cwl.html)

  • PutLogEvents请求的最大批处理大小为1MB。
  • 每个帐户每个区域每秒最多800个事务,但以下区域的配额为每个帐户每个区域每秒1500个事务:美国东部(北弗吉尼亚州)、美国西部(俄勒冈州)和欧洲(爱尔兰)。您可以使用Service Quotas服务请求增加每秒节流配额。

是否有一种方法可以使用SDK获取此数据?

我尝试了Aws\CloudWatch\CloudWatchClient和getMetricStatistics。这是否是正确的方法?

有什么建议吗?

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; // 秒。 (5分钟= 300秒。)
$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;

我得到了以下数据....

模型数据
----------
可以使用模型对象的get()方法从模型中检索数据(例如,`$result->get($key)`)或"像访问结果一样使用关联数组(例如`$result['key']`)。还可以使用search()方法在结果数据上执行JMESPath表达式。

{
    "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": [
                []
            ]
        }
    }
}

由于

英文:

I have an Alert in CloudWatch that get the metric CallCount for resource PutLogEvents type API and service Logs (CloudWatchLogs) on the namespace AWS/Usage.

It is useful to compare it with the limit of this resource (https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/cloudwatch_limits_cwl.html)

The maximum batch size of a PutLogEvents request is 1MB.

800 transactions per second per account per Region, except for the
following Regions where the quota is 1500 transactions per second
per account per Region: US East (N. Virginia), US West (Oregon),
and Europe (Ireland). You can request an increase to the per-second
throttling quota by using the Service Quotas service.

There is any way to get this data using the SDK?

I'm trying with Aws\CloudWatch\CloudWatchClient and getMetricStatistics. Is that the correct way?

Any advice?

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;

I got ....

Model Data
----------
Data can be retrieved from the model object using the get() method of the
model (e.g., `$result->get($key)`) or "accessing the result like an
associative array (e.g. `$result['key']`). You can also execute JMESPath expressions on the result data using the search() method.

{
    "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": [
                []
            ]
        }
    }
}

Since

答案1

得分: 0

GetMetricStatistics 允许您获取计算的统计信息,例如最大值、最小值、平均值,但也有可能更适合您的是 GetMetricData此示例 尚未在 PHP 中提供,但如果您熟悉 C#、Python 或Java,它会在可运行的示例中演示了几种度量操作。

英文:

GetMetricStatistics lets you fetch calculated statistics, such as max, min, average but there is also GetMetricData which might be more what you need. This example is not yet available in PHP, but if you are familiar with C#, python or Java, it demonstrates several of the metric operations in a runnable example.

huangapple
  • 本文由 发表于 2023年7月13日 21:39:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680042.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定