Skip to content

Commit

Permalink
tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
brettmc committed Dec 12, 2022
1 parent 9c0d218 commit 31c30c0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
10 changes: 5 additions & 5 deletions src/API/Trace/TraceState.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class TraceState implements TraceStateInterface
{
use LogsMessagesTrait;

public const MAX_TRACESTATE_LIST_MEMBERS = 32; //@see https://www.w3.org/TR/trace-context/#tracestate-header-field-values
public const MAX_TRACESTATE_COMBINED_LENGTH = 512; //@see https://www.w3.org/TR/trace-context/#tracestate-limits
public const MAX_LIST_MEMBERS = 32; //@see https://www.w3.org/TR/trace-context/#tracestate-header-field-values
public const MAX_COMBINED_LENGTH = 512; //@see https://www.w3.org/TR/trace-context/#tracestate-limits
public const LIST_MEMBERS_SEPARATOR = ',';
public const LIST_MEMBER_KEY_VALUE_SPLITTER = '=';
private const VALID_KEY_CHAR_RANGE = '[_0-9a-z-*\/]';
Expand Down Expand Up @@ -126,15 +126,15 @@ public function __toString(): string
*/
private function parse(string $rawTracestate): array
{
if (strlen($rawTracestate) > self::MAX_TRACESTATE_COMBINED_LENGTH) {
self::logWarning('tracestate discarded, exceeds max combined length: ' . self::MAX_TRACESTATE_COMBINED_LENGTH);
if (strlen($rawTracestate) > self::MAX_COMBINED_LENGTH) {
self::logWarning('tracestate discarded, exceeds max combined length: ' . self::MAX_COMBINED_LENGTH);

return [];
}
$parsedTracestate = [];
$listMembers = explode(self::LIST_MEMBERS_SEPARATOR, $rawTracestate);

if (count($listMembers) > self::MAX_TRACESTATE_LIST_MEMBERS) {
if (count($listMembers) > self::MAX_LIST_MEMBERS) {
self::logWarning('tracestate discarded, too many members');

return [];
Expand Down
16 changes: 8 additions & 8 deletions tests/Unit/API/Trace/TraceStateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function test_to_string_tracestate(): void
public function test_max_tracestate_list_members(): void
{
// Build a tracestate with the max 32 values. Ex '0=0,1=1,...,31=31'
$rawTraceState = range(0, TraceState::MAX_TRACESTATE_LIST_MEMBERS - 1);
$rawTraceState = range(0, TraceState::MAX_LIST_MEMBERS - 1);
array_walk($rawTraceState, static function (&$v, $k) {
$v = 'k' . $k . TraceState::LIST_MEMBER_KEY_VALUE_SPLITTER . 'v' . $v;
});
Expand All @@ -84,14 +84,14 @@ public function test_max_tracestate_list_members(): void
* @var array $rawTraceState
* @see https://github.com/vimeo/psalm/issues/6394
*/
$this->assertCount(TraceState::MAX_TRACESTATE_LIST_MEMBERS, $rawTraceState);
$this->assertCount(TraceState::MAX_LIST_MEMBERS, $rawTraceState);

$validTracestate = new TraceState(implode(TraceState::LIST_MEMBERS_SEPARATOR, $rawTraceState));
$this->assertSame(TraceState::MAX_TRACESTATE_LIST_MEMBERS, $validTracestate->getListMemberCount());
$this->assertSame(TraceState::MAX_LIST_MEMBERS, $validTracestate->getListMemberCount());

// Add a list-member to the tracestate that exceeds the max of 32. This will cause the tracestate to be discarded.
$rawTraceState[32] = 'k32' . TraceState::LIST_MEMBER_KEY_VALUE_SPLITTER . 'v32';
$this->assertCount(TraceState::MAX_TRACESTATE_LIST_MEMBERS + 1, $rawTraceState);
$this->assertCount(TraceState::MAX_LIST_MEMBERS + 1, $rawTraceState);

$truncatedTracestate = new TraceState(implode(TraceState::LIST_MEMBERS_SEPARATOR, $rawTraceState));
$this->assertSame(0, $truncatedTracestate->getListMemberCount());
Expand All @@ -100,21 +100,21 @@ public function test_max_tracestate_list_members(): void
public function test_max_tracestate_length(): void
{
// Build a vendor key with a length of 256 characters. The max characters allowed.
$vendorKey = str_repeat('k', TraceState::MAX_TRACESTATE_COMBINED_LENGTH / 2);
$vendorKey = str_repeat('k', TraceState::MAX_COMBINED_LENGTH / 2);

// Build a vendor value with a length of 255 characters. One below the max allowed.
$vendorValue = str_repeat('v', TraceState::MAX_TRACESTATE_COMBINED_LENGTH / 2 - 1);
$vendorValue = str_repeat('v', TraceState::MAX_COMBINED_LENGTH / 2 - 1);

// tracestate length = 513 characters (not accepted).
$rawTraceState = $vendorKey . TraceState::LIST_MEMBER_KEY_VALUE_SPLITTER . $vendorValue . 'v';
$this->assertGreaterThan(TraceState::MAX_TRACESTATE_COMBINED_LENGTH, strlen($rawTraceState));
$this->assertGreaterThan(TraceState::MAX_COMBINED_LENGTH, strlen($rawTraceState));

$validTracestate = new TraceState($rawTraceState);
$this->assertNull($validTracestate->get($vendorKey));

// tracestate length = 512 characters (accepted).
$rawTraceState = $vendorKey . TraceState::LIST_MEMBER_KEY_VALUE_SPLITTER . $vendorValue;
$this->assertSame(TraceState::MAX_TRACESTATE_COMBINED_LENGTH, strlen($rawTraceState));
$this->assertSame(TraceState::MAX_COMBINED_LENGTH, strlen($rawTraceState));

$validTracestate = new TraceState($rawTraceState);
$this->assertSame($rawTraceState, (string) $validTracestate);
Expand Down

0 comments on commit 31c30c0

Please sign in to comment.