|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Sentry\SentryBundle\EventListener\Tracing; |
| 6 | + |
| 7 | +use Sentry\SentryBundle\EventListener\RequestListenerControllerEvent; |
| 8 | +use Sentry\SentryBundle\EventListener\RequestListenerRequestEvent; |
| 9 | +use Sentry\SentryBundle\EventListener\RequestListenerResponseEvent; |
| 10 | +use Sentry\SentryBundle\EventListener\RequestListenerTerminateEvent; |
| 11 | +use Sentry\State\HubInterface; |
| 12 | +use Sentry\Tracing\Span; |
| 13 | +use Sentry\Tracing\SpanContext; |
| 14 | +use Sentry\Tracing\Transaction; |
| 15 | +use Sentry\Tracing\TransactionContext; |
| 16 | +use Symfony\Component\HttpFoundation\Request; |
| 17 | + |
| 18 | +final class RequestListener |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var HubInterface The current hub |
| 22 | + */ |
| 23 | + private $hub; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var Transaction |
| 27 | + */ |
| 28 | + private $transaction; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var Span |
| 32 | + */ |
| 33 | + private $requestSpan; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var Span |
| 37 | + */ |
| 38 | + private $controllerSpan; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var Span |
| 42 | + */ |
| 43 | + private $responseSpan; |
| 44 | + |
| 45 | + /** |
| 46 | + * Constructor. |
| 47 | + * |
| 48 | + * @param HubInterface $hub The current hub |
| 49 | + */ |
| 50 | + public function __construct(HubInterface $hub) |
| 51 | + { |
| 52 | + $this->hub = $hub; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @param RequestListenerRequestEvent $event The event |
| 57 | + */ |
| 58 | + public function handleKernelRequestEvent(RequestListenerRequestEvent $event): void |
| 59 | + { |
| 60 | + if (!$event->isMasterRequest()) { |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + /** @var Request $request */ |
| 65 | + $request = $event->getRequest(); |
| 66 | + $requestStartTime = $request->server->get('REQUEST_TIME_FLOAT', microtime(true)); |
| 67 | + |
| 68 | + $context = new TransactionContext(); |
| 69 | + $context->setOp('http.server'); |
| 70 | + $context->setName($request->getUri()); |
| 71 | + $context->setData([ |
| 72 | + 'url' => $request->getUri(), |
| 73 | + 'method' => strtoupper($request->getMethod()), |
| 74 | + ]); |
| 75 | + $context->setStartTimestamp($requestStartTime); |
| 76 | + |
| 77 | + $transaction = $this->hub->startTransaction($context); |
| 78 | + |
| 79 | + // Setting the Transaction on the Hub |
| 80 | + $this->hub->setSpan($transaction); |
| 81 | + |
| 82 | + $spanContext = new SpanContext(); |
| 83 | + $spanContext->setOp('kernel.request'); |
| 84 | + $spanContext->setStartTimestamp($requestStartTime); |
| 85 | + |
| 86 | + $this->requestSpan = $transaction->startChild($spanContext); |
| 87 | + $this->transaction = $transaction; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param RequestListenerControllerEvent $event The event |
| 92 | + */ |
| 93 | + public function handleKernelControllerEvent(RequestListenerControllerEvent $event): void |
| 94 | + { |
| 95 | + if (!$event->isMasterRequest() || !$this->transaction) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + if ($this->requestSpan) { |
| 100 | + $this->requestSpan->finish(); |
| 101 | + } |
| 102 | + |
| 103 | + $spanContext = new SpanContext(); |
| 104 | + $spanContext->setOp('controller'); |
| 105 | + |
| 106 | + $this->controllerSpan = $this->transaction->startChild($spanContext); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @param RequestListenerResponseEvent $event The event |
| 111 | + */ |
| 112 | + public function handleKernelResponseEvent(RequestListenerResponseEvent $event): void |
| 113 | + { |
| 114 | + if (!$event->isMasterRequest() || !$this->transaction) { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + $this->transaction->setHttpStatus($event->getResponse()->getStatusCode()); |
| 119 | + |
| 120 | + if ($this->controllerSpan) { |
| 121 | + $this->controllerSpan->finish(); |
| 122 | + } |
| 123 | + |
| 124 | + $spanContext = new SpanContext(); |
| 125 | + $spanContext->setOp('kernel.response'); |
| 126 | + |
| 127 | + $this->responseSpan = $this->transaction->startChild($spanContext); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @param RequestListenerTerminateEvent $event The event |
| 132 | + */ |
| 133 | + public function handleKernelTerminateEvent(RequestListenerTerminateEvent $event): void |
| 134 | + { |
| 135 | + if (!$this->transaction) { |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + if ($this->responseSpan) { |
| 140 | + $this->responseSpan->finish(); |
| 141 | + } |
| 142 | + |
| 143 | + $this->transaction->finish(); |
| 144 | + } |
| 145 | +} |
0 commit comments