Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixing empty tracestate header handling #936

Merged
merged 1 commit into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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