Skip to content

Add http.route.response span #708

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 1 commit into from
Jun 19, 2023
Merged
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
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