Skip to content
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

Add tracing span for Laravel HTTP client requests #585

Merged
merged 2 commits into from Oct 12, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/Sentry/Laravel/Tracing/EventHandler.php
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Contracts\Container\Container;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\Events as DatabaseEvents;
use Illuminate\Http\Client\Events as HttpClientEvents;
use Illuminate\Queue\Events as QueueEvents;
use Illuminate\Queue\Queue;
use Illuminate\Queue\QueueManager;
Expand All @@ -33,6 +34,9 @@ class EventHandler
protected static $eventHandlerMap = [
RoutingEvents\RouteMatched::class => 'routeMatched',
DatabaseEvents\QueryExecuted::class => 'queryExecuted',
HttpClientEvents\RequestSending::class => 'httpClientRequestSending',
HttpClientEvents\ResponseReceived::class => 'httpClientResponseReceived',
HttpClientEvents\ConnectionFailed::class => 'httpClientConnectionFailed',
];

/**
Expand Down Expand Up @@ -95,6 +99,20 @@ class EventHandler
*/
private $currentQueueJobSpan;

/**
* Holds a reference to the parent http client request span.
*
* @var \Sentry\Tracing\Span|null
*/
private $parentHttpClientRequestSpan;

/**
* Holds a reference to the current http client request span.
*
* @var \Sentry\Tracing\Span|null
*/
private $currentHttpClientRequestSpan;

/**
* The backtrace helper.
*
Expand Down Expand Up @@ -267,6 +285,56 @@ private function resolveQueryOriginFromBacktrace(): ?string
return "{$filePath}:{$firstAppFrame->getLine()}";
}

protected function httpClientRequestSendingHandler(HttpClientEvents\RequestSending $event): void
{
$parentSpan = Integration::currentTracingSpan();

if ($parentSpan === null) {
return;
}

$context = new SpanContext;

$context->setOp('http.client');
$context->setDescription($event->request->method() . ' ' . $event->request->url());
$context->setStartTimestamp(microtime(true));

$this->currentHttpClientRequestSpan = $parentSpan->startChild($context);

$this->parentHttpClientRequestSpan = $parentSpan;

SentrySdk::getCurrentHub()->setSpan($this->currentHttpClientRequestSpan);
}

protected function httpClientResponseReceivedHandler(HttpClientEvents\ResponseReceived $event): void
{
if ($this->currentHttpClientRequestSpan !== null) {
$this->currentHttpClientRequestSpan->setHttpStatus($event->response->status());
$this->afterHttpClientRequest();
}
}

protected function httpClientConnectionFailedHandler(HttpClientEvents\ConnectionFailed $event): void
{
if ($this->currentHttpClientRequestSpan !== null) {
$this->currentHttpClientRequestSpan->setStatus(SpanStatus::internalError());
$this->afterHttpClientRequest();
}
}

private function afterHttpClientRequest(): void
{
if ($this->currentHttpClientRequestSpan === null) {
return;
}

$this->currentHttpClientRequestSpan->finish();
$this->currentHttpClientRequestSpan = null;

SentrySdk::getCurrentHub()->setSpan($this->parentHttpClientRequestSpan);
$this->parentHttpClientRequestSpan = null;
}

protected function queueJobProcessingHandler(QueueEvents\JobProcessing $event): void
{
$parentSpan = Integration::currentTracingSpan();
Expand Down