Skip to content

Commit

Permalink
Add http.route.response span (#708)
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive authored Jun 19, 2023
1 parent de3d2e0 commit 63fa831
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/Sentry/Laravel/Tracing/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Sentry\Tracing\SpanStatus;
use Sentry\Tracing\TransactionContext;
use Sentry\Tracing\TransactionSource;
use Symfony\Component\HttpFoundation\Response;

class EventHandler
{
Expand All @@ -35,6 +36,8 @@ class EventHandler
protected static $eventHandlerMap = [
RoutingEvents\RouteMatched::class => 'routeMatched',
DatabaseEvents\QueryExecuted::class => 'queryExecuted',
RoutingEvents\ResponsePrepared::class => 'responsePrepared',
RoutingEvents\PreparingResponse::class => 'responsePreparing',
HttpClientEvents\RequestSending::class => 'httpClientRequestSending',
HttpClientEvents\ResponseReceived::class => 'httpClientResponseReceived',
HttpClientEvents\ConnectionFailed::class => 'httpClientConnectionFailed',
Expand Down Expand Up @@ -131,6 +134,8 @@ public function __construct(array $config, BacktraceHelper $backtraceHelper)
*
* @uses self::routeMatchedHandler()
* @uses self::queryExecutedHandler()
* @uses self::responsePreparedHandler()
* @uses self::responsePreparingHandler()
* @uses self::transactionBeginningHandler()
* @uses self::transactionCommittedHandler()
* @uses self::transactionRolledBackHandler()
Expand Down Expand Up @@ -258,6 +263,37 @@ private function resolveQueryOriginFromBacktrace(): ?string
return "{$filePath}:{$firstAppFrame->getLine()}";
}

protected function responsePreparedHandler(RoutingEvents\ResponsePrepared $event): void
{
$span = $this->popSpan();

if ($span !== null) {
$span->finish();
}
}

protected function responsePreparingHandler(RoutingEvents\PreparingResponse $event): void
{
// If the response is already a Response object there is no need to handle the event anymore
// since there isn't going to be any real work going on, the response is already as prepared
// as it can be. So we ignore the event to prevent loggin a very short empty duplicated span
if ($event->response instanceof Response) {
return;
}

$parentSpan = SentrySdk::getCurrentHub()->getSpan();

// If there is no tracing span active there is no need to handle the event
if ($parentSpan === null) {
return;
}

$context = new SpanContext;
$context->setOp('http.route.response');

$this->pushSpan($parentSpan->startChild($context));
}

protected function transactionBeginningHandler(DatabaseEvents\TransactionBeginning $event): void
{
$parentSpan = SentrySdk::getCurrentHub()->getSpan();
Expand Down

0 comments on commit 63fa831

Please sign in to comment.