Skip to content

Commit

Permalink
Add span for Laravel HTTP client requests (#585)
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive committed Oct 12, 2022
1 parent 78817c5 commit e15abc6
Showing 1 changed file with 68 additions and 0 deletions.
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

0 comments on commit e15abc6

Please sign in to comment.