Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"require": {
"php": "^8.2",
"ext-json": "*",
"ext-mbstring": "*",
"monolog/monolog": "^3.0",
"pccomponentes/ddd": "^2.1|^3.0|^4.0",
"symfony/http-kernel": "^4.4|^5.0|^6.0|^7.0",
Expand Down
7 changes: 5 additions & 2 deletions src/Context/NormalizeMessageProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ public function __invoke(LogRecord $record): LogRecord
'message_id' => $message->messageId()->value(),
'name' => $message::messageName(),
'type' => $message::messageType(),
'payload' => \json_encode($this->sanitizePayloadForLogging($message->messagePayload())),
'payload' => \json_encode(
$this->sanitizePayloadForLogging($message->messagePayload()),
\JSON_INVALID_UTF8_SUBSTITUTE | \JSON_PARTIAL_OUTPUT_ON_ERROR,
),
];

if ($message instanceof AggregateMessage) {
Expand Down Expand Up @@ -65,7 +68,7 @@ private function sanitizeValueForLogging(mixed $value): mixed

private function formatTruncatedString(string $value): string
{
$preview = \substr($value, 0, self::STRING_PREVIEW_LENGTH);
$preview = \mb_strcut($value, 0, self::STRING_PREVIEW_LENGTH, 'UTF-8');

return \sprintf(
'%s...[string truncated; original_length=%d]',
Expand Down
2 changes: 1 addition & 1 deletion src/DomainTrace/MessageTraceMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
$message = $this->messageFromEnvelope($envelope);

$this->tracker->assignReplyTo(
$message->messageId()->value()
$message->messageId()->value(),
);

return $stack->next()->handle($envelope, $stack);
Expand Down
5 changes: 4 additions & 1 deletion src/DomainTrace/RequestTraceMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@ public function handle(Envelope $envelope, StackInterface $stack): Envelope
$this->assertCorrelationId();

$request = $this->requestStack->getCurrentRequest();

if (null === $request) {
return $stack->next()->handle($envelope, $stack);
}

$message = $this->messageFromEnvelope($envelope);

$correlationId = $this->correlationIdFromRequest($request);

if (null !== $correlationId) {
$this->tracker->assignCorrelationId($correlationId, $message->messageId());
}

$replyTo = $this->replyToFromRequest($request);

if (null !== $replyTo) {
$this->tracker->assignReplyTo($replyTo, $message->messageId());
}
Expand All @@ -58,7 +61,7 @@ private function assertCorrelationId(): void
}

$this->tracker->assignCorrelationId(
Uuid::v4()->value()
Uuid::v4()->value(),
);
}

Expand Down
1 change: 1 addition & 0 deletions src/DomainTrace/Tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public function assignCorrelationId(string $correlationId, ?Uuid $messageId = nu
public function assignReplyTo(string $replyTo, ?Uuid $messageId = null): void
{
$this->replyTo = $replyTo;

if (null === $messageId) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/MessageLogger/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
interface Action
{
public function success(): string;

public function error(): string;
}
32 changes: 16 additions & 16 deletions src/Util/AssocSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,30 @@ final class AssocSerializer
{
public static function from($anything): array
{
switch (true) {
case $anything instanceof \Throwable:
return self::throwable($anything);
case \is_array($anything):
return \array_map(
static function ($item) {
return self::from($item);
},
$anything
);
case $anything instanceof \JsonSerializable:
case \is_object($anything):
return self::basic($anything);
default:
return ['value' => $anything];
if ($anything instanceof \Throwable) {
return self::throwable($anything);
}

if (\is_array($anything)) {
return \array_map(
static fn ($item) => self::from($item),
$anything,
);
}

if ($anything instanceof \JsonSerializable || \is_object($anything)) {
return self::basic($anything);
}

return ['value' => $anything];
}

private static function throwable(\Throwable $throwable): array
{
return [
'class' => \get_class($throwable),
'message' => $throwable->getMessage(),
'code' => $throwable->getCode(),
'code' => (string)$throwable->getCode(),
'file' => $throwable->getFile(),
'line' => $throwable->getLine(),
'trace' => self::trace($throwable),
Expand Down
2 changes: 1 addition & 1 deletion src/Util/Filebeat/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
}
},
"code": {
"type": "integer"
"type": "keyword"
},
"file": {
"type": "text",
Expand Down
69 changes: 69 additions & 0 deletions tests/Context/NormalizeContextProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,75 @@ public function testShouldReturnedRecordWithLargeNonBase64PayloadTruncated()
$this->assertStringNotContainsString($largeString, $payload['attachment']);
}

/**
* @dataProvider utf8TruncationProvider
*/
public function testShouldTruncateLargeUtf8StringWithoutSplittingCharacters(
string $prefix,
string $character,
string $expectedPreview
) {
$largeString = $prefix . $character . \str_repeat('b', 18000);

$result = (new NormalizeMessageProcessor())($this->createRecordWithPayload([
'address' => $largeString,
]));

$this->assertIsString($result['context']['message']['payload']);
$payload = $this->decodePayload($result);

$this->assertSame(
$expectedPreview . '...[string truncated; original_length=' . \strlen($largeString) . ']',
$payload['address'],
);
}

public function utf8TruncationProvider(): array
{
return [
'two-byte character crossing boundary' => [\str_repeat('a', 127), 'é', \str_repeat('a', 127)],
'three-byte character crossing boundary' => [\str_repeat('a', 127), '€', \str_repeat('a', 127)],
'four-byte character crossing boundary' => [\str_repeat('a', 127), '😀', \str_repeat('a', 127)],
'two-byte character fitting boundary' => [\str_repeat('a', 126), 'é', \str_repeat('a', 126) . 'é'],
'three-byte character fitting boundary' => [\str_repeat('a', 125), '€', \str_repeat('a', 125) . '€'],
'four-byte character fitting boundary' => [\str_repeat('a', 124), '😀', \str_repeat('a', 124) . '😀'],
];
}

public function testShouldReplaceInvalidUtf8WithoutLosingOtherPayloadFields()
{
$result = (new NormalizeMessageProcessor())($this->createRecordWithPayload([
'address' => "street \xB1 name",
'city' => 'Madrid',
]));

$this->assertIsString($result['context']['message']['payload']);
$this->assertSame([
'address' => "street \u{FFFD} name",
'city' => 'Madrid',
], $this->decodePayload($result));
}

public function testShouldKeepSerializablePayloadFieldsWhenJsonEncodingFails()
{
$resource = \fopen('php://memory', 'r');

try {
$result = (new NormalizeMessageProcessor())($this->createRecordWithPayload([
'attachment' => $resource,
'address' => 'street name',
]));
} finally {
\fclose($resource);
}

$this->assertIsString($result['context']['message']['payload']);
$this->assertSame([
'attachment' => null,
'address' => 'street name',
], $this->decodePayload($result));
}

public function testShouldReturnedRecordWithTruncatedNestedLargeStringPayload()
{
$largeString = \str_repeat('b', 18000);
Expand Down
38 changes: 38 additions & 0 deletions tests/Util/AssocSerializerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);

namespace PcComponentes\DddLogging\Tests\Util;

use PcComponentes\DddLogging\Util\AssocSerializer;
use PHPUnit\Framework\TestCase;

final class AssocSerializerTest extends TestCase
{
public function testShouldSerializeDefaultExceptionCodeAsString()
{
$result = AssocSerializer::from(new \Exception());

$this->assertSame('0', $result['code']);
}

public function testShouldSerializeIntegerExceptionCodeAsString()
{
$result = AssocSerializer::from(new \Exception('message', 123));

$this->assertSame('123', $result['code']);
}

public function testShouldPreserveAlphanumericExceptionCode()
{
$exception = new class extends \Exception {
public function __construct()
{
$this->code = 'HY000';
}
};

$result = AssocSerializer::from($exception);

$this->assertSame('HY000', $result['code']);
}
}
Loading