Skip to content

Commit 3dfc3d1

Browse files
authored
feat: Implement onEnding interface of Span Processor in Trace SDK (#1785)
* Updated * Fixed * nits * Backport-ed the 2.x Span::end() implementation #1483
1 parent 36df650 commit 3dfc3d1

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace OpenTelemetry\SDK\Trace;
6+
7+
/**
8+
* @experimental
9+
*/
10+
interface ExtendedSpanProcessorInterface extends SpanProcessorInterface
11+
{
12+
/**
13+
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/v1.51.0/specification/trace/sdk.md#onending
14+
*/
15+
public function onEnding(ReadWriteSpanInterface $span): void;
16+
}

Trace/Span.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class Span extends API\Span implements ReadWriteSpanInterface
2626
private array $events = [];
2727
private int $totalRecordedEvents = 0;
2828
private StatusDataInterface $status;
29-
private int $endEpochNanos = 0;
29+
private ?int $endEpochNanos = null;
3030
private bool $hasEnded = false;
3131

3232
/**
@@ -268,16 +268,20 @@ public function setStatus(string $code, ?string $description = null): self
268268
#[\Override]
269269
public function end(?int $endEpochNanos = null): void
270270
{
271-
if ($this->hasEnded) {
271+
if ($this->endEpochNanos !== null) {
272272
return;
273273
}
274274

275275
$this->endEpochNanos = $endEpochNanos ?? Clock::getDefault()->now();
276-
$this->hasEnded = true;
277-
278-
$this->checkForDroppedElements();
276+
$span = clone $this;
277+
$this->hasEnded = true; // prevent further modifications to the span by async code
278+
if ($this->spanProcessor instanceof ExtendedSpanProcessorInterface) {
279+
$this->spanProcessor->onEnding($span);
280+
}
281+
$span->hasEnded = true;
279282

280-
$this->spanProcessor->onEnd($this);
283+
$this->spanProcessor->onEnd($span);
284+
$span->checkForDroppedElements();
281285
}
282286

283287
/** @inheritDoc */
@@ -317,7 +321,7 @@ public function toSpanData(): SpanDataInterface
317321
$this->totalRecordedLinks,
318322
$this->totalRecordedEvents,
319323
$this->status,
320-
$this->endEpochNanos,
324+
$this->endEpochNanos ?? 0,
321325
$this->hasEnded
322326
);
323327
}

Trace/SpanProcessor/MultiSpanProcessor.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use OpenTelemetry\Context\ContextInterface;
88
use OpenTelemetry\SDK\Common\Future\CancellationInterface;
9+
use OpenTelemetry\SDK\Trace\ExtendedSpanProcessorInterface;
910
use OpenTelemetry\SDK\Trace\ReadableSpanInterface;
1011
use OpenTelemetry\SDK\Trace\ReadWriteSpanInterface;
1112
use OpenTelemetry\SDK\Trace\SpanProcessorInterface;
@@ -14,7 +15,7 @@
1415
* Class SpanMultiProcessor is a SpanProcessor that forwards all events to an
1516
* array of SpanProcessors.
1617
*/
17-
final class MultiSpanProcessor implements SpanProcessorInterface
18+
final class MultiSpanProcessor implements ExtendedSpanProcessorInterface
1819
{
1920
/** @var list<SpanProcessorInterface> */
2021
private array $processors = [];
@@ -46,6 +47,17 @@ public function onStart(ReadWriteSpanInterface $span, ContextInterface $parentCo
4647
}
4748
}
4849

50+
/** @inheritDoc */
51+
#[\Override]
52+
public function onEnding(ReadWriteSpanInterface $span): void
53+
{
54+
foreach ($this->processors as $processor) {
55+
if ($processor instanceof ExtendedSpanProcessorInterface) {
56+
$processor->onEnding($span);
57+
}
58+
}
59+
}
60+
4961
/** @inheritDoc */
5062
#[\Override]
5163
public function onEnd(ReadableSpanInterface $span): void

0 commit comments

Comments
 (0)