This commit is contained in:
Your Name
2021-07-26 19:46:18 +02:00
parent e7a49138bb
commit aae17f10a6
818 changed files with 70695 additions and 16408 deletions
+11 -28
View File
@@ -2,22 +2,22 @@
namespace Nuwave\Lighthouse\Tracing;
use Carbon\Carbon;
use GraphQL\Language\Parser;
use GraphQL\Type\Definition\ResolveInfo;
use Nuwave\Lighthouse\Events\StartRequest;
use Nuwave\Lighthouse\Events\ManipulateAST;
use Nuwave\Lighthouse\Schema\AST\ASTHelper;
use Nuwave\Lighthouse\Events\StartExecution;
use Nuwave\Lighthouse\Schema\AST\PartialParser;
use Nuwave\Lighthouse\Execution\ExtensionsResponse;
use Illuminate\Support\Carbon;
use Nuwave\Lighthouse\Events\BuildExtensionsResponse;
use Nuwave\Lighthouse\Events\ManipulateAST;
use Nuwave\Lighthouse\Events\StartExecution;
use Nuwave\Lighthouse\Events\StartRequest;
use Nuwave\Lighthouse\Execution\ExtensionsResponse;
use Nuwave\Lighthouse\Schema\AST\ASTHelper;
class Tracing
{
/**
* The timestamp the request was initially started.
*
* @var \Carbon\Carbon
* @var \Illuminate\Support\Carbon
*/
protected $requestStart;
@@ -35,29 +35,23 @@ class Tracing
*
* Is reset between batches.
*
* @var array[]
* @var array<int, array<string, mixed>>
*/
protected $resolverTraces = [];
/**
* Set the tracing directive on all fields of the query to enable tracing them.
*
* @param \Nuwave\Lighthouse\Events\ManipulateAST $manipulateAST
* @return void
*/
public function handleManipulateAST(ManipulateAST $manipulateAST): void
{
ASTHelper::attachDirectiveToObjectTypeFields(
$manipulateAST->documentAST,
PartialParser::directive('@tracing')
Parser::constDirective('@tracing')
);
}
/**
* Handle request start.
*
* @param \Nuwave\Lighthouse\Events\StartRequest $startRequest
* @return void
*/
public function handleStartRequest(StartRequest $startRequest): void
{
@@ -67,9 +61,6 @@ class Tracing
/**
* Handle batch request start.
*
* @param \Nuwave\Lighthouse\Events\StartExecution $startExecution
* @return void
*/
public function handleStartExecution(StartExecution $startExecution): void
{
@@ -78,9 +69,6 @@ class Tracing
/**
* Return additional information for the result.
*
* @param \Nuwave\Lighthouse\Events\BuildExtensionsResponse $buildExtensionsResponse
* @return \Nuwave\Lighthouse\Execution\ExtensionsResponse
*/
public function handleBuildExtensionsResponse(BuildExtensionsResponse $buildExtensionsResponse): ExtensionsResponse
{
@@ -104,10 +92,8 @@ class Tracing
/**
* Record resolver execution time.
*
* @param \GraphQL\Type\Definition\ResolveInfo $resolveInfo
* @param float|int $start
* @param float|int $end
* @return void
*/
public function record(ResolveInfo $resolveInfo, $start, $end): void
{
@@ -140,12 +126,11 @@ class Tracing
*
* @param float|int $start
* @param float|int $end
* @return int
*/
protected function diffTimeInNanoseconds($start, $end): int
{
if ($this->platformSupportsNanoseconds()) {
return $end - $start;
return (int) ($end - $start);
}
// Difference is in seconds (with microsecond precision)
@@ -157,8 +142,6 @@ class Tracing
/**
* Test if the current PHP version has the `hrtime` function available to get a nanosecond precision point in time.
*
* @return bool
*/
protected function platformSupportsNanoseconds(): bool
{
+14 -43
View File
@@ -3,84 +3,55 @@
namespace Nuwave\Lighthouse\Tracing;
use Closure;
use GraphQL\Deferred;
use GraphQL\Type\Definition\ResolveInfo;
use Nuwave\Lighthouse\Schema\Values\FieldValue;
use Nuwave\Lighthouse\Execution\Resolved;
use Nuwave\Lighthouse\Schema\Directives\BaseDirective;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
use Nuwave\Lighthouse\Schema\Values\FieldValue;
use Nuwave\Lighthouse\Support\Contracts\FieldMiddleware;
use Nuwave\Lighthouse\Support\Contracts\DefinedDirective;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
class TracingDirective extends BaseDirective implements FieldMiddleware, DefinedDirective
class TracingDirective extends BaseDirective implements FieldMiddleware
{
const NAME = 'tracing';
/**
* @var \Nuwave\Lighthouse\Tracing\Tracing
*/
protected $tracing;
/**
* TracingDirective constructor.
*
* @param \Nuwave\Lighthouse\Tracing\Tracing $tracing
* @return void
*/
public function __construct(Tracing $tracing)
{
$this->tracing = $tracing;
}
/**
* Name of the directive.
*
* @return string
*/
public function name(): string
{
return self::NAME;
}
public static function definition(): string
{
return /* @lang GraphQL */ <<<'SDL'
return /** @lang GraphQL */ <<<'GRAPHQL'
"""
Do not use this directive directly, it is automatically added to the schema
when using the tracing extension.
"""
directive @tracing on FIELD_DEFINITION
SDL;
GRAPHQL;
}
/**
* Resolve the field directive.
*
* @param \Nuwave\Lighthouse\Schema\Values\FieldValue $fieldValue
* @param \Closure $next
* @return \Nuwave\Lighthouse\Schema\Values\FieldValue
*/
public function handleField(FieldValue $fieldValue, Closure $next): FieldValue
{
// Make sure this middleware is applied last
$fieldValue = $next($fieldValue);
$resolver = $fieldValue->getResolver();
$previousResolver = $fieldValue->getResolver();
return $fieldValue->setResolver(function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($resolver) {
$fieldValue->setResolver(function ($root, array $args, GraphQLContext $context, ResolveInfo $resolveInfo) use ($previousResolver) {
$start = $this->tracing->getTime();
$result = $resolver($root, $args, $context, $resolveInfo);
$result = $previousResolver($root, $args, $context, $resolveInfo);
$end = $this->tracing->getTime();
if ($result instanceof Deferred) {
$result->then(function () use ($resolveInfo, $start, $end): void {
$this->tracing->record($resolveInfo, $start, $end);
});
} else {
Resolved::handle($result, function () use ($resolveInfo, $start, $end): void {
$this->tracing->record($resolveInfo, $start, $end);
}
});
return $result;
});
return $fieldValue;
}
}
@@ -2,28 +2,28 @@
namespace Nuwave\Lighthouse\Tracing;
use Illuminate\Support\ServiceProvider;
use Nuwave\Lighthouse\Events\StartRequest;
use Nuwave\Lighthouse\Events\ManipulateAST;
use Nuwave\Lighthouse\Events\StartExecution;
use Nuwave\Lighthouse\Events\BuildExtensionsResponse;
use Nuwave\Lighthouse\Schema\Factories\DirectiveFactory;
use Illuminate\Contracts\Events\Dispatcher as EventsDispatcher;
use Illuminate\Support\ServiceProvider;
use Nuwave\Lighthouse\Events\BuildExtensionsResponse;
use Nuwave\Lighthouse\Events\ManipulateAST;
use Nuwave\Lighthouse\Events\RegisterDirectiveNamespaces;
use Nuwave\Lighthouse\Events\StartExecution;
use Nuwave\Lighthouse\Events\StartRequest;
class TracingServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @param \Nuwave\Lighthouse\Schema\Factories\DirectiveFactory $directiveFactory
* @param \Illuminate\Contracts\Events\Dispatcher $eventsDispatcher
* @return void
*/
public function boot(DirectiveFactory $directiveFactory, EventsDispatcher $eventsDispatcher): void
public function register(): void
{
$directiveFactory->addResolved(
TracingDirective::NAME,
TracingDirective::class
$this->app->singleton(Tracing::class);
}
public function boot(EventsDispatcher $eventsDispatcher): void
{
$eventsDispatcher->listen(
RegisterDirectiveNamespaces::class,
static function (): string {
return __NAMESPACE__;
}
);
$eventsDispatcher->listen(
@@ -46,14 +46,4 @@ class TracingServiceProvider extends ServiceProvider
Tracing::class.'@handleBuildExtensionsResponse'
);
}
/**
* Register any application services.
*
* @return void
*/
public function register(): void
{
$this->app->singleton(Tracing::class);
}
}