Skip to content

Commit d05a08b

Browse files
committed
Add 128bit support (no internal BC issues)
1 parent d1a5ee2 commit d05a08b

File tree

4 files changed

+60
-34
lines changed

4 files changed

+60
-34
lines changed

src/Codec/TextCodec.php

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,45 @@ class TextCodec implements CodecInterface
99
{
1010
public function decode($data): ?SpanContext
1111
{
12-
if (false === is_string($data)) {
12+
if (false === \is_string($data)) {
1313
return null;
1414
}
15-
$elements = explode(':', $data);
16-
if (4 !== count($elements)) {
15+
$elements = \explode(':', $data);
16+
if (4 !== \count($elements)) {
1717
return null;
1818
}
1919

2020
return new SpanContext(
21-
$this->convertInt64($elements[0]),
22-
$this->convertInt64($elements[1]),
23-
$this->convertInt64($elements[2]),
24-
$this->convertInt64($elements[3])
21+
...$this->convertInt128($elements[0]),
22+
$this->convertInt64($elements[1]),
23+
$this->convertInt64($elements[2]),
24+
$this->convertInt64($elements[3])
2525
);
2626
}
2727

2828
public function convertInt64(string $hex): int
2929
{
30-
$hex8byte = str_pad($hex, 16, '0', STR_PAD_LEFT);
30+
$hex8byte = \str_pad($hex, 16, '0', STR_PAD_LEFT);
3131

32-
return unpack('Jint64', pack('H*', $hex8byte))['int64'];
32+
return \unpack('Jint64', pack('H*', $hex8byte))['int64'];
3333
}
3434

35-
public function encode(SpanContext $context)
35+
public function convertInt128(string $hex): array
3636
{
37-
return sprintf(
38-
'%x:%x:%x:%x',
39-
$context->getTraceId(),
37+
$hex16byte = \str_pad($hex, 32, '0', STR_PAD_LEFT);
38+
39+
return [
40+
$this->convertInt64(substr($hex16byte, 0, 16)),
41+
$this->convertInt64(substr($hex16byte, 16, 16)),
42+
];
43+
}
44+
45+
public function encode(SpanContext $context): string
46+
{
47+
return \sprintf(
48+
'%x%x:%x:%x:%x',
49+
$context->getTraceIdHigh(),
50+
$context->getTraceIdLow(),
4051
$context->getSpanId(),
4152
$context->getParentId(),
4253
$context->getFlags()

src/Span/Context/SpanContext.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,28 @@
55

66
class SpanContext implements \IteratorAggregate
77
{
8-
private $traceId;
8+
private int $traceIdHigh;
99

10-
private $spanId;
10+
private int $traceIdLow;
1111

12-
private $parentId;
12+
private int $spanId;
1313

14-
private $flags;
14+
private int $parentId;
1515

16-
private $baggage;
16+
private int $flags;
17+
18+
private array $baggage;
1719

1820
public function __construct(
19-
int $traceId,
20-
int $spanId,
21-
int $parentId,
22-
int $flags = 0,
21+
int $traceIdHigh,
22+
int $traceIdLow,
23+
int $spanId,
24+
int $parentId,
25+
int $flags = 0,
2326
array $baggage = []
2427
) {
25-
$this->traceId = $traceId;
28+
$this->traceIdHigh = $traceIdHigh;
29+
$this->traceIdLow = $traceIdLow;
2630
$this->spanId = $spanId;
2731
$this->parentId = $parentId;
2832
$this->flags = $flags;
@@ -31,7 +35,17 @@ public function __construct(
3135

3236
public function getTraceId(): int
3337
{
34-
return $this->traceId;
38+
return $this->traceIdLow;
39+
}
40+
41+
public function getTraceIdHigh(): int
42+
{
43+
return $this->traceIdHigh;
44+
}
45+
46+
public function getTraceIdLow(): int
47+
{
48+
return $this->traceIdLow;
3549
}
3650

3751
public function getSpanId(): int

src/Span/Factory/SpanFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public function child(
5858
return new Span(
5959
$tracer,
6060
new SpanContext(
61-
$parentContext->getTraceId(),
61+
$parentContext->getTraceIdHigh(),
62+
$parentContext->getTraceIdLow(),
6263
$this->idGenerator->next(),
6364
$parentContext->getSpanId(),
6465
$parentContext->getFlags(),

src/Span/Span.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@
1212

1313
class Span extends \Jaeger\Thrift\Span implements SpanInterface
1414
{
15-
private $tracer;
15+
private FinishableInterface $tracer;
1616

17-
private $context;
17+
private SpanContext $context;
1818

1919
public function __construct(
2020
FinishableInterface $tracer,
21-
SpanContext $context,
22-
string $operationName,
23-
int $startTime,
24-
array $tags = [],
25-
array $logs = []
21+
SpanContext $context,
22+
string $operationName,
23+
int $startTime,
24+
array $tags = [],
25+
array $logs = []
2626
) {
2727
$this->tracer = $tracer;
2828
$this->context = $context;
29-
$this->traceIdLow = $context->getTraceId();
30-
$this->traceIdHigh = 0;
29+
$this->traceIdLow = $context->getTraceIdLow();
30+
$this->traceIdHigh = $context->getTraceIdHigh();
3131
$this->spanId = $context->getSpanId();
3232
$this->parentSpanId = $context->getParentId();
3333
$this->flags = $context->getFlags();

0 commit comments

Comments
 (0)