]> git.mxchange.org Git - friendica.git/commitdiff
Use X-REQUEST-ID for Logging
authorPhilipp <admin@philipp.info>
Sun, 25 Dec 2022 18:19:06 +0000 (19:19 +0100)
committerPhilipp <admin@philipp.info>
Mon, 26 Dec 2022 20:18:04 +0000 (21:18 +0100)
src/App/Request.php
src/Core/Logger/Util/Introspection.php

index 71b6a9ea3f088f4d160c76ccde3253a69979ff08..1af1206d1f6a66c1ba0df18049fb917a3e59359d 100644 (file)
@@ -22,6 +22,7 @@
 namespace Friendica\App;
 
 use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\System;
 
 /**
  * Container for the whole request
@@ -38,9 +39,17 @@ class Request
         * @var string
         */
        const DEFAULT_FORWARD_FOR_HEADER = 'HTTP_X_FORWARDED_FOR';
+       /**
+        * The default Request-ID header to retrieve the current transaction ID from the HTTP header (if set)
+        *
+        * @var string
+        */
+       const DEFAULT_REQUEST_ID_HEADER = 'HTTP_X_REQUEST_ID';
 
        /** @var string The remote IP address of the current request */
        protected $remoteAddress;
+       /** @var string The request-id of the current request */
+       protected $requestId;
 
        /**
         * @return string The remote IP address of the current request
@@ -52,9 +61,20 @@ class Request
                return $this->remoteAddress;
        }
 
+       /**
+        * @return string The request ID of the current request
+        *
+        * Do always use this instead of $_SERVER['X_REQUEST_ID']
+        */
+       public function getRequestId(): string
+       {
+               return $this->requestId;
+       }
+
        public function __construct(IManageConfigValues $config, array $server = [])
        {
                $this->remoteAddress = $this->determineRemoteAddress($config, $server);
+               $this->requestId = $server[static::DEFAULT_REQUEST_ID_HEADER] ?? System::createGUID(8);
        }
 
        /**
index 444af2409e67474f05008f96c3dbb936cc1aa6ac..987a4e3cbadd1d13fd9ef1e1760aaba689f96e36 100644 (file)
@@ -21,6 +21,7 @@
 
 namespace Friendica\Core\Logger\Util;
 
+use Friendica\App\Request;
 use Friendica\Core\Logger\Capabilities\IHaveCallIntrospections;
 
 /**
@@ -28,6 +29,9 @@ use Friendica\Core\Logger\Capabilities\IHaveCallIntrospections;
  */
 class Introspection implements IHaveCallIntrospections
 {
+       /** @var string */
+       private $requestId;
+
        /** @var int  */
        private $skipStackFramesCount;
 
@@ -43,8 +47,9 @@ class Introspection implements IHaveCallIntrospections
         * @param string[] $skipClassesPartials  An array of classes to skip during logging
         * @param int      $skipStackFramesCount If the logger should use information from other hierarchy levels of the call
         */
-       public function __construct(array $skipClassesPartials = [], int $skipStackFramesCount = 0)
+       public function __construct(Request $request, array $skipClassesPartials = [], int $skipStackFramesCount = 0)
        {
+               $this->requestId            = $request->getRequestId();
                $this->skipClassesPartials  = $skipClassesPartials;
                $this->skipStackFramesCount = $skipStackFramesCount;
        }
@@ -77,9 +82,10 @@ class Introspection implements IHaveCallIntrospections
                $i += $this->skipStackFramesCount;
 
                return [
-                       'file'     => isset($trace[$i - 1]['file']) ? basename($trace[$i - 1]['file']) : null,
-                       'line'     => $trace[$i - 1]['line'] ?? null,
-                       'function' => $trace[$i]['function'] ?? null,
+                       'file'       => isset($trace[$i - 1]['file']) ? basename($trace[$i - 1]['file']) : null,
+                       'line'       => $trace[$i - 1]['line'] ?? null,
+                       'function'   => $trace[$i]['function'] ?? null,
+                       'request-id' => $this->requestId,
                ];
        }