diff --git a/src/SDK/Trace/SpanProcessor/BatchSpanProcessor.php b/src/SDK/Trace/SpanProcessor/BatchSpanProcessor.php index 1faddaba3..e7837fca0 100644 --- a/src/SDK/Trace/SpanProcessor/BatchSpanProcessor.php +++ b/src/SDK/Trace/SpanProcessor/BatchSpanProcessor.php @@ -8,6 +8,7 @@ use function count; use InvalidArgumentException; use OpenTelemetry\Context\Context; +use OpenTelemetry\SDK\Behavior\LogsMessagesTrait; use OpenTelemetry\SDK\Common\Future\CancellationInterface; use OpenTelemetry\SDK\Common\Time\ClockInterface; use OpenTelemetry\SDK\Trace\ReadableSpanInterface; @@ -15,15 +16,13 @@ use OpenTelemetry\SDK\Trace\SpanDataInterface; use OpenTelemetry\SDK\Trace\SpanExporterInterface; use OpenTelemetry\SDK\Trace\SpanProcessorInterface; -use Psr\Log\LoggerAwareInterface; -use Psr\Log\LoggerAwareTrait; use SplQueue; use function sprintf; use Throwable; -class BatchSpanProcessor implements SpanProcessorInterface, LoggerAwareInterface +class BatchSpanProcessor implements SpanProcessorInterface { - use LoggerAwareTrait; + use LogsMessagesTrait; public const DEFAULT_SCHEDULE_DELAY = 5000; public const DEFAULT_EXPORT_TIMEOUT = 30000; @@ -166,9 +165,7 @@ private function flush(?string $flushMethod = null, ?CancellationInterface $canc continue; } - if ($this->logger !== null) { - $this->logger->error(sprintf('Unhandled %s error', $flushMethod), ['exception' => $e]); - } + self::logError(sprintf('Unhandled %s error', $flushMethod), ['exception' => $e]); } } @@ -185,9 +182,7 @@ private function flush(?string $flushMethod = null, ?CancellationInterface $canc try { $this->exporter->export($this->queue->dequeue())->await(); } catch (Throwable $e) { - if ($this->logger !== null) { - $this->logger->error('Unhandled export error', ['exception' => $e]); - } + self::logError('Unhandled export error', ['exception' => $e]); } finally { $this->queueSize -= $batchSize; } diff --git a/src/SDK/Trace/SpanProcessor/SimpleSpanProcessor.php b/src/SDK/Trace/SpanProcessor/SimpleSpanProcessor.php index 24928fe55..c4c7003f6 100644 --- a/src/SDK/Trace/SpanProcessor/SimpleSpanProcessor.php +++ b/src/SDK/Trace/SpanProcessor/SimpleSpanProcessor.php @@ -6,20 +6,19 @@ use Closure; use OpenTelemetry\Context\Context; +use OpenTelemetry\SDK\Behavior\LogsMessagesTrait; use OpenTelemetry\SDK\Common\Future\CancellationInterface; use OpenTelemetry\SDK\Trace\ReadableSpanInterface; use OpenTelemetry\SDK\Trace\ReadWriteSpanInterface; use OpenTelemetry\SDK\Trace\SpanExporterInterface; use OpenTelemetry\SDK\Trace\SpanProcessorInterface; -use Psr\Log\LoggerAwareInterface; -use Psr\Log\LoggerAwareTrait; use SplQueue; use function sprintf; use Throwable; -class SimpleSpanProcessor implements SpanProcessorInterface, LoggerAwareInterface +class SimpleSpanProcessor implements SpanProcessorInterface { - use LoggerAwareTrait; + use LogsMessagesTrait; private SpanExporterInterface $exporter; @@ -100,9 +99,7 @@ private function flush(Closure $task, string $taskName, bool $propagateResult = continue; } - if ($this->logger !== null) { - $this->logger->error(sprintf('Unhandled %s error', $taskName), ['exception' => $e]); - } + self::logError(sprintf('Unhandled %s error', $taskName), ['exception' => $e]); } } } finally { diff --git a/tests/Unit/SDK/Trace/SpanProcessor/BatchSpanProcessorTest.php b/tests/Unit/SDK/Trace/SpanProcessor/BatchSpanProcessorTest.php index b63010fa2..10920340c 100644 --- a/tests/Unit/SDK/Trace/SpanProcessor/BatchSpanProcessorTest.php +++ b/tests/Unit/SDK/Trace/SpanProcessor/BatchSpanProcessorTest.php @@ -11,6 +11,7 @@ use OpenTelemetry\API\Trace as API; use OpenTelemetry\Context\Context; use OpenTelemetry\SDK\Common\Future\CompletedFuture; +use OpenTelemetry\SDK\Common\Log\LoggerHolder; use OpenTelemetry\SDK\Common\Time\ClockFactory; use OpenTelemetry\SDK\Common\Time\ClockInterface; use OpenTelemetry\SDK\Trace\ReadWriteSpanInterface; @@ -20,6 +21,7 @@ use OpenTelemetry\SDK\Trace\SpanProcessorInterface; use OpenTelemetry\Tests\Unit\SDK\Util\TestClock; use Psr\Log\LoggerInterface; +use Psr\Log\LogLevel; /** * @covers \OpenTelemetry\SDK\Trace\SpanProcessor\BatchSpanProcessor @@ -360,16 +362,22 @@ public function test_throwing_exporter_export(): void $exporter->method('export')->willThrowException(new LogicException()); $logger = $this->createMock(LoggerInterface::class); - $logger->expects($this->once())->method('error'); + $logger->expects($this->once())->method('log')->with(LogLevel::ERROR); $processor = new BatchSpanProcessor($exporter, $this->testClock); - $processor->setLogger($logger); $span = $this->createSampledSpanMock(); $processor->onStart($span, Context::getCurrent()); $processor->onEnd($span); - $processor->forceFlush(); + $previousLogger = LoggerHolder::get(); + LoggerHolder::set($logger); + + try { + $processor->forceFlush(); + } finally { + LoggerHolder::set($previousLogger); + } } public function test_throwing_exporter_flush(): void @@ -401,16 +409,22 @@ public function test_throwing_exporter_flush_cannot_rethrow_in_original_caller_l $exporter->method('shutdown')->willThrowException(new LogicException()); $logger = $this->createMock(LoggerInterface::class); - $logger->expects($this->once())->method('error'); + $logger->expects($this->once())->method('log')->with(LogLevel::ERROR); $processor = new BatchSpanProcessor($exporter, $this->testClock); - $processor->setLogger($logger); $span = $this->createSampledSpanMock(); $processor->onStart($span, Context::getCurrent()); $processor->onEnd($span); - $processor->forceFlush(); + $previousLogger = LoggerHolder::get(); + LoggerHolder::set($logger); + + try { + $processor->forceFlush(); + } finally { + LoggerHolder::set($previousLogger); + } } public function test_throwing_exporter_flush_rethrows_in_original_caller(): void diff --git a/tests/Unit/SDK/Trace/SpanProcessor/SimpleSpanProcessorTest.php b/tests/Unit/SDK/Trace/SpanProcessor/SimpleSpanProcessorTest.php index b24cb5aff..1bfe4a6f3 100644 --- a/tests/Unit/SDK/Trace/SpanProcessor/SimpleSpanProcessorTest.php +++ b/tests/Unit/SDK/Trace/SpanProcessor/SimpleSpanProcessorTest.php @@ -12,12 +12,14 @@ use OpenTelemetry\API\Trace\SpanContextInterface; use OpenTelemetry\Context\Context; use OpenTelemetry\SDK\Common\Future\CompletedFuture; +use OpenTelemetry\SDK\Common\Log\LoggerHolder; use OpenTelemetry\SDK\Trace\ReadableSpanInterface; use OpenTelemetry\SDK\Trace\ReadWriteSpanInterface; use OpenTelemetry\SDK\Trace\SpanExporterInterface; use OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor; use OpenTelemetry\Tests\Unit\SDK\Util\SpanData; use Psr\Log\LoggerInterface; +use Psr\Log\LogLevel; /** * @covers \OpenTelemetry\SDK\Trace\SpanProcessor\SimpleSpanProcessor @@ -138,16 +140,22 @@ public function test_throwing_exporter_export(): void $exporter->method('export')->willThrowException(new LogicException()); $logger = $this->createMock(LoggerInterface::class); - $logger->expects($this->once())->method('error'); + $logger->expects($this->once())->method('log')->with(LogLevel::ERROR); $processor = new SimpleSpanProcessor($exporter); - $processor->setLogger($logger); $this->readableSpan->expects('getContext')->andReturn($this->sampledSpanContext); $this->readableSpan->expects('toSpanData')->andReturn(new SpanData()); - $processor->onStart($this->readWriteSpan, Context::getCurrent()); - $processor->onEnd($this->readableSpan); + $previousLogger = LoggerHolder::get(); + LoggerHolder::set($logger); + + try { + $processor->onStart($this->readWriteSpan, Context::getCurrent()); + $processor->onEnd($this->readableSpan); + } finally { + LoggerHolder::set($previousLogger); + } } public function test_throwing_exporter_flush(): void