Skip to content
This repository has been archived by the owner on Jan 8, 2021. It is now read-only.

Commit

Permalink
Merge pull request #6 from wp-graphql/feature/#5-query-logging
Browse files Browse the repository at this point in the history
This Introduces Query Logging, like Query Monitor
  • Loading branch information
jasonbahl committed Oct 19, 2017
2 parents bd7ab45 + 93409fe commit 81ccd6e
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 19 deletions.
27 changes: 8 additions & 19 deletions src/InstrumentSchema.php
Expand Up @@ -116,25 +116,14 @@ public static function wrap_field_resolvers( $fields, $type_name ) {
'startOffset' => $start_offset,
];

try {

/**
* If the current field doesn't have a resolve function, use the defaultFieldResolver,
* otherwise use the $field_resolver
*/
if ( null === $field_resolver || ! is_callable( $field_resolver ) ) {
$result = Executor::defaultFieldResolver( $source, $args, $context, $info );
} else {
$result = call_user_func( $field_resolver, $source, $args, $context, $info );
}

} catch ( \Exception $error ) {

/**
* Throw an exception for the error that was returned from the resolver
* @since 0.0.1
*/
throw new \Exception( $error );
/**
* If the current field doesn't have a resolve function, use the defaultFieldResolver,
* otherwise use the $field_resolver
*/
if ( null === $field_resolver || ! is_callable( $field_resolver ) ) {
$result = Executor::defaultFieldResolver( $source, $args, $context, $info );
} else {
$result = call_user_func( $field_resolver, $source, $args, $context, $info );
}

$trace['duration'] = Tracing::get_resolver_duration( $resolver_start );
Expand Down
83 changes: 83 additions & 0 deletions src/QueryTrace.php
@@ -0,0 +1,83 @@
<?php

namespace WPGraphQL\Extensions\Insights;

/**
* Class QueryTrace
*
* @package WPGraphQL\Extensions\Insights
*/
class QueryTrace {

/**
* Holds the trace data for queries
* @access protected
* @var array
*/
protected static $trace = [];

/**
* QueryTrace constructor.
* @access public
*/
public function __construct() {

if ( ! defined( 'SAVEQUERIES' ) ) {
define( 'SAVEQUERIES', true );
}

if ( ! defined( 'QM_DB_EXPENSIVE' ) ) {
define( 'QM_DB_EXPENSIVE', 0.05 );
}

/**
* Enable Core WordPress query logging
*/
if ( SAVEQUERIES && property_exists( $GLOBALS['wpdb'], 'save_queries' ) ) {
$GLOBALS['wpdb']->save_queries = true;
}

}

/**
* Initialize the trace if SAVEQUERIES is enabled
* @access public
*/
public static function init_trace() {

if ( ! defined( 'SAVEQUERIES' ) || true !== SAVEQUERIES ) {
return;
}

}

/**
* Returns the query trace for output with the Extensions
* @access public
* @return array
*/
public static function get_trace() {

if ( ! empty( $GLOBALS['wpdb']->queries ) && is_array( $GLOBALS['wpdb']->queries ) ) {
$queries = array_map( function( $query ) {
$output = [
'sql' => $query[0],
'time' => $query[1],
'stack' => $query[2],
];
return $output;
}, $GLOBALS['wpdb']->queries );

self::$trace['queryCount'] = count( $queries );
$totalTime = 0;
foreach ( $queries as $query ) {
$totalTime = $totalTime + $query['time'];
}
self::$trace['totalTime'] = $totalTime;
self::$trace['queries'] = $queries;
}

return self::$trace;
}

}
17 changes: 17 additions & 0 deletions src/Tracing.php
Expand Up @@ -289,4 +289,21 @@ public static function add_tracing_to_response_extensions( $response, $schema, $
return $response;
}

/**
* Adds the tracked queries to the extensions response
*
* @param object $response
* @param object $schema
* @param string $operation_name
* @param string $request
* @param array $variables
*
* @return mixed
*/
public static function add_tracked_queries_to_response_extensions( $response, $schema, $operation_name, $request, $variables ) {

$response->extensions['queryLog'] = QueryTrace::get_trace();
return $response;
}

}
2 changes: 2 additions & 0 deletions wp-graphql-insights.php
Expand Up @@ -130,6 +130,7 @@ private function includes() {
private function actions() {

add_action( 'do_graphql_request', [ '\WPGraphQL\Extensions\Insights\Tracing', 'init_trace' ], 99, 3 );
add_action( 'graphql_execute', [ '\WPGraphQL\Extensions\Insights\QueryTrace', 'init_trace' ], 99, 3 );
add_action( 'graphql_execute', [ '\WPGraphQL\Extensions\Insights\Tracing', 'close_trace' ], 99, 5 );

}
Expand All @@ -148,6 +149,7 @@ private function filters() {
* Filter the request_results to include Tracing in the extensions
*/
add_filter( 'graphql_request_results', [ 'WPGraphQL\Extensions\Insights\Tracing', 'add_tracing_to_response_extensions' ], 10, 5 );
add_filter( 'graphql_request_results', [ 'WPGraphQL\Extensions\Insights\Tracing', 'add_tracked_queries_to_response_extensions' ], 10, 5 );

}

Expand Down

0 comments on commit 81ccd6e

Please sign in to comment.