Skip to content

Commit fd8ac30

Browse files
authored
Run exports with the Context the SpanProcessor was created in (#880)
* Run exports in spanprocessor construction context * Resolve psalm issues
1 parent 0ce2fda commit fd8ac30

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

src/SDK/Trace/SpanProcessor/BatchSpanProcessor.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use InvalidArgumentException;
1010
use OpenTelemetry\API\Metrics\MeterProviderInterface;
1111
use OpenTelemetry\API\Metrics\ObserverInterface;
12+
use OpenTelemetry\Context\Context;
1213
use OpenTelemetry\Context\ContextInterface;
1314
use OpenTelemetry\SDK\Behavior\LogsMessagesTrait;
1415
use OpenTelemetry\SDK\Common\Future\CancellationInterface;
@@ -44,6 +45,7 @@ class BatchSpanProcessor implements SpanProcessorInterface
4445
private int $scheduledDelayNanos;
4546
private int $maxExportBatchSize;
4647
private bool $autoFlush;
48+
private ContextInterface $exportContext;
4749

4850
private ?int $nextScheduledRun = null;
4951
private bool $running = false;
@@ -55,7 +57,7 @@ class BatchSpanProcessor implements SpanProcessorInterface
5557
private array $batch = [];
5658
/** @var SplQueue<list<SpanDataInterface>> */
5759
private SplQueue $queue;
58-
/** @var SplQueue<array{int, string, ?CancellationInterface, bool}> */
60+
/** @var SplQueue<array{int, string, ?CancellationInterface, bool, ContextInterface}> */
5961
private SplQueue $flush;
6062

6163
private bool $closed = false;
@@ -93,6 +95,7 @@ public function __construct(
9395
$this->maxExportBatchSize = $maxExportBatchSize;
9496
$this->autoFlush = $autoFlush;
9597

98+
$this->exportContext = Context::getCurrent();
9699
$this->queue = new SplQueue();
97100
$this->flush = new SplQueue();
98101

@@ -199,7 +202,7 @@ private function flush(?string $flushMethod = null, ?CancellationInterface $canc
199202
{
200203
if ($flushMethod !== null) {
201204
$flushId = $this->batchId + $this->queue->count() + (int) (bool) $this->batch;
202-
$this->flush->enqueue([$flushId, $flushMethod, $cancellation, !$this->running]);
205+
$this->flush->enqueue([$flushId, $flushMethod, $cancellation, !$this->running, Context::getCurrent()]);
203206
}
204207

205208
if ($this->running) {
@@ -213,7 +216,8 @@ private function flush(?string $flushMethod = null, ?CancellationInterface $canc
213216
try {
214217
for (;;) {
215218
while (!$this->flush->isEmpty() && $this->flush->bottom()[0] <= $this->batchId) {
216-
[, $flushMethod, $cancellation, $propagateResult] = $this->flush->dequeue();
219+
[, $flushMethod, $cancellation, $propagateResult, $context] = $this->flush->dequeue();
220+
$scope = $context->activate();
217221

218222
try {
219223
$result = $this->exporter->$flushMethod($cancellation);
@@ -223,10 +227,11 @@ private function flush(?string $flushMethod = null, ?CancellationInterface $canc
223227
} catch (Throwable $e) {
224228
if ($propagateResult) {
225229
$exception = $e;
226-
227-
continue;
230+
} else {
231+
self::logError(sprintf('Unhandled %s error', $flushMethod), ['exception' => $e]);
228232
}
229-
self::logError(sprintf('Unhandled %s error', $flushMethod), ['exception' => $e]);
233+
} finally {
234+
$scope->detach();
230235
}
231236
}
232237

@@ -239,6 +244,7 @@ private function flush(?string $flushMethod = null, ?CancellationInterface $canc
239244
}
240245
$batchSize = count($this->queue->bottom());
241246
$this->batchId++;
247+
$scope = $this->exportContext->activate();
242248

243249
try {
244250
$this->exporter->export($this->queue->dequeue())->await();
@@ -247,6 +253,7 @@ private function flush(?string $flushMethod = null, ?CancellationInterface $canc
247253
} finally {
248254
$this->processed += $batchSize;
249255
$this->queueSize -= $batchSize;
256+
$scope->detach();
250257
}
251258
}
252259
} finally {

src/SDK/Trace/SpanProcessor/SimpleSpanProcessor.php

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace OpenTelemetry\SDK\Trace\SpanProcessor;
66

77
use Closure;
8+
use OpenTelemetry\Context\Context;
89
use OpenTelemetry\Context\ContextInterface;
910
use OpenTelemetry\SDK\Behavior\LogsMessagesTrait;
1011
use OpenTelemetry\SDK\Common\Future\CancellationInterface;
@@ -21,9 +22,10 @@ class SimpleSpanProcessor implements SpanProcessorInterface
2122
use LogsMessagesTrait;
2223

2324
private SpanExporterInterface $exporter;
25+
private ContextInterface $exportContext;
2426

2527
private bool $running = false;
26-
/** @var SplQueue<array{Closure, string, bool}> */
28+
/** @var SplQueue<array{Closure, string, bool, ContextInterface}> */
2729
private SplQueue $queue;
2830

2931
private bool $closed = false;
@@ -32,6 +34,7 @@ public function __construct(SpanExporterInterface $exporter)
3234
{
3335
$this->exporter = $exporter;
3436

37+
$this->exportContext = Context::getCurrent();
3538
$this->queue = new SplQueue();
3639
}
3740

@@ -49,7 +52,7 @@ public function onEnd(ReadableSpanInterface $span): void
4952
}
5053

5154
$spanData = $span->toSpanData();
52-
$this->flush(fn () => $this->exporter->export([$spanData])->await(), 'export');
55+
$this->flush(fn () => $this->exporter->export([$spanData])->await(), 'export', false, $this->exportContext);
5356
}
5457

5558
public function forceFlush(?CancellationInterface $cancellation = null): bool
@@ -58,7 +61,7 @@ public function forceFlush(?CancellationInterface $cancellation = null): bool
5861
return false;
5962
}
6063

61-
return $this->flush(fn (): bool => $this->exporter->forceFlush($cancellation), __FUNCTION__, true);
64+
return $this->flush(fn (): bool => $this->exporter->forceFlush($cancellation), __FUNCTION__, true, Context::getCurrent());
6265
}
6366

6467
public function shutdown(?CancellationInterface $cancellation = null): bool
@@ -69,12 +72,12 @@ public function shutdown(?CancellationInterface $cancellation = null): bool
6972

7073
$this->closed = true;
7174

72-
return $this->flush(fn (): bool => $this->exporter->shutdown($cancellation), __FUNCTION__, true);
75+
return $this->flush(fn (): bool => $this->exporter->shutdown($cancellation), __FUNCTION__, true, Context::getCurrent());
7376
}
7477

75-
private function flush(Closure $task, string $taskName, bool $propagateResult = false): bool
78+
private function flush(Closure $task, string $taskName, bool $propagateResult, ContextInterface $context): bool
7679
{
77-
$this->queue->enqueue([$task, $taskName, $propagateResult && !$this->running]);
80+
$this->queue->enqueue([$task, $taskName, $propagateResult && !$this->running, $context]);
7881

7982
if ($this->running) {
8083
return false;
@@ -86,7 +89,8 @@ private function flush(Closure $task, string $taskName, bool $propagateResult =
8689

8790
try {
8891
while (!$this->queue->isEmpty()) {
89-
[$task, $taskName, $propagateResult] = $this->queue->dequeue();
92+
[$task, $taskName, $propagateResult, $context] = $this->queue->dequeue();
93+
$scope = $context->activate();
9094

9195
try {
9296
$result = $task();
@@ -96,10 +100,11 @@ private function flush(Closure $task, string $taskName, bool $propagateResult =
96100
} catch (Throwable $e) {
97101
if ($propagateResult) {
98102
$exception = $e;
99-
100-
continue;
103+
} else {
104+
self::logError(sprintf('Unhandled %s error', $taskName), ['exception' => $e]);
101105
}
102-
self::logError(sprintf('Unhandled %s error', $taskName), ['exception' => $e]);
106+
} finally {
107+
$scope->detach();
103108
}
104109
}
105110
} finally {

0 commit comments

Comments
 (0)