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

Commit

Permalink
no message
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonbahl committed Oct 20, 2017
1 parent 93409fe commit 9ddf3af
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
45 changes: 43 additions & 2 deletions src/Tracing.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php

namespace WPGraphQL\Extensions\Insights;
use GraphQL\Type\Definition\ResolveInfo;
use WPGraphQL\AppContext;

/**
* Class Tracing
Expand All @@ -9,6 +11,12 @@
*/
class Tracing {

/**
* Stores an individual trace for a field resolver
* @var array
*/
protected static $field_resolver_trace = [];

/**
* Stores whether tracing should be included in the response for GraphQL Requests
*/
Expand All @@ -21,6 +29,12 @@ class Tracing {
*/
public static $store_data = true;

/**
* Stores the start microtime of a resolver
* @var string
*/
protected static $resolver_start = null;

/**
* Stores the resolver traces
*
Expand Down Expand Up @@ -177,7 +191,6 @@ public static function get_resolver_start_offset() {
*/
public static function get_resolver_duration( $resolver_start ) {
$resolver_end = microtime( true );

return ( $resolver_end - $resolver_start ) * 1000000;
}

Expand Down Expand Up @@ -302,8 +315,36 @@ public static function add_tracing_to_response_extensions( $response, $schema, $
*/
public static function add_tracked_queries_to_response_extensions( $response, $schema, $operation_name, $request, $variables ) {

$response->extensions['queryLog'] = QueryTrace::get_trace();
if ( true === self::include_tracing_in_response( $response, $schema, $operation_name, $request, $variables ) ) {
$response->extensions['queryLog'] = QueryTrace::get_trace();
}
return $response;
}

public static function init_field_resolver_trace( $field, $type_object, $source, $args, AppContext $context, ResolveInfo $info ) {

$start_offset = Tracing::get_resolver_start_offset();
self::$resolver_start = microtime( true );

self::$field_resolver_trace = [
'path' => $info->path,
'parentType' => $info->parentType->name,
'fieldName' => $info->fieldName,
'returnType' => $info->returnType->name,
'startOffset' => $start_offset,
];

}

public static function close_field_resolver_trace( $field, $type_object, $source, $args, AppContext $context, ResolveInfo $info ) {
self::$field_resolver_trace['duration'] = Tracing::get_resolver_duration( self::$resolver_start );
Tracing::trace_resolver( self::$field_resolver_trace );
self::reset_field_resolver_trace();
}

protected static function reset_field_resolver_trace() {
self::$field_resolver_trace = [];
self::$resolver_start = null;
}

}
41 changes: 36 additions & 5 deletions wp-graphql-insights.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
* Author URI: https://www.wpgraphql.com
* Text Domain: wp-graphql-insights
* Domain Path: /languages
* Version: 0.0.1
* Version: 0.0.2
*
* @package WPGraphQL_Insights
*/
Expand Down Expand Up @@ -84,7 +84,7 @@ private static function setup_constants() {

// Plugin version.
if ( ! defined( 'WPGRAPHQL_INSIGHTS_VERSION' ) ) {
define( 'WPGRAPHQL_INSIGHTS_VERSION', '0.0.1' );
define( 'WPGRAPHQL_INSIGHTS_VERSION', '0.0.2' );
}

// Plugin Folder Path.
Expand Down Expand Up @@ -129,10 +129,31 @@ private function includes() {
*/
private function actions() {

/**
* Initialize the trace when the GraphQL request begins
*/
add_action( 'do_graphql_request', [ '\WPGraphQL\Extensions\Insights\Tracing', 'init_trace' ], 99, 3 );

/**
* Initialize the Query Trace when execution begins
*/
add_action( 'graphql_execute', [ '\WPGraphQL\Extensions\Insights\QueryTrace', 'init_trace' ], 99, 3 );

/**
* Close the trace when execution completes
*/
add_action( 'graphql_execute', [ '\WPGraphQL\Extensions\Insights\Tracing', 'close_trace' ], 99, 5 );

/**
* Initialize each resolver trace
*/
add_action( 'graphql_before_resolve', [ 'WPGraphQL\Extensions\Insights\Tracing', 'init_field_resolver_trace' ], 10, 6 );

/**
* Close each resolver trace
*/
add_action( 'graphql_after_resolve', [ 'WPGraphQL\Extensions\Insights\Tracing', 'close_field_resolver_trace' ], 10, 6 );

}

/**
Expand All @@ -143,8 +164,6 @@ private function actions() {
*/
private function filters() {

add_filter( 'graphql_schema', [ 'WPGraphQL\Extensions\Insights\InstrumentSchema', 'instrument' ], 10, 1 );

/**
* Filter the request_results to include Tracing in the extensions
*/
Expand All @@ -159,9 +178,21 @@ private function filters() {

/**
* Initialize the plugin
* @return object
* @return mixed|object|bool
*/
function graphql_insights_init() {

/**
* If the version of WPGraphQL isn't up to date, don't instantiate tracing
* @todo: consider displaying an Admin Notice or something to that tune if the versions aren't compatible
*/
if ( defined( 'WPGRAPHQL_VERSION' ) && version_compare( WPGRAPHQL_VERSION, '0.0.20', '<=' ) ) {
return false;
}

/**
* Return the instance of the Insights plugin to kick off functionality
*/
return \WPGraphQL\Extensions\Insights::instance();
}

Expand Down

0 comments on commit 9ddf3af

Please sign in to comment.