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