Skip to content

Commit

Permalink
Set the traced request directly on the request fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
stayallive committed Sep 8, 2024
1 parent cfb8a0f commit 018e46a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 21 deletions.
30 changes: 23 additions & 7 deletions src/EventListener/TracingRequestListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

namespace Sentry\SentryBundle\EventListener;

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

public function __construct(HubInterface $hub, RequestFetcherInterface $requestFetcher)
{
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 @@ -36,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 @@ -76,14 +94,12 @@ public function handleKernelTerminateEvent(TerminateEvent $event): void
return;
}

RequestFetcher::withCurrentRequest(
$event->getRequest(),
static function () use ($transaction): void {
$transaction->finish();
},
);

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

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

/**
Expand Down
22 changes: 8 additions & 14 deletions src/Integration/RequestFetcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ final class RequestFetcher implements RequestFetcherInterface
private $requestStack;

/**
* @var HttpMessageFactoryInterface The factory to convert Symfony requests to PSR-7 requests
* @var \Symfony\Component\HttpFoundation\Request|null The current request
*/
private $httpMessageFactory;
private $currentRequest;

/**
* @var \Symfony\Component\HttpFoundation\Request|null The current request
* @var HttpMessageFactoryInterface The factory to convert Symfony requests to PSR-7 requests
*/
private static $currentRequest = null;
private $httpMessageFactory;

/**
* Class constructor.
Expand All @@ -47,7 +47,7 @@ public function __construct(RequestStack $requestStack, ?HttpMessageFactoryInter
new HttpFactory(),
new HttpFactory(),
new HttpFactory(),
new HttpFactory(),
new HttpFactory()
);
}

Expand All @@ -56,7 +56,7 @@ public function __construct(RequestStack $requestStack, ?HttpMessageFactoryInter
*/
public function fetchRequest(): ?ServerRequestInterface
{
$request = self::$currentRequest ?? $this->requestStack->getCurrentRequest();
$request = $this->currentRequest ?? $this->requestStack->getCurrentRequest();

if (null === $request) {
return null;
Expand All @@ -69,14 +69,8 @@ public function fetchRequest(): ?ServerRequestInterface
}
}

public static function withCurrentRequest(Request $request, callable $callback): void
public function setRequest(?Request $request): void
{
self::$currentRequest = $request;

try {
$callback();
} finally {
self::$currentRequest = null;
}
$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 018e46a

Please sign in to comment.