Skip to content

Commit

Permalink
Use LogsMessagesTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
Nevay committed Aug 17, 2022
1 parent 4a2b869 commit d309f83
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 27 deletions.
15 changes: 5 additions & 10 deletions src/SDK/Trace/SpanProcessor/BatchSpanProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,21 @@
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;
use OpenTelemetry\SDK\Trace\ReadWriteSpanInterface;
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;
Expand Down Expand Up @@ -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]);
}
}

Expand All @@ -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;
}
Expand Down
11 changes: 4 additions & 7 deletions src/SDK/Trace/SpanProcessor/SimpleSpanProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down
26 changes: 20 additions & 6 deletions tests/Unit/SDK/Trace/SpanProcessor/BatchSpanProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions tests/Unit/SDK/Trace/SpanProcessor/SimpleSpanProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d309f83

Please sign in to comment.