|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Sentry\Laravel\Tracing\Integrations; |
| 4 | + |
| 5 | +use GraphQL\Language\AST\DocumentNode; |
| 6 | +use GraphQL\Language\AST\OperationDefinitionNode; |
| 7 | +use Illuminate\Contracts\Events\Dispatcher as EventDispatcher; |
| 8 | +use Nuwave\Lighthouse\Events\EndExecution; |
| 9 | +use Nuwave\Lighthouse\Events\EndRequest; |
| 10 | +use Nuwave\Lighthouse\Events\StartExecution; |
| 11 | +use Nuwave\Lighthouse\Events\StartRequest; |
| 12 | +use Sentry\Integration\IntegrationInterface; |
| 13 | +use Sentry\Laravel\Integration; |
| 14 | +use Sentry\SentrySdk; |
| 15 | +use Sentry\Tracing\SpanContext; |
| 16 | + |
| 17 | +class LighthouseIntegration implements IntegrationInterface |
| 18 | +{ |
| 19 | + /** @var array<int, array{?string, \GraphQL\Language\AST\OperationDefinitionNode}> */ |
| 20 | + private $operations; |
| 21 | + |
| 22 | + /** @var \Sentry\Tracing\Span|null */ |
| 23 | + private $previousSpan; |
| 24 | + |
| 25 | + /** @var \Sentry\Tracing\Span|null */ |
| 26 | + private $requestSpan; |
| 27 | + |
| 28 | + /** @var \Sentry\Tracing\Span|null */ |
| 29 | + private $operationSpan; |
| 30 | + |
| 31 | + /** @var \Illuminate\Contracts\Events\Dispatcher */ |
| 32 | + private $eventDispatcher; |
| 33 | + |
| 34 | + /** |
| 35 | + * Indicates if, when building the transaction name, the operation name should be ignored. |
| 36 | + * |
| 37 | + * @var bool |
| 38 | + */ |
| 39 | + private $ignoreOperationName; |
| 40 | + |
| 41 | + public function __construct(EventDispatcher $eventDispatcher, bool $ignoreOperationName = false) |
| 42 | + { |
| 43 | + $this->eventDispatcher = $eventDispatcher; |
| 44 | + $this->ignoreOperationName = $ignoreOperationName; |
| 45 | + } |
| 46 | + |
| 47 | + public function setupOnce(): void |
| 48 | + { |
| 49 | + if ($this->isApplicable()) { |
| 50 | + $this->eventDispatcher->listen(StartRequest::class, [$this, 'handleStartRequest']); |
| 51 | + $this->eventDispatcher->listen(StartExecution::class, [$this, 'handleStartExecution']); |
| 52 | + $this->eventDispatcher->listen(EndExecution::class, [$this, 'handleEndExecution']); |
| 53 | + $this->eventDispatcher->listen(EndRequest::class, [$this, 'handleEndRequest']); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public function handleStartRequest(StartRequest $startRequest): void |
| 58 | + { |
| 59 | + $this->previousSpan = Integration::currentTracingSpan(); |
| 60 | + |
| 61 | + if ($this->previousSpan === null) { |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + $context = new SpanContext; |
| 66 | + $context->setOp('graphql.request'); |
| 67 | + |
| 68 | + $this->operations = []; |
| 69 | + $this->requestSpan = $this->previousSpan->startChild($context); |
| 70 | + $this->operationSpan = null; |
| 71 | + |
| 72 | + SentrySdk::getCurrentHub()->setSpan($this->requestSpan); |
| 73 | + } |
| 74 | + |
| 75 | + public function handleStartExecution(StartExecution $startExecution): void |
| 76 | + { |
| 77 | + if ($this->requestSpan === null) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + if (!$startExecution->query instanceof DocumentNode) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + /** @var \GraphQL\Language\AST\OperationDefinitionNode|null $operationDefinition */ |
| 86 | + $operationDefinition = $startExecution->query->definitions[0] ?? null; |
| 87 | + |
| 88 | + if (!$operationDefinition instanceof OperationDefinitionNode) { |
| 89 | + return; |
| 90 | + } |
| 91 | + |
| 92 | + $this->operations[] = [$startExecution->operationName ?? null, $operationDefinition]; |
| 93 | + |
| 94 | + $this->updateTransactionName(); |
| 95 | + |
| 96 | + $context = new SpanContext; |
| 97 | + $context->setOp("graphql.{$operationDefinition->operation}"); |
| 98 | + |
| 99 | + $this->operationSpan = $this->requestSpan->startChild($context); |
| 100 | + |
| 101 | + SentrySdk::getCurrentHub()->setSpan($this->operationSpan); |
| 102 | + } |
| 103 | + |
| 104 | + public function handleEndExecution(EndExecution $endExecution): void |
| 105 | + { |
| 106 | + if ($this->operationSpan === null) { |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + $this->operationSpan->finish(); |
| 111 | + $this->operationSpan = null; |
| 112 | + |
| 113 | + SentrySdk::getCurrentHub()->setSpan($this->requestSpan); |
| 114 | + } |
| 115 | + |
| 116 | + public function handleEndRequest(EndRequest $endRequest): void |
| 117 | + { |
| 118 | + if ($this->requestSpan === null) { |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + $this->requestSpan->finish(); |
| 123 | + $this->requestSpan = null; |
| 124 | + |
| 125 | + SentrySdk::getCurrentHub()->setSpan($this->previousSpan); |
| 126 | + $this->previousSpan = null; |
| 127 | + |
| 128 | + $this->operations = []; |
| 129 | + } |
| 130 | + |
| 131 | + private function updateTransactionName(): void |
| 132 | + { |
| 133 | + $transaction = SentrySdk::getCurrentHub()->getTransaction(); |
| 134 | + |
| 135 | + if ($transaction === null) { |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + $groupedOperations = []; |
| 140 | + |
| 141 | + foreach ($this->operations as [$operationName, $operation]) { |
| 142 | + if (!isset($groupedOperations[$operation->operation])) { |
| 143 | + $groupedOperations[$operation->operation] = []; |
| 144 | + } |
| 145 | + |
| 146 | + if ($operationName === null || $this->ignoreOperationName) { |
| 147 | + $groupedOperations[$operation->operation] = array_merge( |
| 148 | + $groupedOperations[$operation->operation], |
| 149 | + $this->extractOperationNames($operation) |
| 150 | + ); |
| 151 | + } else { |
| 152 | + $groupedOperations[$operation->operation][] = $operationName; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + if (empty($groupedOperations)) { |
| 157 | + return; |
| 158 | + } |
| 159 | + |
| 160 | + array_walk($groupedOperations, static function (array &$operations, string $operationType) { |
| 161 | + sort($operations, SORT_STRING); |
| 162 | + |
| 163 | + $operations = "{$operationType}{" . implode(',', $operations) . '}'; |
| 164 | + }); |
| 165 | + |
| 166 | + ksort($groupedOperations, SORT_STRING); |
| 167 | + |
| 168 | + $transactionName = 'lighthouse?' . implode('&', $groupedOperations); |
| 169 | + |
| 170 | + $transaction->setName($transactionName); |
| 171 | + |
| 172 | + Integration::setTransaction($transactionName); |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * @return array<int, string> |
| 177 | + */ |
| 178 | + private function extractOperationNames(OperationDefinitionNode $operation): array |
| 179 | + { |
| 180 | + if (!$this->ignoreOperationName && $operation->name !== null) { |
| 181 | + return [$operation->name->value]; |
| 182 | + } |
| 183 | + |
| 184 | + $selectionSet = []; |
| 185 | + |
| 186 | + /** @var \GraphQL\Language\AST\FieldNode $selection */ |
| 187 | + foreach ($operation->selectionSet->selections as $selection) { |
| 188 | + // Not respecting aliases because they are only relevant for clients |
| 189 | + // and the tracing we extract here is targeted at server developers. |
| 190 | + $selectionSet[] = $selection->name->value; |
| 191 | + } |
| 192 | + |
| 193 | + sort($selectionSet, SORT_STRING); |
| 194 | + |
| 195 | + return $selectionSet; |
| 196 | + } |
| 197 | + |
| 198 | + private function isApplicable(): bool |
| 199 | + { |
| 200 | + if (!class_exists(StartRequest::class) || !class_exists(StartExecution::class)) { |
| 201 | + return false; |
| 202 | + } |
| 203 | + |
| 204 | + return property_exists(StartExecution::class, 'query'); |
| 205 | + } |
| 206 | +} |
0 commit comments