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

Idea: Add debug mode that returns query generated for request in headers #32

Closed
nerrad opened this issue Sep 24, 2019 · 3 comments

Comments

@nerrad
Copy link

@nerrad nerrad commented Sep 24, 2019

Is your feature request related to a problem? Please describe.

Parsing the code to figure out what query is generated for a request is possible but not always easy. For debugging purposes, it would be useful if REST responses could (via option/constant etc) could return the generated query in a header on request.

Describe the solution you'd like

One possible mechanism for implementing this would be to:

  • define a constant in wp-config.php (i.e. WC_REST_DEBUG) or have a filter (probably best option) for turning on debug mode.
  • When debug mode is on, use $wpdb->last_query to get the generated query fulfilling a request and return that on a header, maybe something like X-WC-Debug-Query. That header can then be used to see what the actual query was used for the results.

Describe alternatives you've considered

  • dumping $wpdb->query temporarily directly in the code and getting it in the (broken) response.
@issue-label-bot

This comment has been minimized.

Copy link

@issue-label-bot issue-label-bot bot commented Sep 24, 2019

Issue-Label Bot is automatically applying the label type: feature request to this issue, with a confidence of 0.98. Please mark this comment with 👍 or 👎 to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

@nerrad

This comment has been minimized.

Copy link
Author

@nerrad nerrad commented Sep 24, 2019

I wrote this which is a little example plugin:

<?php
/**
 * Plugin Name: Debug REST API Queries
 * Description: Single use plugin.  Will output the last query executed on a REST Request
 * Author: Darren Ethier for Automattic
 * Author URI: https://woocommerce.com
 * Version: 1.0
 * Requires PHP: 5.6
 * License: GPLv3
 */

add_filter( 'rest_request_after_callbacks', function ( $response ) {
	if ( WP_DEBUG && $response instanceof WP_REST_Response ) {
		global $wpdb;
		$response->header( 'X-WC-last-query', $wpdb->last_query );
	}
	return $response;
} );

It outputs the last wp query executed on valid responses to any REST endpoint request on the X-WC-last-query header. The downside however is that with the Woo data model, a given entity may be composed from multiple queries. So with only the last query output it might not be that useful. Ideally, a solution would concatenate all the queries executed for a given request and return that as the header value (might need to address potential size limits on header as well).

@nerrad

This comment has been minimized.

Copy link
Author

@nerrad nerrad commented Sep 25, 2019

New plugin:

<?php
/**
 * Plugin Name: Debug REST API Queries
 * Description: Single use plugin.  Will output the last query executed on a REST Request
 * Author: Darren Ethier for Automattic
 * Author URI: https://woocommerce.com
 * Version: 1.0
 * Requires PHP: 5.6
 * License: GPLv3
 */

add_filter( 'rest_pre_dispatch', function( $ignore, $server, $request ) {
	if ( $request['WP_DEBUG'] || WP_DEBUG ) {
		if ( ! defined( 'SAVEQUERIES' ) ) {
			define( 'SAVEQUERIES', true );
		}
	}
	return $ignore;
}, 10, 3 );

add_filter( 'rest_request_after_callbacks', function ( $response, $handler, $request ) {
	if ( ( $request['WP_DEBUG'] || WP_DEBUG ) && $response instanceof WP_REST_Response ) {
		global $wpdb;
		$queries = array_filter(
			array_map( function ( $query_item ) {
				if ( strpos( $query_item[2], 'Controller' ) !== false ) {
					return $query_item[0];
				} else {
					return null;
				}
			}, $wpdb->queries ),
			function ( $item ) { return $item !== null; }
		);
		$response->header( 'X-WC-all-queries', wp_json_encode( $queries ) );
		$response->header( 'X-WC-last-query', $wpdb->last_query );
	}
	return $response;
}, 10, 3 );

Since we can get pretty much what we want via the plugin approach, I'm going to close this. I may add this in it's own repo as I think it has utility for gaining insight into the queries run on a given endpoint request.

@nerrad nerrad closed this Sep 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant
You can’t perform that action at this time.