Skip to content

GraphQL and HTTP client improvements #720

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

Merged
merged 2 commits into from
Jul 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/Sentry/Laravel/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,8 @@ protected function httpClientResponseReceivedHandler(HttpClientEvents\ResponseRe
'status_code' => $event->response->status(),
'http.query' => $fullUri->getQuery(),
'http.fragment' => $fullUri->getFragment(),
'request_body_size' => strlen($event->request->body()),
'response_body_size' => strlen($event->response->body()),
]
));
}
Expand All @@ -313,6 +315,7 @@ protected function httpClientConnectionFailedHandler(HttpClientEvents\Connection
'method' => $event->request->method(),
'http.query' => $fullUri->getQuery(),
'http.fragment' => $fullUri->getFragment(),
'request_body_size' => strlen($event->request->body()),
]
));
}
Expand Down
52 changes: 47 additions & 5 deletions src/Sentry/Laravel/Tracing/Integrations/LighthouseIntegration.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
use GraphQL\Language\AST\DocumentNode;
use GraphQL\Language\AST\OperationDefinitionNode;
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
use Illuminate\Support\Str;
use Nuwave\Lighthouse\Events\EndExecution;
use Nuwave\Lighthouse\Events\EndRequest;
use Nuwave\Lighthouse\Events\StartExecution;
use Nuwave\Lighthouse\Events\StartRequest;
use Sentry\Event;
use Sentry\Integration\IntegrationInterface;
use Sentry\Laravel\Integration;
use Sentry\Options;
use Sentry\SentrySdk;
use Sentry\State\Scope;
use Sentry\Tracing\SpanContext;
use Sentry\Tracing\TransactionSource;

Expand Down Expand Up @@ -47,12 +51,50 @@ public function __construct(EventDispatcher $eventDispatcher, bool $ignoreOperat

public function setupOnce(): void
{
if ($this->isApplicable()) {
$this->eventDispatcher->listen(StartRequest::class, [$this, 'handleStartRequest']);
$this->eventDispatcher->listen(StartExecution::class, [$this, 'handleStartExecution']);
$this->eventDispatcher->listen(EndExecution::class, [$this, 'handleEndExecution']);
$this->eventDispatcher->listen(EndRequest::class, [$this, 'handleEndRequest']);
if (!$this->isApplicable()) {
return;
}

$this->eventDispatcher->listen(StartRequest::class, [$this, 'handleStartRequest']);
$this->eventDispatcher->listen(StartExecution::class, [$this, 'handleStartExecution']);
$this->eventDispatcher->listen(EndExecution::class, [$this, 'handleEndExecution']);
$this->eventDispatcher->listen(EndRequest::class, [$this, 'handleEndRequest']);

Scope::addGlobalEventProcessor(function (Event $event): Event {
$currentHub = SentrySdk::getCurrentHub();
$integration = $currentHub->getIntegration(self::class);
$client = $currentHub->getClient();

// The client bound to the current hub, if any, could not have this
// integration enabled. If this is the case, bail out
if (null === $integration || null === $client) {
return $event;
}

$this->processEvent($event, $client->getOptions());

return $event;
});
}

private function processEvent(Event $event, Options $options): void
{
// Detect if we are processing a GraphQL request, if not skip processing the event
if (!Str::startsWith($event->getTransaction(), 'lighthouse?')) {
return;
}

$requestData = $event->getRequest();

// Make sure we have the request data and it contains the query
if (!isset($requestData['data']['query'])) {
return;
}

// https://develop.sentry.dev/sdk/features/#graphql-client-integrations
$requestData['api_target'] = 'graphql';

$event->setRequest($requestData);
}

public function handleStartRequest(StartRequest $startRequest): void
Expand Down