vendor/sentry/sentry-symfony/src/EventListener/TracingRequestListener.php line 67

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sentry\SentryBundle\EventListener;
  4. use Sentry\Tracing\Transaction;
  5. use Sentry\Tracing\TransactionContext;
  6. use Sentry\Tracing\TransactionSource;
  7. use Symfony\Component\HttpFoundation\Request;
  8. use Symfony\Component\HttpKernel\Event\RequestEvent;
  9. use Symfony\Component\HttpKernel\Event\TerminateEvent;
  10. /**
  11.  * This event listener acts on the master requests and starts a transaction
  12.  * to report performance data to Sentry. It gathers useful data like the
  13.  * HTTP status code of the response or the name of the route that handles
  14.  * the request and add them as tags.
  15.  */
  16. final class TracingRequestListener extends AbstractTracingRequestListener
  17. {
  18.     /**
  19.      * This method is called for each subrequest handled by the framework and
  20.      * starts a new {@see Transaction}.
  21.      *
  22.      * @param RequestEvent $event The event
  23.      */
  24.     public function handleKernelRequestEvent(RequestEvent $event): void
  25.     {
  26.         if (!$this->isMainRequest($event)) {
  27.             return;
  28.         }
  29.         /** @var Request $request */
  30.         $request $event->getRequest();
  31.         /** @var float $requestStartTime */
  32.         $requestStartTime $request->server->get('REQUEST_TIME_FLOAT'microtime(true));
  33.         $context TransactionContext::fromHeaders(
  34.             $request->headers->get('sentry-trace'''),
  35.             $request->headers->get('baggage''')
  36.         );
  37.         $context->setOp('http.server');
  38.         $routeName $request->attributes->get('_route');
  39.         if (null !== $routeName && \is_string($routeName)) {
  40.             $context->setName(sprintf('%s %s'$request->getMethod(), $routeName));
  41.             $context->setSource(TransactionSource::route());
  42.         } else {
  43.             $context->setName(sprintf('%s %s%s%s'$request->getMethod(), $request->getSchemeAndHttpHost(), $request->getBaseUrl(), $request->getPathInfo()));
  44.             $context->setSource(TransactionSource::url());
  45.         }
  46.         $context->setStartTimestamp($requestStartTime);
  47.         $context->setTags($this->getTags($request));
  48.         $this->hub->setSpan($this->hub->startTransaction($context));
  49.     }
  50.     /**
  51.      * This method is called for each request handled by the framework and
  52.      * ends the tracing on terminate after the client received the response.
  53.      *
  54.      * @param TerminateEvent $event The event
  55.      */
  56.     public function handleKernelTerminateEvent(TerminateEvent $event): void
  57.     {
  58.         $transaction $this->hub->getTransaction();
  59.         if (null === $transaction) {
  60.             return;
  61.         }
  62.         $transaction->finish();
  63.     }
  64.     /**
  65.      * Gets the tags to attach to the transaction.
  66.      *
  67.      * @param Request $request The HTTP request
  68.      *
  69.      * @return array<string, string>
  70.      */
  71.     private function getTags(Request $request): array
  72.     {
  73.         $client $this->hub->getClient();
  74.         $httpFlavor $this->getHttpFlavor($request);
  75.         $tags = [
  76.             'net.host.port' => (string) $request->getPort(),
  77.             'http.method' => $request->getMethod(),
  78.             'http.url' => $request->getUri(),
  79.             'route' => $this->getRouteName($request),
  80.         ];
  81.         if (null !== $httpFlavor) {
  82.             $tags['http.flavor'] = $httpFlavor;
  83.         }
  84.         if (false !== filter_var($request->getHost(), \FILTER_VALIDATE_IP)) {
  85.             $tags['net.host.ip'] = $request->getHost();
  86.         } else {
  87.             $tags['net.host.name'] = $request->getHost();
  88.         }
  89.         if (null !== $request->getClientIp() && null !== $client && $client->getOptions()->shouldSendDefaultPii()) {
  90.             $tags['net.peer.ip'] = $request->getClientIp();
  91.         }
  92.         return $tags;
  93.     }
  94.     /**
  95.      * Gets the HTTP flavor from the request.
  96.      *
  97.      * @param Request $request The HTTP request
  98.      */
  99.     private function getHttpFlavor(Request $request): ?string
  100.     {
  101.         $protocolVersion $request->getProtocolVersion();
  102.         if (null !== $protocolVersion && str_starts_with($protocolVersion'HTTP/')) {
  103.             return substr($protocolVersion, \strlen('HTTP/'));
  104.         }
  105.         return $protocolVersion;
  106.     }
  107. }