Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a more detailed stack trace into the query log #202

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions includes/classes/Elasticsearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -1618,13 +1618,52 @@ public function add_elasticpress_version_to_user_agent( $user_agent ) {
* Query logging. Don't log anything to the queries property when
* WP_DEBUG is not enabled. Calls action 'ep_add_query_log' if you
* want to access the query outside of the ElasticPress plugin. This
* runs regardless of debufg settings.
* runs regardless of debug settings.
*
* @param array $query Query to log.
*/
protected function add_query_log( $query ) {
// VIP: Search Dev Tools relies on this backtrace
if ( ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || ( defined( 'WP_EP_DEBUG' ) && WP_EP_DEBUG ) ) {
$query['backtrace'] = wp_debug_backtrace_summary( null, 1, false ); // VIP: Search Dev Tools relies on this backtrace
$backtrace = debug_backtrace( 0 );
$call_path = array();

foreach ( $backtrace as $bt_key => $call ) {
if ( ! isset( $call['args'] ) ) {
$call['args'] = array( '' );
}

if ( in_array( $call['function'], array( __FUNCTION__ ) ) ) {
continue;
}

$path = '';

$path = isset( $call['file'] ) ? str_replace( ABSPATH, '', $call['file'] ) : '';
$path .= isset( $call['line'] ) ? ':' . $call['line'] : '';

if ( isset( $call['class'] ) ) {
$call_type = $call['type'] ?? '???';
$path .= " {$call['class']}{$call_type}{$call['function']}()";
} elseif ( in_array( $call['function'], array( 'do_action', 'apply_filters', 'do_action_ref_array', 'apply_filters_ref_array' ) ) ) {
if ( is_object( $call['args'][0] ) && ! method_exists( $call['args'][0], '__toString' ) ) {
$path .= " {$call['function']}(Object)";
} elseif ( is_array( $call['args'][0] ) ) {
$path .= " {$call['function']}(Array)";
} else {
$path .= " {$call['function']}('{$call['args'][0]}')";
}
} elseif ( in_array( $call['function'], array( 'include', 'include_once', 'require', 'require_once' ) ) ) {
$file = 0 == $bt_key ? '' : $call['args'][0];
$path .= " {$call['function']}('" . str_replace( ABSPATH, '', $file ) . "')";
} else {
$path .= " {$call['function']}()";
}

$call_path[] = trim( $path );
$query['backtrace'] = $call_path;
}

$this->queries[] = $query;
}

Expand Down
Loading