From 17c277662c4771b817566034a263cdf940350d02 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Thu, 16 Mar 2023 12:24:35 +1100 Subject: [PATCH] fixing empty tracestate header handling if an empty tracestate header was passed, we were emitting a warning (discovered in the demo app after enabling logging). to fix this, check for empty string as well as null. --- phpstan.neon.dist | 4 ++++ src/API/Trace/TraceState.php | 5 +++-- tests/Unit/API/Trace/TraceStateTest.php | 21 +++++++++++++++++++-- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/phpstan.neon.dist b/phpstan.neon.dist index e8aa05967..fc15014fb 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -27,5 +27,9 @@ parameters: - tests - message: "#Call to an undefined method .*:shouldHaveReceived.*#" + paths: + - tests + - + message: "#Call to an undefined method .*:expects.*#" paths: - tests \ No newline at end of file diff --git a/src/API/Trace/TraceState.php b/src/API/Trace/TraceState.php index 6e87767bc..a1dd3a5bf 100644 --- a/src/API/Trace/TraceState.php +++ b/src/API/Trace/TraceState.php @@ -30,9 +30,10 @@ class TraceState implements TraceStateInterface public function __construct(string $rawTracestate = null) { - if ($rawTracestate !== null) { - $this->traceState = $this->parse($rawTracestate); + if ($rawTracestate === null || trim($rawTracestate) === '') { + return; } + $this->traceState = $this->parse($rawTracestate); } /** diff --git a/tests/Unit/API/Trace/TraceStateTest.php b/tests/Unit/API/Trace/TraceStateTest.php index 926fe324d..74310d12b 100644 --- a/tests/Unit/API/Trace/TraceStateTest.php +++ b/tests/Unit/API/Trace/TraceStateTest.php @@ -7,18 +7,22 @@ use OpenTelemetry\API\Common\Log\LoggerHolder; use OpenTelemetry\API\Trace\TraceState; use PHPUnit\Framework\TestCase; -use Psr\Log\NullLogger; +use Psr\Log\LoggerInterface; use function str_repeat; use function strlen; /** * @covers OpenTelemetry\API\Trace\TraceState + * @psalm-suppress UndefinedInterfaceMethod */ class TraceStateTest extends TestCase { + private LoggerInterface $logger; + public function setUp(): void { - LoggerHolder::set(new NullLogger()); + $this->logger = $this->createMock(LoggerInterface::class); + LoggerHolder::set($this->logger); } public function test_get_tracestate_value(): void @@ -28,6 +32,19 @@ public function test_get_tracestate_value(): void $this->assertSame('value1', $tracestate->get('vendor1')); } + public function test_get_tracestate_with_empty_string(): void + { + $this->logger->expects($this->never())->method('log')->with( + $this->equalTo('warning'), + $this->anything(), + $this->anything(), + ); + + $tracestate = new TraceState(''); + + $this->assertSame(0, $tracestate->getListMemberCount()); + } + public function test_with_tracestate_value(): void { $tracestate = new TraceState('vendor1=value1');