Skip to content

Commit 3fa6101

Browse files
authored
revert pass-by-reference to LogRecordProcessor::onEmit (open-telemetry#1492)
add a test to demonstrate that mutations are still seen by later processors without by-reference
1 parent a537dbf commit 3fa6101

File tree

7 files changed

+86
-6
lines changed

7 files changed

+86
-6
lines changed

src/SDK/Logs/LogRecordProcessorInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
interface LogRecordProcessorInterface
1111
{
12-
public function onEmit(ReadWriteLogRecord &$record, ?ContextInterface $context = null): void;
12+
public function onEmit(ReadWriteLogRecord $record, ?ContextInterface $context = null): void;
1313
public function shutdown(?CancellationInterface $cancellation = null): bool;
1414
public function forceFlush(?CancellationInterface $cancellation = null): bool;
1515
}

src/SDK/Logs/Processor/BatchLogRecordProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public function __construct(
134134
});
135135
}
136136

137-
public function onEmit(ReadWriteLogRecord &$record, ?ContextInterface $context = null): void
137+
public function onEmit(ReadWriteLogRecord $record, ?ContextInterface $context = null): void
138138
{
139139
if ($this->closed) {
140140
return;

src/SDK/Logs/Processor/MultiLogRecordProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct(array $processors)
2222
}
2323
}
2424

25-
public function onEmit(ReadWriteLogRecord &$record, ?ContextInterface $context = null): void
25+
public function onEmit(ReadWriteLogRecord $record, ?ContextInterface $context = null): void
2626
{
2727
foreach ($this->processors as $processor) {
2828
$processor->onEmit($record, $context);

src/SDK/Logs/Processor/NoopLogRecordProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static function getInstance(): self
2121
/**
2222
* @codeCoverageIgnore
2323
*/
24-
public function onEmit(ReadWriteLogRecord &$record, ?ContextInterface $context = null): void
24+
public function onEmit(ReadWriteLogRecord $record, ?ContextInterface $context = null): void
2525
{
2626
}
2727

src/SDK/Logs/Processor/SimpleLogRecordProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __construct(private readonly LogRecordExporterInterface $exporte
1919
/**
2020
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/sdk.md#onemit
2121
*/
22-
public function onEmit(ReadWriteLogRecord &$record, ?ContextInterface $context = null): void
22+
public function onEmit(ReadWriteLogRecord $record, ?ContextInterface $context = null): void
2323
{
2424
$this->exporter->export([$record]);
2525
}

tests/Integration/SDK/Logs/LoggerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(private readonly InMemoryExporter $exporter)
3535
{
3636
}
3737

38-
public function onEmit(ReadWriteLogRecord &$record, ?ContextInterface $context = null): void
38+
public function onEmit(ReadWriteLogRecord $record, ?ContextInterface $context = null): void
3939
{
4040
$record->setAttributes(['baz' => 'bat']);
4141
$this->exporter->export([$record]);
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
--TEST--
2+
Test mutating a LogRecord during SpanProcessor::onEmit
3+
--FILE--
4+
<?php
5+
require_once 'vendor/autoload.php';
6+
7+
use OpenTelemetry\SDK\Trace\TracerProviderBuilder;
8+
use OpenTelemetry\SDK\Trace\ReadWriteSpanInterface;
9+
use OpenTelemetry\SDK\Trace\ReadableSpanInterface;
10+
use OpenTelemetry\SDK\Common\Future\CancellationInterface;
11+
use OpenTelemetry\SDK\Trace\SpanProcessorInterface;
12+
use OpenTelemetry\Context\ContextInterface;
13+
14+
$exporter = (new \OpenTelemetry\SDK\Logs\Exporter\ConsoleExporterFactory())->create();
15+
16+
$one = new class implements \OpenTelemetry\SDK\Logs\LogRecordProcessorInterface {
17+
public function onEmit(\OpenTelemetry\SDK\Logs\ReadWriteLogRecord $record, ?ContextInterface $context = null): void
18+
{
19+
$record->setBody('updated');
20+
$record->setAttribute('new-key', 'new-value');
21+
}
22+
23+
public function shutdown(?CancellationInterface $cancellation = null): bool
24+
{
25+
return true;
26+
}
27+
28+
public function forceFlush(?CancellationInterface $cancellation = null): bool
29+
{
30+
return true;
31+
}
32+
};
33+
$two = new \OpenTelemetry\SDK\Logs\Processor\SimpleLogRecordProcessor($exporter);
34+
35+
$loggerProvider = (new \OpenTelemetry\SDK\Logs\LoggerProviderBuilder())
36+
->addLogRecordProcessor(new \OpenTelemetry\SDK\Logs\Processor\MultiLogRecordProcessor([$one, $two]))
37+
->build();
38+
39+
$logger = $loggerProvider->getLogger('test');
40+
$logger->emit(new \OpenTelemetry\API\Logs\LogRecord('body'));
41+
?>
42+
--EXPECTF--
43+
{
44+
"resource": {
45+
"attributes": {
46+
"telemetry.sdk.name": "opentelemetry",
47+
"telemetry.sdk.language": "php",
48+
"telemetry.sdk.version": "%s",
49+
"telemetry.distro.name": "%s",
50+
"telemetry.distro.version": "%s",
51+
"service.name": "%s"
52+
},
53+
"dropped_attributes_count": 0
54+
},
55+
"scopes": [
56+
{
57+
"name": "test",
58+
"version": null,
59+
"attributes": [],
60+
"dropped_attributes_count": 0,
61+
"schema_url": null,
62+
"logs": [
63+
{
64+
"timestamp": null,
65+
"observed_timestamp": %d,
66+
"severity_number": 0,
67+
"severity_text": null,
68+
"body": "updated",
69+
"trace_id": "%s",
70+
"span_id": "%s",
71+
"trace_flags": 0,
72+
"attributes": {
73+
"new-key": "new-value"
74+
},
75+
"dropped_attributes_count": 0
76+
}
77+
]
78+
}
79+
]
80+
}

0 commit comments

Comments
 (0)