/
AbstractTracingRequestListener.php
76 lines (62 loc) · 2.01 KB
/
AbstractTracingRequestListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
declare(strict_types=1);
namespace Sentry\SentryBundle\EventListener;
use Sentry\State\HubInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\Routing\Route;
abstract class AbstractTracingRequestListener
{
use KernelEventForwardCompatibilityTrait;
/**
* @var HubInterface The current hub
*/
protected $hub;
/**
* Constructor.
*
* @param HubInterface $hub The current hub
*/
public function __construct(HubInterface $hub)
{
$this->hub = $hub;
}
/**
* This method is called once a response for the current HTTP request is
* created, but before it is sent off to the client. Its use is mainly for
* gathering information like the HTTP status code and attaching them as
* tags of the span/transaction.
*
* @param ResponseEvent $event The event
*/
public function handleKernelResponseEvent(ResponseEvent $event): void
{
$response = $event->getResponse();
$span = $this->hub->getSpan();
if (null === $span) {
return;
}
$span->setHttpStatus($response->getStatusCode());
}
/**
* Gets the name of the route or fallback to the controller FQCN if the
* route is anonymous (e.g. a subrequest).
*
* @param Request $request The HTTP request
*/
protected function getRouteName(Request $request): string
{
$route = $request->attributes->get('_route');
if ($route instanceof Route) {
$route = $route->getPath();
}
if (null === $route) {
$route = $request->attributes->get('_controller');
if (\is_array($route) && \is_callable($route, true)) {
$route = sprintf('%s::%s', \is_object($route[0]) ? get_debug_type($route[0]) : $route[0], $route[1]);
}
}
return \is_string($route) ? $route : '<unknown>';
}
}