Skip to content

Commit

Permalink
fixing empty tracestate header handling (#936)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
brettmc authored Mar 16, 2023
1 parent 640b6f5 commit 0538a5a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
4 changes: 4 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@ parameters:
- tests
-
message: "#Call to an undefined method .*:shouldHaveReceived.*#"
paths:
- tests
-
message: "#Call to an undefined method .*:expects.*#"
paths:
- tests
5 changes: 3 additions & 2 deletions src/API/Trace/TraceState.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
21 changes: 19 additions & 2 deletions tests/Unit/API/Trace/TraceStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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');
Expand Down

0 comments on commit 0538a5a

Please sign in to comment.