-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement mutable ReadWriteLogRecord (#1482)
open-telemetry/opentelemetry-specification#3907 implements some new requirements for logging: - ReadWriteLogRecord can mutate (eg by processors) - mutated ReadWriteLogRecord can be seen by later processors This is a breaking change because LogRecordProcessorInterface onEmit param changes to by-reference
- Loading branch information
Showing
10 changed files
with
206 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Tests\Integration\SDK\Logs; | ||
|
||
use OpenTelemetry\API\Logs\LogRecord; | ||
use OpenTelemetry\Context\ContextInterface; | ||
use OpenTelemetry\SDK\Common\Future\CancellationInterface; | ||
use OpenTelemetry\SDK\Logs\Exporter\InMemoryExporter; | ||
use OpenTelemetry\SDK\Logs\LoggerProvider; | ||
use OpenTelemetry\SDK\Logs\LogRecordProcessorInterface; | ||
use OpenTelemetry\SDK\Logs\Processor\MultiLogRecordProcessor; | ||
use OpenTelemetry\SDK\Logs\Processor\SimpleLogRecordProcessor; | ||
use OpenTelemetry\SDK\Logs\ReadWriteLogRecord; | ||
use PHPUnit\Framework\Attributes\CoversNothing; | ||
use PHPUnit\Framework\Attributes\Group; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversNothing] | ||
class LoggerTest extends TestCase | ||
{ | ||
/** | ||
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.40.0/specification/logs/sdk.md#onemit | ||
*/ | ||
#[Group('logs-compliance')] | ||
public function test_log_record_mutations_visible_to_later_processors(): void | ||
{ | ||
$logRecord = (new LogRecord()) | ||
->setAttributes(['foo' => 'bar']); | ||
$storage = new \ArrayObject(); | ||
$exporter = new InMemoryExporter($storage); | ||
$mutator = new class($exporter) implements LogRecordProcessorInterface { | ||
public function __construct(private readonly InMemoryExporter $exporter) | ||
{ | ||
} | ||
|
||
public function onEmit(ReadWriteLogRecord &$record, ?ContextInterface $context = null): void | ||
{ | ||
$record->setAttributes(['baz' => 'bat']); | ||
$this->exporter->export([$record]); | ||
} | ||
|
||
public function shutdown(?CancellationInterface $cancellation = null): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function forceFlush(?CancellationInterface $cancellation = null): bool | ||
{ | ||
return true; | ||
} | ||
}; | ||
$multi = new MultiLogRecordProcessor([ | ||
new SimpleLogRecordProcessor($exporter), | ||
$mutator, | ||
new SimpleLogRecordProcessor($exporter), | ||
]); | ||
$logger = LoggerProvider::builder()->addLogRecordProcessor($multi)->build()->getLogger('test'); | ||
|
||
$this->assertCount(0, $storage); | ||
$logger->emit($logRecord); | ||
$this->assertCount(3, $storage); | ||
|
||
$first = $storage[0]; //@var array $first | ||
$this->assertSame(['foo' => 'bar'], $first['attributes'], 'original attributes'); //@phpstan-ignore-line | ||
|
||
$second = $storage[1]; //@var array $second | ||
$this->assertSame(['foo' => 'bar', 'baz' => 'bat'], $second['attributes'], 'mutated attributes'); //@phpstan-ignore-line | ||
|
||
$third = $storage[2]; //@var array $third | ||
$this->assertSame(['foo' => 'bar', 'baz' => 'bat'], $third['attributes'], 'attributes after mutation by second processor'); //@phpstan-ignore-line | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Unit\SDK\Logs; | ||
|
||
use OpenTelemetry\API\Logs\LogRecord; | ||
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeInterface; | ||
use OpenTelemetry\SDK\Logs\LoggerSharedState; | ||
use OpenTelemetry\SDK\Logs\LogRecordLimitsBuilder; | ||
use OpenTelemetry\SDK\Logs\LogRecordProcessorInterface; | ||
use OpenTelemetry\SDK\Logs\ReadWriteLogRecord; | ||
use OpenTelemetry\SDK\Resource\ResourceInfoFactory; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[CoversClass(ReadWriteLogRecord::class)] | ||
class ReadWriteLogRecordTest extends TestCase | ||
{ | ||
private ReadWriteLogRecord $record; | ||
|
||
public function setUp(): void | ||
{ | ||
$limits = (new LogRecordLimitsBuilder())->setAttributeCountLimit(10)->build(); | ||
$loggerSharedState = new LoggerSharedState( | ||
ResourceInfoFactory::emptyResource(), | ||
$limits, | ||
$this->createMock(LogRecordProcessorInterface::class) | ||
); | ||
$record = (new LogRecord()) | ||
->setTimestamp(1) | ||
->setObservedTimestamp(2) | ||
->setSeverityText('severity') | ||
->setSeverityNumber(3) | ||
->setBody('body') | ||
->setAttributes(['key' => 'value']); | ||
|
||
$this->record = new ReadWriteLogRecord( | ||
$this->createMock(InstrumentationScopeInterface::class), | ||
$loggerSharedState, | ||
$record | ||
); | ||
} | ||
|
||
public function test_modify_timestamp(): void | ||
{ | ||
$this->record->setTimestamp(4); | ||
$this->assertEquals(4, $this->record->getTimestamp()); | ||
} | ||
|
||
public function test_set_observed_timestamp(): void | ||
{ | ||
$this->record->setObservedTimestamp(5); | ||
$this->assertEquals(5, $this->record->getObservedTimestamp()); | ||
} | ||
|
||
public function test_set_severity_text(): void | ||
{ | ||
$this->record->setSeverityText('severity2'); | ||
$this->assertEquals('severity2', $this->record->getSeverityText()); | ||
} | ||
|
||
public function test_set_severity_number(): void | ||
{ | ||
$this->record->setSeverityNumber(6); | ||
$this->assertEquals(6, $this->record->getSeverityNumber()); | ||
} | ||
|
||
public function test_set_body(): void | ||
{ | ||
$this->record->setBody('body2'); | ||
$this->assertEquals('body2', $this->record->getBody()); | ||
} | ||
|
||
public function test_add_attribute(): void | ||
{ | ||
$this->record->setAttribute('key2', 'value2'); | ||
$this->assertEquals(['key' => 'value', 'key2' => 'value2'], $this->record->getAttributes()->toArray()); | ||
} | ||
|
||
public function test_remove_attribute(): void | ||
{ | ||
$this->record->removeAttribute('key'); | ||
$this->assertEquals([], $this->record->getAttributes()->toArray()); | ||
} | ||
|
||
public function test_modify_attribute(): void | ||
{ | ||
$this->record->setAttribute('key', 'updated'); | ||
$this->assertEquals(['key' => 'updated'], $this->record->getAttributes()->toArray()); | ||
} | ||
} |