Repository navigation
Expand file tree
/
Copy pathHttpRequestLifecycle.php
More file actions
182 lines (152 loc) · 4.8 KB
/
Copy pathHttpRequestLifecycle.php
File metadata and controls
182 lines (152 loc) · 4.8 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
namespace Perfbase\Drupal\Lifecycle;
use Drupal\Core\Session\AccountInterface;
use Perfbase\Drupal\Config\ConfigResolver;
use Perfbase\Drupal\Runtime\PerfbaseFactoryInterface;
use Perfbase\Drupal\Support\ErrorHandler;
use Perfbase\Drupal\Support\FilterMatcher;
use Perfbase\Drupal\Support\SpanNaming;
use Perfbase\SDK\Utils\EnvironmentUtils;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Route;
/**
* Profiles a main HTTP request.
*
* The trace starts on kernel.request before routing has run, so route-based
* decisions (admin-route exclusion, include filters naming routes) and
* route-based attributes are evaluated again when the request terminates.
*/
class HttpRequestLifecycle extends AbstractDrupalProfiler {
private Request $request;
private ?AccountInterface $currentUser;
private ?int $responseStatusCode = NULL;
public function __construct(
Request $request,
PerfbaseFactoryInterface $factory,
ConfigResolver $configResolver,
ErrorHandler $errorHandler,
?AccountInterface $currentUser = NULL,
) {
$this->request = $request;
$this->currentUser = $currentUser;
parent::__construct(
SpanNaming::forHttp($request),
$factory,
$configResolver,
$errorHandler
);
}
/**
*
*/
public function setResponse(Response $response): void {
$this->responseStatusCode = $response->getStatusCode();
$this->setAttribute('http_status_code', (string) $this->responseStatusCode);
}
/**
* Start-time checks.
*
* Routing has not run yet, so include filters (which may name routes) are
* not evaluated here; they are applied before submission. Exclusions that
* already match the path can safely skip the request early.
*/
protected function shouldProfile(): bool {
if (!$this->configResolver->isEnabled($this->config)) {
return FALSE;
}
if (empty($this->config['profile_http']) || !$this->isProfilerAvailable()) {
return FALSE;
}
if (!empty($this->config['exclude_admin_routes']) && $this->isAdminRoute()) {
return FALSE;
}
$excludes = $this->config['exclude']['http'];
return $excludes === [] || !FilterMatcher::matches(SpanNaming::httpFilterComponents($this->request), $excludes);
}
/**
*
*/
protected function setDefaultAttributes(): void {
parent::setDefaultAttributes();
$this->setAttributes([
'source' => 'http',
'environment' => $this->getEnvironment(),
'app_version' => $this->getAppVersion(),
'http_method' => strtoupper($this->request->getMethod()),
'http_url' => SpanNaming::httpUrl($this->request),
'user_ip' => EnvironmentUtils::getUserIp() ?: '',
'user_agent' => EnvironmentUtils::getUserUserAgent() ?: '',
]);
$this->setRouteAttributes();
}
/**
* Route and user details are only known after routing and authentication.
*/
protected function beforeStop(): void {
$this->setRouteAttributes();
}
/**
*
*/
protected function shouldSubmitTrace(): bool {
if ($this->responseStatusCode !== NULL
&& !in_array($this->responseStatusCode, $this->config['profile_http_status_codes'], TRUE)) {
return FALSE;
}
if (!empty($this->config['exclude_admin_routes']) && $this->isAdminRoute()) {
return FALSE;
}
return FilterMatcher::passesFilters(
SpanNaming::httpFilterComponents($this->request),
$this->config['include'],
$this->config['exclude'],
'http'
);
}
/**
*
*/
private function setRouteAttributes(): void {
$this->setAttribute('action', SpanNaming::actionForHttp($this->request));
$routeName = $this->request->attributes->get('_route');
if (is_string($routeName) && $routeName !== '') {
$this->setAttribute('drupal.route_name', $routeName);
}
$userId = $this->resolveUserId();
if ($userId !== NULL) {
$this->setAttribute('user_id', $userId);
}
}
/**
*
*/
private function resolveUserId(): ?string {
if ($this->currentUser === NULL) {
return NULL;
}
try {
if (!$this->currentUser->isAuthenticated()) {
return NULL;
}
$id = $this->currentUser->id();
}
catch (\Throwable $exception) {
return NULL;
}
return is_int($id) || is_string($id) ? (string) $id : NULL;
}
/**
* Whether the matched route is an administrative route.
*
* Drupal stores this as the `_admin_route` option on the route object (set
* for /admin paths and entity forms), not as a request attribute.
*/
private function isAdminRoute(): bool {
$routeObject = $this->request->attributes->get('_route_object');
if ($routeObject instanceof Route) {
return (bool) $routeObject->getOption('_admin_route');
}
return FALSE;
}
}