vendor/sentry/sentry-symfony/src/EventListener/TracingSubRequestListener.php line 24

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sentry\SentryBundle\EventListener;
  4. use Sentry\Tracing\Span;
  5. use Sentry\Tracing\SpanContext;
  6. use Symfony\Component\HttpKernel\Event\FinishRequestEvent;
  7. use Symfony\Component\HttpKernel\Event\RequestEvent;
  8. /**
  9. * This event listener acts on the sub requests and starts a child span of the
  10. * current transaction to gather performance data for each of them.
  11. */
  12. final class TracingSubRequestListener extends AbstractTracingRequestListener
  13. {
  14. /**
  15. * This method is called for each subrequest handled by the framework and
  16. * traces each by starting a new {@see Span}.
  17. *
  18. * @param RequestEvent $event The event
  19. */
  20. public function handleKernelRequestEvent(RequestEvent $event): void
  21. {
  22. if ($this->isMainRequest($event)) {
  23. return;
  24. }
  25. $request = $event->getRequest();
  26. $span = $this->hub->getSpan();
  27. if (null === $span) {
  28. return;
  29. }
  30. $spanContext = new SpanContext();
  31. $spanContext->setOp('http.server');
  32. $spanContext->setDescription(sprintf('%s %s%s%s', $request->getMethod(), $request->getSchemeAndHttpHost(), $request->getBaseUrl(), $request->getPathInfo()));
  33. $spanContext->setData([
  34. 'http.request.method' => $request->getMethod(),
  35. 'http.url' => $request->getUri(),
  36. 'route' => $this->getRouteName($request),
  37. ]);
  38. $this->hub->setSpan($span->startChild($spanContext));
  39. }
  40. /**
  41. * This method is called for each subrequest handled by the framework and
  42. * ends the tracing.
  43. *
  44. * @param FinishRequestEvent $event The event
  45. */
  46. public function handleKernelFinishRequestEvent(FinishRequestEvent $event): void
  47. {
  48. if ($this->isMainRequest($event)) {
  49. return;
  50. }
  51. $span = $this->hub->getSpan();
  52. if (null === $span) {
  53. return;
  54. }
  55. $span->finish();
  56. }
  57. }