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

Keep reference to the request until after the transaction is finished #879

Merged
merged 5 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
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
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
Loading