Skip to content

Commit

Permalink
implement mutable ReadWriteLogRecord (#1482)
Browse files Browse the repository at this point in the history
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
brettmc authored Jan 23, 2025
1 parent c568df5 commit 0b255ca
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 12 deletions.
18 changes: 17 additions & 1 deletion Logs/Exporter/InMemoryExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use OpenTelemetry\SDK\Common\Future\CompletedFuture;
use OpenTelemetry\SDK\Common\Future\FutureInterface;
use OpenTelemetry\SDK\Logs\LogRecordExporterInterface;
use OpenTelemetry\SDK\Logs\ReadableLogRecord;

class InMemoryExporter implements LogRecordExporterInterface
{
Expand All @@ -22,7 +23,7 @@ public function __construct(private readonly ArrayObject $storage = new ArrayObj
public function export(iterable $batch, ?CancellationInterface $cancellation = null): FutureInterface
{
foreach ($batch as $record) {
$this->storage->append($record);
$this->storage->append($this->convert($record));
}

return new CompletedFuture(true);
Expand All @@ -42,4 +43,19 @@ public function getStorage(): ArrayObject
{
return $this->storage;
}

private function convert(ReadableLogRecord $record): array
{
return [
'timestamp' => $record->getTimestamp(),
'observed_timestamp' => $record->getObservedTimestamp(),
'severity_number' => $record->getSeverityNumber(),
'severity_text' => $record->getSeverityText(),
'body' => $record->getBody(),
'attributes' => $record->getAttributes()->toArray(),
'trace_id' => $record->getSpanContext()?->getTraceId(),
'span_id' => $record->getSpanContext()?->getSpanId(),
'trace_flags' => $record->getSpanContext()?->getTraceFlags(),
];
}
}
2 changes: 1 addition & 1 deletion Logs/LogRecordProcessorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

interface LogRecordProcessorInterface
{
public function onEmit(ReadWriteLogRecord $record, ?ContextInterface $context = null): void;
public function onEmit(ReadWriteLogRecord &$record, ?ContextInterface $context = null): void;
public function shutdown(?CancellationInterface $cancellation = null): bool;
public function forceFlush(?CancellationInterface $cancellation = null): bool;
}
2 changes: 1 addition & 1 deletion Logs/Processor/BatchLogRecordProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function __construct(
});
}

public function onEmit(ReadWriteLogRecord $record, ?ContextInterface $context = null): void
public function onEmit(ReadWriteLogRecord &$record, ?ContextInterface $context = null): void
{
if ($this->closed) {
return;
Expand Down
2 changes: 1 addition & 1 deletion Logs/Processor/MultiLogRecordProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __construct(array $processors)
}
}

public function onEmit(ReadWriteLogRecord $record, ?ContextInterface $context = null): void
public function onEmit(ReadWriteLogRecord &$record, ?ContextInterface $context = null): void
{
foreach ($this->processors as $processor) {
$processor->onEmit($record, $context);
Expand Down
2 changes: 1 addition & 1 deletion Logs/Processor/NoopLogRecordProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static function getInstance(): self
/**
* @codeCoverageIgnore
*/
public function onEmit(ReadWriteLogRecord $record, ?ContextInterface $context = null): void
public function onEmit(ReadWriteLogRecord &$record, ?ContextInterface $context = null): void
{
}

Expand Down
2 changes: 1 addition & 1 deletion Logs/Processor/SimpleLogRecordProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function __construct(private readonly LogRecordExporterInterface $exporte
/**
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/sdk.md#onemit
*/
public function onEmit(ReadWriteLogRecord $record, ?ContextInterface $context = null): void
public function onEmit(ReadWriteLogRecord &$record, ?ContextInterface $context = null): void
{
$this->exporter->export([$record]);
}
Expand Down
13 changes: 13 additions & 0 deletions Logs/ReadWriteLogRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,17 @@

class ReadWriteLogRecord extends ReadableLogRecord
{
public function setAttribute(string $name, mixed $value): self
{
$this->attributesBuilder->offsetSet($name, $value);

return $this;
}

public function removeAttribute(string $key): self
{
$this->attributesBuilder->offsetUnset($key);

return $this;
}
}
11 changes: 5 additions & 6 deletions Logs/ReadableLogRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use OpenTelemetry\API\Trace\SpanContextInterface;
use OpenTelemetry\Context\Context;
use OpenTelemetry\Context\ContextInterface;
use OpenTelemetry\SDK\Common\Attribute\AttributesBuilderInterface;
use OpenTelemetry\SDK\Common\Attribute\AttributesInterface;
use OpenTelemetry\SDK\Common\Attribute\LogRecordAttributeValidator;
use OpenTelemetry\SDK\Common\Instrumentation\InstrumentationScopeInterface;
Expand All @@ -20,7 +21,7 @@
*/
class ReadableLogRecord extends LogRecord
{
protected AttributesInterface $convertedAttributes;
protected AttributesBuilderInterface $attributesBuilder;
protected SpanContextInterface $spanContext;

public function __construct(
Expand All @@ -38,12 +39,10 @@ public function __construct(
$this->severityNumber = $logRecord->severityNumber;
$this->severityText = $logRecord->severityText;

//convert attributes now so that excess data is not sent to processors
$this->convertedAttributes = $this->loggerSharedState
$this->attributesBuilder = $this->loggerSharedState
->getLogRecordLimits()
->getAttributeFactory()
->builder($logRecord->attributes, new LogRecordAttributeValidator())
->build();
->builder($logRecord->attributes, new LogRecordAttributeValidator());
}

public function getInstrumentationScope(): InstrumentationScopeInterface
Expand Down Expand Up @@ -96,6 +95,6 @@ public function getBody()

public function getAttributes(): AttributesInterface
{
return $this->convertedAttributes;
return $this->attributesBuilder->build();
}
}

0 comments on commit 0b255ca

Please sign in to comment.