Created
March 6, 2022 19:29
-
-
Save felipeelia/7a7a8a6bf9da15ef06c37c867eb83dbe to your computer and use it in GitHub Desktop.
Set of functions to help debugging ElasticPress dynamic sync requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This is a set of functions that can be used to debug | |
* the dynamic sync made by ElasticPress | |
* | |
* phpcs:disable WordPress.PHP.DevelopmentFunctions -- Reason: the whole purpose of the file is to use error_log(). | |
* | |
* @package ElasticPress_Custom | |
*/ | |
/** | |
* Called in the beginning of the batch processing. | |
* | |
* Log a header with the document count and their average size. | |
* | |
* @param array $documents Array of documents to be sent to Elasticsearch. | |
*/ | |
function ep_log_ep_before_send_dynamic_bulk_requests( $documents ) { | |
error_log( '' ); | |
error_log( '' ); | |
error_log( '' ); | |
error_log( '==============================================' ); | |
error_log( '============ STARTING A NEW BATCH ============' ); | |
error_log( '==============================================' ); | |
error_log( '' ); | |
error_log( '' ); | |
error_log( 'Documents count: ' . count( $documents ) ); | |
error_log( 'Documents avg size: ' . ( mb_strlen( implode( '', $documents ) ) / count( $documents ) ) ); | |
error_log( '' ); | |
error_log( '==============================================' ); | |
error_log( '' ); | |
} | |
add_action( 'ep_before_send_dynamic_bulk_requests', 'ep_log_ep_before_send_dynamic_bulk_requests' ); | |
/** | |
* Called if detected a post that is bigger than the max buffer size possible. | |
* | |
* @param string $next_document JSON string of the post detected as too big. | |
*/ | |
function ep_log_ep_dynamic_bulk_post_too_big( $next_document ) { | |
error_log( 'Indexable too big. Request not sent.' ); | |
error_log( '==============================================' ); | |
} | |
add_action( 'ep_dynamic_bulk_post_too_big', 'ep_log_ep_dynamic_bulk_post_too_big' ); | |
/** | |
* Called right after a request was made. | |
* | |
* @param WP_Error|array $result Result of the request. | |
* @param array $body Array of documents sent to Elasticsearch. | |
* @param array $documents Array of documents to be sent to Elasticsearch. | |
* @param int $min_buffer_size Min buffer size for dynamic bulk index (in bytes.) | |
* @param int $max_buffer_size Max buffer size for dynamic bulk index (in bytes.) | |
* @param int $current_buffer_size Current buffer size for dynamic bulk index (in bytes.) | |
* @param int $request_time Total time of the request. | |
*/ | |
function ep_log_ep_after_send_dynamic_bulk_request( $result, $body, $documents, $min_buffer_size, $max_buffer_size, $current_buffer_size, $request_time ) { | |
error_log( 'count( $body ): ' . count( $body ) ); | |
error_log( 'count( $documents ): ' . count( $documents ) ); | |
error_log( '$min_buffer_size: ' . $min_buffer_size ); | |
error_log( '$max_buffer_size: ' . $max_buffer_size ); | |
error_log( '$current_buffer_size: ' . $current_buffer_size ); | |
error_log( 'Current request size: ' . mb_strlen( implode( '', $body ) ) ); | |
error_log( 'ES Time: ' . ( is_array( $result ) && isset( $result['took'] ) ? $result['took'] / 1000 : '?' ) ); | |
error_log( 'Request Time: ' . $request_time ); | |
error_log( '==============================================' ); | |
if ( is_wp_error( $result ) ) { | |
error_log( $result->get_error_code() . ' - ' . $result->get_error_message() ); | |
error_log( '==============================================' ); | |
if ( count( $body ) === 1 ) { | |
error_log( 'ONE POST TOO BIG' ); | |
error_log( '==============================================' ); | |
} elseif ( mb_strlen( implode( '', $body ) ) === $min_buffer_size ) { | |
error_log( 'THIS REALLY FAILED' ); | |
error_log( '==============================================' ); | |
} | |
} | |
} | |
add_action( 'ep_after_send_dynamic_bulk_request', 'ep_log_ep_after_send_dynamic_bulk_request', 10, 7 ); | |
/** | |
* Called in the end of the batch processing. | |
* | |
* @param array $results Array of results sent. | |
* @param int $requests Number of all requests sent. | |
* @return void | |
*/ | |
function ep_log_ep_after_send_dynamic_bulk_requests( $results, $requests ) { | |
error_log( '' ); | |
error_log( '# of requests: ' . $requests ); | |
error_log( '==============================================' ); | |
} | |
add_action( 'ep_after_send_dynamic_bulk_requests', 'ep_log_ep_after_send_dynamic_bulk_requests', 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment