Skip to content

Commit

Permalink
Add milliseconds via microtime to the LogEvent class for latency calc…
Browse files Browse the repository at this point in the history
…ulations
  • Loading branch information
Hectorhammett committed Nov 18, 2024
1 parent 658b118 commit cd9603f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/HttpHandler/Guzzle6HttpHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
20 changes: 14 additions & 6 deletions src/Logging/LogEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
*/
Expand All @@ -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)

Check failure on line 120 in src/Logging/LogEvent.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis / PHPStan Static Analysis

PHPDoc tag @param for parameter $startTime with type string|null is not subtype of native type float|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;

Check failure on line 126 in src/Logging/LogEvent.php

View workflow job for this annotation

GitHub Actions / PHPStan Static Analysis / PHPStan Static Analysis

Property Google\Auth\Logging\LogEvent::$latency (int|null) does not accept float.
}
}
}

0 comments on commit cd9603f

Please sign in to comment.