Skip to content

Commit

Permalink
Fix: Support for max event size limit (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
amandas-sukionis authored and maxbanton committed May 9, 2018
1 parent 676a418 commit 4034b71
Showing 1 changed file with 40 additions and 20 deletions.
60 changes: 40 additions & 20 deletions src/Handler/CloudWatch.php
Expand Up @@ -9,6 +9,18 @@

class CloudWatch extends AbstractProcessingHandler
{
/**
* Requests per second limit (https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/cloudwatch_limits_cwl.html)
*/
const RPS_LIMIT = 5;

/**
* Event size limit (https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/cloudwatch_limits_cwl.html)
*
* @var int
*/
const EVENT_SIZE_LIMIT = 262118; // 262144 - reserved 26

/**
* @var CloudWatchLogsClient
*/
Expand Down Expand Up @@ -66,11 +78,6 @@ class CloudWatch extends AbstractProcessingHandler
*/
private $currentDataAmount = 0;

/**
* Requests per second limit (https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/cloudwatch_limits_cwl.html)
*/
const RPS_LIMIT = 5;

/**
* @var int
*/
Expand Down Expand Up @@ -133,15 +140,17 @@ public function __construct(
*/
protected function write(array $record)
{
$record = $this->formatRecord($record);

if ($this->currentDataAmount + $this->getMessageSize($record) >= $this->dataAmountLimit ||
count($this->buffer) >= $this->batchSize
) {
$this->flushBuffer();
$this->addToBuffer($record);
} else {
$this->addToBuffer($record);
$records = $this->formatRecords($record);

foreach ($records as $record) {
if ($this->currentDataAmount + $this->getMessageSize($record) >= $this->dataAmountLimit ||
count($this->buffer) >= $this->batchSize
) {
$this->flushBuffer();
$this->addToBuffer($record);
} else {
$this->addToBuffer($record);
}
}
}

Expand Down Expand Up @@ -197,15 +206,26 @@ private function getMessageSize($record)
}

/**
* Event size in the batch can not be bigger than 256 KB
* https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/cloudwatch_limits_cwl.html
*
* @param array $entry
* @return array
*/
private function formatRecord(array $entry)
private function formatRecords(array $entry)
{
return [
'message' => $entry['formatted'],
'timestamp' => $entry['datetime']->format('U.u') * 1000
];
$entries = str_split($entry['formatted'], self::EVENT_SIZE_LIMIT);
$timestamp = $entry['datetime']->format('U.u') * 1000;
$records = [];

foreach ($entries as $entry) {
$records[] = [
'message' => $entry,
'timestamp' => $timestamp
];
}

return $records;
}

/**
Expand Down Expand Up @@ -272,7 +292,7 @@ function ($group) {
$this
->client
->createLogGroup($createLogGroupArguments);

if ($this->retention !== null) {
$this
->client
Expand Down

0 comments on commit 4034b71

Please sign in to comment.