Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Naoray committed Jan 12, 2025
1 parent 1d5a89d commit 063cde5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 22 deletions.
4 changes: 2 additions & 2 deletions src/Handlers/SignatureDeduplicationHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(
?SignatureGenerator $signatureGenerator = null,
) {
parent::__construct($handler, $deduplicationStore, $deduplicationLevel, $time, $bubble);
$this->signatureGenerator = $signatureGenerator ?? new DefaultSignatureGenerator;
$this->signatureGenerator = $signatureGenerator ?? new DefaultSignatureGenerator();
}

/**
Expand All @@ -49,6 +49,6 @@ protected function isDuplicate(array $store, LogRecord $record): bool
*/
protected function buildDeduplicationStoreEntry(LogRecord $record): string
{
return $record->datetime->getTimestamp().':'.$this->signatureGenerator->generate($record);
return $record->datetime->getTimestamp() . ':' . $this->signatureGenerator->generate($record);
}
}
33 changes: 13 additions & 20 deletions tests/Handlers/SignatureDeduplicationHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
use Naoray\LaravelGithubMonolog\Handlers\SignatureDeduplicationHandler;

beforeEach(function () {
$this->deduplicationStore = sys_get_temp_dir().'/test-dedup-'.uniqid().'.log';
$this->signatureGenerator = new DefaultSignatureGenerator;
$this->deduplicationStore = sys_get_temp_dir() . '/test-dedup-' . uniqid() . '.log';
$this->signatureGenerator = new DefaultSignatureGenerator();
$this->handler = new SignatureDeduplicationHandler(
new NullHandler,
new NullHandler(),
$this->deduplicationStore,
Level::Debug,
time: 60,
Expand All @@ -26,7 +26,7 @@

test('deduplicates records with same signature', function () {
$record1 = new LogRecord(
datetime: new \DateTimeImmutable,
datetime: new \DateTimeImmutable(),
channel: 'test',
level: Level::Error,
message: 'Test message',
Expand All @@ -41,7 +41,7 @@

// Same signature should be deduplicated
$record2 = new LogRecord(
datetime: new \DateTimeImmutable,
datetime: new \DateTimeImmutable(),
channel: 'different-channel',
level: Level::Error,
message: 'Test message',
Expand All @@ -53,7 +53,7 @@

// Different signature should not be deduplicated
$record3 = new LogRecord(
datetime: new \DateTimeImmutable,
datetime: new \DateTimeImmutable(),
channel: 'test',
level: Level::Error,
message: 'Different message',
Expand All @@ -64,19 +64,16 @@

// Verify deduplication store contains both unique signatures
$this->handler->close();
$lines = file($this->deduplicationStore, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($lines === false) {
throw new \RuntimeException('Failed to read deduplication store file');
}
$signatures = array_map(fn ($line) => explode(':', $line, 2)[1], $lines);
expect($signatures)
$contents = file_get_contents($this->deduplicationStore);
dd($contents);
expect($contents)
->toContain($this->signatureGenerator->generate($record1))
->toContain($this->signatureGenerator->generate($record3));
});

test('deduplication respects time window', function () {
$record = new LogRecord(
datetime: new \DateTimeImmutable,
datetime: new \DateTimeImmutable(),
channel: 'test',
level: Level::Error,
message: 'Test message',
Expand All @@ -86,7 +83,7 @@

// Create handler with 1 second time window
$handler = new SignatureDeduplicationHandler(
new NullHandler,
new NullHandler(),
$this->deduplicationStore,
Level::Debug,
time: 1,
Expand All @@ -107,11 +104,7 @@
$handler->close();

// Verify deduplication store contains only the most recent entry
$lines = file($this->deduplicationStore, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($lines === false) {
throw new \RuntimeException('Failed to read deduplication store file');
}
$signatures = array_map(fn ($line) => explode(':', $line, 2)[1], $lines);
$contents = file_get_contents($this->deduplicationStore);
$signature = $this->signatureGenerator->generate($record);
expect(array_count_values($signatures)[$signature])->toBe(1);
expect(substr_count($contents, $signature))->toBe(1);
});

0 comments on commit 063cde5

Please sign in to comment.