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

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