Search code examples
amazon-web-servicessdkamazon-cloudwatchamazon-cloudwatchlogs

AWS SDK: Get CallCount of CloudWatchLogs:PutLogEvents API Usage


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


Solution

  • 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.