-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpRequestLifecycle.php
More file actions
149 lines (124 loc) · 4.41 KB
/
HttpRequestLifecycle.php
File metadata and controls
149 lines (124 loc) · 4.41 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<?php
declare(strict_types=1);
namespace Perfbase\Symfony\Lifecycle;
use Perfbase\Symfony\Profiling\AbstractProfiler;
use Perfbase\Symfony\Support\FilterMatcher;
use Perfbase\Symfony\Support\PerfbaseClientProvider;
use Perfbase\Symfony\Support\PerfbaseErrorHandler;
use Perfbase\Symfony\Support\SpanNaming;
use Perfbase\SDK\Utils\EnvironmentUtils;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\User\UserInterface;
class HttpRequestLifecycle extends AbstractProfiler
{
private Request $request;
private ?string $routeTemplate;
private ?string $routeName;
private ?string $controller;
private ?TokenStorageInterface $tokenStorage;
/**
* @param array<string, mixed> $config
*/
public function __construct(
Request $request,
?string $routeTemplate,
?string $routeName,
?string $controller,
PerfbaseClientProvider $clientProvider,
PerfbaseErrorHandler $errorHandler,
array $config,
string $environment,
string $appVersion,
?TokenStorageInterface $tokenStorage = null
) {
parent::__construct(
SpanNaming::forHttp($request, $routeTemplate, $routeName),
$clientProvider,
$errorHandler,
$config,
$environment,
$appVersion
);
$this->request = $request;
$this->routeTemplate = $routeTemplate;
$this->routeName = $routeName;
$this->controller = $controller;
$this->tokenStorage = $tokenStorage;
}
public function setResponseStatusCode(int $statusCode): void
{
$this->setAttribute('http_status_code', (string) $statusCode);
}
protected function shouldProfile(): bool
{
if (!(bool) ($this->config['enabled'] ?? false)) {
return false;
}
return FilterMatcher::passesFilters(
$this->getRequestComponents(),
$this->normalizeFilters($this->config['include']['http'] ?? ['*']),
$this->normalizeFilters($this->config['exclude']['http'] ?? [])
);
}
protected function setDefaultAttributes(): void
{
parent::setDefaultAttributes();
$actionTarget = $this->routeTemplate ?: ($this->routeName ?: $this->getRequestPath());
$attributes = [
'source' => 'http',
'action' => sprintf('%s %s', $this->request->getMethod(), $actionTarget),
'http_method' => $this->request->getMethod(),
'http_url' => $this->request->getSchemeAndHttpHost() . $this->getRequestPath(),
'user_ip' => (string) (EnvironmentUtils::getUserIp() ?? ''),
'user_agent' => (string) (EnvironmentUtils::getUserUserAgent() ?? ''),
];
$this->setAttributes($attributes);
$userIdentifier = $this->resolveUserIdentifier();
if ($userIdentifier !== null && $userIdentifier !== '') {
$this->setAttribute('user_id', $userIdentifier);
}
}
/**
* @return array<string>
*/
private function getRequestComponents(): array
{
$path = $this->getRequestPath();
$components = [
$path,
sprintf('%s %s', $this->request->getMethod(), $path),
];
if ($this->routeTemplate !== null && $this->routeTemplate !== '') {
$components[] = $this->routeTemplate;
$components[] = sprintf('%s %s', $this->request->getMethod(), $this->routeTemplate);
}
if ($this->routeName !== null && $this->routeName !== '') {
$components[] = $this->routeName;
}
if ($this->controller !== null && $this->controller !== '') {
$components[] = $this->controller;
}
return array_values(array_unique($components));
}
private function getRequestPath(): string
{
$path = $this->request->getPathInfo();
return $path === '' ? '/' : $path;
}
private function resolveUserIdentifier(): ?string
{
if ($this->tokenStorage === null) {
return null;
}
$token = $this->tokenStorage->getToken();
if ($token === null) {
return null;
}
$user = $token->getUser();
if (!$user instanceof UserInterface) {
return null;
}
return $user->getUserIdentifier();
}
}