Skip to content

Commit

Permalink
Change the array type of payload to string
Browse files Browse the repository at this point in the history
  • Loading branch information
Hectorhammett committed Nov 25, 2024
1 parent 37a98a1 commit ab90bf7
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 29 deletions.
45 changes: 19 additions & 26 deletions src/Logging/LoggingTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
trait LoggingTrait
{
/**
* @param LogEvent $event
* @param RpcLogEvent $event
*/
private function logRequest(RpcLogEvent $event): void
{
Expand All @@ -39,7 +39,7 @@ private function logRequest(RpcLogEvent $event): void
'request.method' => $event->method,
'request.url' => $event->url,
'request.headers' => $event->headers,
'request.payload' => $event->payload,
'request.payload' => $this->truncatePayload($event->payload),
'request.jwt' => $this->getJwtToken($event->headers ?? []),
'retryAttempt' => $event->retryAttempt
];
Expand All @@ -58,7 +58,7 @@ private function logRequest(RpcLogEvent $event): void
}

/**
* @param LogEvent $event
* @param RpcLogEvent $event
*/
private function logResponse(RpcLogEvent $event): void
{
Expand All @@ -69,7 +69,7 @@ private function logResponse(RpcLogEvent $event): void
'requestId' => $event->requestId ?? null,
'jsonPayload' => [
'response.headers' => $event->headers,
'response.payload' => $event->payload,
'response.payload' => $this->truncatePayload($event->payload),
'latency' => $event->latency,
]
];
Expand All @@ -89,32 +89,12 @@ private function logResponse(RpcLogEvent $event): void
}

if (!is_null($event->status)) {
$infoEvent = [
'timestamp' => $event->timestamp,
'severity' => LogLevel::INFO,
'clientId' => $event->clientId,
'requestId' => $event->requestId ?? null,
'jsonPayload' => [
'response.status' => $event->status
]
];

// Remove null values
$infoEvent = array_filter($infoEvent, fn ($value) => !is_null($value));

$stringifiedEvent = json_encode($infoEvent);

// There was an error stringifying the event, return to not break execution
if ($stringifiedEvent === false) {
return;
}

$this->logger->info($stringifiedEvent);
$this->logStatus($event);
}
}

/**
* @param LogEvent $event
* @param RpcLogEvent $event
*/
private function logStatus(RpcLogEvent $event): void
{
Expand Down Expand Up @@ -168,4 +148,17 @@ private function getJwtToken(array $headers): null|array
'token' => base64_decode($token)
];
}

/**
* @param string $payload
* @return string
*/
private function truncatePayload(string $payload): string
{
if (strlen($payload) <= 500) {
return $payload;
}

return substr($payload, 0, 500) . '...';
}
}
4 changes: 2 additions & 2 deletions src/Logging/RpcLogEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ class RpcLogEvent
/**
* An array representation of JSON for the response or request
*
* @var null|string|array<string, mixed>
* @var null|string
*/
public null|string|array $payload = null;
public null|string $payload = null;

/**
* Status code for REST or gRPC methods
Expand Down
2 changes: 1 addition & 1 deletion tests/Logging/LoggingTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ private function getNewLogEvent(): RpcLogEvent
'header1' => 'test',
'Authorization' => 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.cThIIoDvwdueQB468K5xDc5633seEFoqwxjF_xSJyQQ'
];
$event->payload = ['param' => 'test'];
$event->payload = json_encode(['param' => 'test']);
$event->status = 200;
$event->retryAttempt = 0;
$event->rpcName = 'Rpc NameTest';
Expand Down

0 comments on commit ab90bf7

Please sign in to comment.