99use InvalidArgumentException ;
1010use OpenTelemetry \API \Metrics \MeterProviderInterface ;
1111use OpenTelemetry \API \Metrics \ObserverInterface ;
12+ use OpenTelemetry \Context \Context ;
1213use OpenTelemetry \Context \ContextInterface ;
1314use OpenTelemetry \SDK \Behavior \LogsMessagesTrait ;
1415use 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 {
0 commit comments