Skip to content

Commit b245108

Browse files
committed
Add request id with tests and user guide
1 parent 56d7160 commit b245108

5 files changed

Lines changed: 69 additions & 0 deletions

File tree

‎system/HTTP/IncomingRequest.php‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@
4949
*/
5050
class IncomingRequest extends Request
5151
{
52+
/**
53+
* The Request ID associated with this request useful for
54+
* debugging, traceability, and logging.
55+
*
56+
* Incoming request's X-Request-Id header will get priority
57+
* over automatically generated request ids.
58+
*/
59+
protected string $requestId;
60+
5261
/**
5362
* The URI for this request.
5463
*
@@ -158,6 +167,7 @@ public function __construct($config, ?URI $uri = null, $body = 'php://input', ?U
158167
$body = null;
159168
}
160169

170+
$this->requestId = $this->hasHeader('X-Request-ID') ? $this->getHeaderLine('X-Request-ID') : $this->generateRequestId();
161171
$this->uri = $uri;
162172
$this->body = $body;
163173
$this->userAgent = $userAgent;
@@ -179,6 +189,11 @@ public function __clone()
179189
$this->input = null;
180190
}
181191

192+
private function generateRequestId(): string
193+
{
194+
return bin2hex(random_bytes(16));
195+
}
196+
182197
private function getPostMaxSize(): int
183198
{
184199
$postMaxSize = ini_get('post_max_size');
@@ -191,6 +206,11 @@ private function getPostMaxSize(): int
191206
};
192207
}
193208

209+
public function getRequestId(): string
210+
{
211+
return $this->requestId;
212+
}
213+
194214
/**
195215
* Handles setting up the locale, perhaps auto-detecting through
196216
* content negotiation.

‎tests/system/HTTP/IncomingRequestTest.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,23 @@ private function createRequest(?App $config = null, $body = null, ?string $path
6262
return new IncomingRequest($config, $uri, $body, new UserAgent());
6363
}
6464

65+
public function testAutoGeneratedRequestId(): void
66+
{
67+
$this->assertInstanceOf(IncomingRequest::class, $this->request);
68+
$this->assertNotEmpty($this->request->getRequestId());
69+
$this->assertSame(32, strlen($this->request->getRequestId()));
70+
}
71+
72+
public function testPassedRequestIdDoesntGetReplaced(): void
73+
{
74+
service('superglobals')->setServer('HTTP_X_REQUEST_ID', 'custom-value');
75+
76+
$config = new App();
77+
$request = new IncomingRequest($config, new SiteURI($config), null, new UserAgent());
78+
79+
$this->assertSame('custom-value', $request->getRequestId());
80+
}
81+
6582
public function testCanGrabRequestVars(): void
6683
{
6784
service('superglobals')->setRequest('TEST', '5');

‎user_guide_src/source/changelogs/v4.8.0.rst‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,7 @@ HTTP
295295
Now you can simply do ``new URI($uri, true)``.
296296
- ``CLIRequest`` now supports parsing array options written multiple times (e.g., ``--option=value1 --option=value2``) into an array of values. This allows you to easily pass multiple values for the same option without needing to use a comma-separated string.
297297
When used with ``CLIRequest::getOption()``, an array option will return its last value (for example, in this case, ``value2``). To retrieve all values for an array option, use ``CLIRequest::getRawOption()``.
298+
- ``IncomingRequest`` now generates a unique request ID for each request and provides it via the new ``getRequestId()`` method. If the request includes an ``X-Request-ID`` header, that value will be used as the request ID instead of generating a new one. See :ref:`request-id` for details.
298299

299300
Validation
300301
==========

‎user_guide_src/source/incoming/incomingrequest.rst‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,27 @@ You can also check if the request was made through and HTTPS connection with the
7676

7777
.. literalinclude:: incomingrequest/006.php
7878

79+
.. _request-id:
80+
81+
Getting Request ID
82+
******************
83+
84+
.. versionadded:: 4.8.0
85+
86+
You can get the unique request ID for this request with the ``getRequestID()`` method.
87+
This ID is generated by the framework and is unique to this request.
88+
It can be used for logging or debugging purposes to track a specific request through the application.
89+
The ID is generated via ``bin2hex(random_bytes(16))``, so it is a 32-character hexadecimal string.
90+
91+
.. note:: If the incoming request has a header named ``X-Request-ID``, the value of that header
92+
will be used as the request ID instead of generating a new one. This allows you to pass in
93+
your own request ID if you have one available, such as from a client or a load balancer.
94+
95+
.. code-block:: php
96+
97+
// Returns the request ID, either from the header or generated by the framework
98+
$requestId = $request->getRequestID();
99+
79100
Retrieving Input
80101
****************
81102

@@ -376,6 +397,13 @@ The methods provided by the parent classes that are available are:
376397
:returns: True if the request is an HTTPS request, otherwise false.
377398
:rtype: bool
378399

400+
.. php:method:: getRequestId()
401+
402+
:returns: The unique request ID for this request, either from the ``X-Request-ID`` header or generated by the framework.
403+
:rtype: string
404+
405+
The request ID is a 32-character hexadecimal string (or incoming header value if provided).
406+
379407
.. php:method:: getVar([$index = null[, $filter = null[, $flags = null]]])
380408
381409
:param string $index: The name of the variable/key to look for.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
$requestId = $request->getRequestId();

0 commit comments

Comments
 (0)