From f21d33971fdb35f1728c83db3eb97634a96c0ea4 Mon Sep 17 00:00:00 2001 From: Ivan Kristianto Date: Wed, 21 Apr 2021 05:17:15 +0000 Subject: [PATCH 1/4] Truncate ses message data to cloudwatch --- inc/ses_to_cloudwatch/namespace.php | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/inc/ses_to_cloudwatch/namespace.php b/inc/ses_to_cloudwatch/namespace.php index f13d212e..5c6e5155 100644 --- a/inc/ses_to_cloudwatch/namespace.php +++ b/inc/ses_to_cloudwatch/namespace.php @@ -25,6 +25,11 @@ function bootstrap() { * @param array $message The response message. */ function on_sent_message( $result, $message ) { + // Truncate the size of message array item to max 5KB. + array_walk_recursive( $message, function ( $value, $key ) { + return truncate_message( $value ); + } ); + Cloud\get_logger( 'ses', 'Sent' )->info( json_encode( $message ) ); } @@ -35,6 +40,11 @@ function on_sent_message( $result, $message ) { * @param array $message The error message. */ function on_error_sending_message( Exception $error, $message ) { + // Truncate the size of message array item to max 5KB. + array_walk_recursive( $message, function ( $value, $key ) { + return truncate_message( $value ); + } ); + Cloud\get_logger( 'ses', 'Failed' )->error( json_encode( [ 'error' => [ 'class' => get_class( $error ), @@ -43,3 +53,21 @@ function on_error_sending_message( Exception $error, $message ) { 'message' => $message, ] ) ); } + +/** + * Truncate string to given maximum size. + * + * @param string $message String message to truncate. + * @param int $max_size Maximum size in bytes, default 5KB. + * @param string $replacement String replacement. + * + * @return string + */ +function truncate_message( string $message, int $max_size = 5 * 1024, string $replacement = '…' ) : string { + if ( mb_strlen( $message ) < $max_size ) { + return $message; + } + + // Truncate query in the middle. + return substr_replace( $message, $replacement, $max_size / 2, mb_strlen( $message ) - $max_size + strlen( $replacement ) ); +} \ No newline at end of file From 4f1326b2e915b705daa213ccc68f098e324bdd6d Mon Sep 17 00:00:00 2001 From: Ivan Kristianto Date: Wed, 21 Apr 2021 05:21:01 +0000 Subject: [PATCH 2/4] Add extra space at the end of file --- inc/ses_to_cloudwatch/namespace.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inc/ses_to_cloudwatch/namespace.php b/inc/ses_to_cloudwatch/namespace.php index 5c6e5155..ae82dfd1 100644 --- a/inc/ses_to_cloudwatch/namespace.php +++ b/inc/ses_to_cloudwatch/namespace.php @@ -70,4 +70,4 @@ function truncate_message( string $message, int $max_size = 5 * 1024, string $re // Truncate query in the middle. return substr_replace( $message, $replacement, $max_size / 2, mb_strlen( $message ) - $max_size + strlen( $replacement ) ); -} \ No newline at end of file +} From ffc26251e898519b860b0e3f2c9fc708c0ce123f Mon Sep 17 00:00:00 2001 From: Ivan Kristianto Date: Wed, 21 Apr 2021 13:05:21 +0000 Subject: [PATCH 3/4] Replace message to string and use strlen --- inc/ses_to_cloudwatch/namespace.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/inc/ses_to_cloudwatch/namespace.php b/inc/ses_to_cloudwatch/namespace.php index ae82dfd1..2228f797 100644 --- a/inc/ses_to_cloudwatch/namespace.php +++ b/inc/ses_to_cloudwatch/namespace.php @@ -27,7 +27,7 @@ function bootstrap() { function on_sent_message( $result, $message ) { // Truncate the size of message array item to max 5KB. array_walk_recursive( $message, function ( $value, $key ) { - return truncate_message( $value ); + return truncate_string( $value ); } ); Cloud\get_logger( 'ses', 'Sent' )->info( json_encode( $message ) ); @@ -42,7 +42,7 @@ function on_sent_message( $result, $message ) { function on_error_sending_message( Exception $error, $message ) { // Truncate the size of message array item to max 5KB. array_walk_recursive( $message, function ( $value, $key ) { - return truncate_message( $value ); + return truncate_string( $value ); } ); Cloud\get_logger( 'ses', 'Failed' )->error( json_encode( [ @@ -57,17 +57,17 @@ function on_error_sending_message( Exception $error, $message ) { /** * Truncate string to given maximum size. * - * @param string $message String message to truncate. + * @param string $string String message to truncate. * @param int $max_size Maximum size in bytes, default 5KB. * @param string $replacement String replacement. * * @return string */ -function truncate_message( string $message, int $max_size = 5 * 1024, string $replacement = '…' ) : string { - if ( mb_strlen( $message ) < $max_size ) { - return $message; +function truncate_string( string $string, int $max_size = 5 * 1024, string $replacement = '…' ) : string { + if ( strlen( $string ) < $max_size ) { + return $string; } - // Truncate query in the middle. - return substr_replace( $message, $replacement, $max_size / 2, mb_strlen( $message ) - $max_size + strlen( $replacement ) ); + // Truncate string in the middle. + return substr_replace( $string, $replacement, $max_size / 2, strlen( $string ) - $max_size + strlen( $replacement ) ); } From ce45581340860a2215c12806e3f69d0b4f0f8918 Mon Sep 17 00:00:00 2001 From: Robert O'Rourke Date: Thu, 22 Apr 2021 10:36:33 +0000 Subject: [PATCH 4/4] Remove docblock alignment --- inc/ses_to_cloudwatch/namespace.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inc/ses_to_cloudwatch/namespace.php b/inc/ses_to_cloudwatch/namespace.php index 2228f797..762b76e8 100644 --- a/inc/ses_to_cloudwatch/namespace.php +++ b/inc/ses_to_cloudwatch/namespace.php @@ -57,8 +57,8 @@ function on_error_sending_message( Exception $error, $message ) { /** * Truncate string to given maximum size. * - * @param string $string String message to truncate. - * @param int $max_size Maximum size in bytes, default 5KB. + * @param string $string String to truncate. + * @param int $max_size Maximum size in bytes, default 5KB. * @param string $replacement String replacement. * * @return string