Skip to content

Commit

Permalink
Keep reference to the request until after the transaction is finished (
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive authored Sep 26, 2024
1 parent 9b82b0a commit 1bc2033
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
23 changes: 23 additions & 0 deletions src/EventListener/TracingRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Sentry\SentryBundle\EventListener;

use Sentry\Integration\RequestFetcherInterface;
use Sentry\SentryBundle\Integration\RequestFetcher;
use Sentry\State\HubInterface;
use Sentry\Tracing\TransactionSource;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
Expand All @@ -20,6 +23,18 @@
*/
final class TracingRequestListener extends AbstractTracingRequestListener
{
/**
* @var RequestFetcherInterface|null
*/
private $requestFetcher;

public function __construct(HubInterface $hub, ?RequestFetcherInterface $requestFetcher = null)
{
parent::__construct($hub);

$this->requestFetcher = $requestFetcher;
}

/**
* This method is called for each subrequest handled by the framework and
* starts a new {@see Transaction}.
Expand All @@ -35,6 +50,10 @@ public function handleKernelRequestEvent(RequestEvent $event): void
/** @var Request $request */
$request = $event->getRequest();

if ($this->requestFetcher instanceof RequestFetcher) {
$this->requestFetcher->setRequest($request);
}

/** @var float $requestStartTime */
$requestStartTime = $request->server->get('REQUEST_TIME_FLOAT', microtime(true));

Expand Down Expand Up @@ -77,6 +96,10 @@ public function handleKernelTerminateEvent(TerminateEvent $event): void

$transaction->finish();
metrics()->flush();

if ($this->requestFetcher instanceof RequestFetcher) {
$this->requestFetcher->setRequest(null);
}
}

/**
Expand Down
13 changes: 12 additions & 1 deletion src/Integration/RequestFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Sentry\Integration\RequestFetcherInterface;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
use Symfony\Bridge\PsrHttpMessage\HttpMessageFactoryInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

/**
Expand All @@ -23,6 +24,11 @@ final class RequestFetcher implements RequestFetcherInterface
*/
private $requestStack;

/**
* @var Request|null The current request
*/
private $currentRequest;

/**
* @var HttpMessageFactoryInterface The factory to convert Symfony requests to PSR-7 requests
*/
Expand Down Expand Up @@ -50,7 +56,7 @@ public function __construct(RequestStack $requestStack, ?HttpMessageFactoryInter
*/
public function fetchRequest(): ?ServerRequestInterface
{
$request = $this->requestStack->getCurrentRequest();
$request = $this->currentRequest ?? $this->requestStack->getCurrentRequest();

if (null === $request) {
return null;
Expand All @@ -62,4 +68,9 @@ public function fetchRequest(): ?ServerRequestInterface
return null;
}
}

public function setRequest(?Request $request): void
{
$this->currentRequest = $request;
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

<service id="Sentry\SentryBundle\EventListener\TracingRequestListener" class="Sentry\SentryBundle\EventListener\TracingRequestListener">
<argument type="service" id="Sentry\State\HubInterface" />
<argument type="service" id="Sentry\Integration\RequestFetcherInterface" />

<tag name="kernel.event_listener" event="kernel.request" method="handleKernelRequestEvent" priority="4" />
<tag name="kernel.event_listener" event="kernel.response" method="handleKernelResponseEvent" priority="15" />
Expand Down

0 comments on commit 1bc2033

Please sign in to comment.