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

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