From cd9603f9509aeaf9b9278fb1b68ad4fc9b5963a2 Mon Sep 17 00:00:00 2001 From: Hector Mendoza Jacobo Date: Mon, 18 Nov 2024 22:19:54 +0000 Subject: [PATCH] Add milliseconds via microtime to the LogEvent class for latency calculations --- src/HttpHandler/Guzzle6HttpHandler.php | 4 ++-- src/Logging/LogEvent.php | 20 ++++++++++++++------ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/HttpHandler/Guzzle6HttpHandler.php b/src/HttpHandler/Guzzle6HttpHandler.php index 80a79ef86..575f092c5 100644 --- a/src/HttpHandler/Guzzle6HttpHandler.php +++ b/src/HttpHandler/Guzzle6HttpHandler.php @@ -76,7 +76,7 @@ public function __invoke(RequestInterface $request, array $options = []) $response = $this->client->send($request, $options); if ($this->logger) { - $responseEvent = new LogEvent($requestEvent->timestamp); + $responseEvent = new LogEvent($requestEvent->milliseconds); $responseEvent->headers = $response->getHeaders(); $responseEvent->payload = $response->getBody()->getContents(); @@ -121,7 +121,7 @@ public function async(RequestInterface $request, array $options = []) if ($this->logger) { $promise->then(function (ResponseInterface $response) use ($requestEvent) { - $responseEvent = new LogEvent($requestEvent->timestamp); + $responseEvent = new LogEvent($requestEvent->milliseconds); $responseEvent->headers = $response->getHeaders(); $responseEvent->payload = $response->getBody()->getContents(); diff --git a/src/Logging/LogEvent.php b/src/Logging/LogEvent.php index 3007ce2e4..b65550748 100644 --- a/src/Logging/LogEvent.php +++ b/src/Logging/LogEvent.php @@ -22,9 +22,16 @@ class LogEvent /** * Timestamp in format RFC3339 representing when this event ocurred * - * @var null|string + * @var string + */ + public readonly string $timestamp; + + /** + * The time in milliseconds at time on creation for calculating latency + * + * @var float */ - public null|string $timestamp = null; + public readonly float $milliseconds; /** * Rest method type @@ -90,14 +97,14 @@ class LogEvent public null|string $serviceName = null; /** - * The Client Id for easy trace + * The Client Id for tracing * * @var null|int $clientId */ public null|int $clientId = null; /** - * The Request id for easy trace + * The Request id for tracing * * @var null|int $requestId; */ @@ -110,12 +117,13 @@ class LogEvent * * @param null|string $startTime (Optional) Parameter to calculate the latency */ - public function __construct(null|string $startTime = null) + public function __construct(null|float $startTime = null) { $this->timestamp = date(DATE_RFC3339); + $this->milliseconds = round(microtime(true) * 1000); if ($startTime) { - $this->latency = (int) strtotime($this->timestamp) - strtotime($startTime); + $this->latency = (int) $this->milliseconds - $startTime; } } }