/
TracingRequestListener.php
120 lines (101 loc) · 3.76 KB
/
TracingRequestListener.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
declare(strict_types=1);
namespace Sentry\SentryBundle\EventListener;
use Sentry\Tracing\Transaction;
use Sentry\Tracing\TransactionContext;
use Sentry\Tracing\TransactionSource;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\TerminateEvent;
/**
* This event listener acts on the master requests and starts a transaction
* to report performance data to Sentry. It gathers useful data like the
* HTTP status code of the response or the name of the route that handles
* the request and add them as tags.
*/
final class TracingRequestListener extends AbstractTracingRequestListener
{
/**
* This method is called for each subrequest handled by the framework and
* starts a new {@see Transaction}.
*
* @param RequestEvent $event The event
*/
public function handleKernelRequestEvent(RequestEvent $event): void
{
if (!$this->isMainRequest($event)) {
return;
}
/** @var Request $request */
$request = $event->getRequest();
/** @var float $requestStartTime */
$requestStartTime = $request->server->get('REQUEST_TIME_FLOAT', microtime(true));
$context = TransactionContext::fromHeaders(
$request->headers->get('sentry-trace', ''),
$request->headers->get('baggage', '')
);
$context->setOp('http.server');
$context->setName(sprintf('%s %s%s%s', $request->getMethod(), $request->getSchemeAndHttpHost(), $request->getBaseUrl(), $request->getPathInfo()));
$context->setSource(TransactionSource::url());
$context->setStartTimestamp($requestStartTime);
$context->setTags($this->getTags($request));
$this->hub->setSpan($this->hub->startTransaction($context));
}
/**
* This method is called for each request handled by the framework and
* ends the tracing on terminate after the client received the response.
*
* @param TerminateEvent $event The event
*/
public function handleKernelTerminateEvent(TerminateEvent $event): void
{
$transaction = $this->hub->getTransaction();
if (null === $transaction) {
return;
}
$transaction->finish();
}
/**
* Gets the tags to attach to the transaction.
*
* @param Request $request The HTTP request
*
* @return array<string, string>
*/
private function getTags(Request $request): array
{
$client = $this->hub->getClient();
$httpFlavor = $this->getHttpFlavor($request);
$tags = [
'net.host.port' => (string) $request->getPort(),
'http.method' => $request->getMethod(),
'http.url' => $request->getUri(),
'route' => $this->getRouteName($request),
];
if (null !== $httpFlavor) {
$tags['http.flavor'] = $httpFlavor;
}
if (false !== filter_var($request->getHost(), \FILTER_VALIDATE_IP)) {
$tags['net.host.ip'] = $request->getHost();
} else {
$tags['net.host.name'] = $request->getHost();
}
if (null !== $request->getClientIp() && null !== $client && $client->getOptions()->shouldSendDefaultPii()) {
$tags['net.peer.ip'] = $request->getClientIp();
}
return $tags;
}
/**
* Gets the HTTP flavor from the request.
*
* @param Request $request The HTTP request
*/
private function getHttpFlavor(Request $request): ?string
{
$protocolVersion = $request->getProtocolVersion();
if (null !== $protocolVersion && str_starts_with($protocolVersion, 'HTTP/')) {
return substr($protocolVersion, \strlen('HTTP/'));
}
return $protocolVersion;
}
}