Skip to content

Commit b7a7354

Browse files
authored
Merge pull request #11 from code-tool/alternative-128bit-support
Alternative 128bit support
2 parents dc086aa + f86b2ab commit b7a7354

File tree

5 files changed

+77
-45
lines changed

5 files changed

+77
-45
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
}
88
},
99
"require": {
10-
"php": ">=7.1",
10+
"php": ">=7.4",
1111
"ext-sockets": "*",
1212
"apache/thrift": ">=0.11, <0.16"
1313
},

src/Codec/TextCodec.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ 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
}
19+
[$traceIdHigh, $traceIdLow] = $this->convertInt128($elements[0]);
1920

2021
return new SpanContext(
21-
$this->convertInt64($elements[0]),
22+
$traceIdHigh,
23+
$traceIdLow,
2224
$this->convertInt64($elements[1]),
2325
$this->convertInt64($elements[2]),
2426
$this->convertInt64($elements[3])
@@ -27,16 +29,27 @@ public function decode($data): ?SpanContext
2729

2830
public function convertInt64(string $hex): int
2931
{
30-
$hex8byte = str_pad($hex, 16, '0', STR_PAD_LEFT);
32+
$hex8byte = \str_pad($hex, 16, '0', STR_PAD_LEFT);
3133

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

35-
public function encode(SpanContext $context)
37+
public function convertInt128(string $hex): array
3638
{
37-
return sprintf(
38-
'%x:%x:%x:%x',
39-
$context->getTraceId(),
39+
$hex16byte = \str_pad($hex, 32, '0', STR_PAD_LEFT);
40+
41+
return [
42+
$this->convertInt64(substr($hex16byte, 0, 16)),
43+
$this->convertInt64(substr($hex16byte, 16, 16)),
44+
];
45+
}
46+
47+
public function encode(SpanContext $context): string
48+
{
49+
return \sprintf(
50+
'%x%x:%x:%x:%x',
51+
$context->getTraceIdHigh(),
52+
$context->getTraceIdLow(),
4053
$context->getSpanId(),
4154
$context->getParentId(),
4255
$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: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,25 @@
1212

1313
class SpanFactory implements SpanFactoryInterface
1414
{
15-
private $idGenerator;
15+
private IdGeneratorInterface $idGenerator;
1616

17-
private $sampler;
17+
private SamplerInterface $sampler;
1818

19-
public function __construct(IdGeneratorInterface $idGenerator, SamplerInterface $sampler)
19+
private bool $trace128;
20+
21+
public function __construct(IdGeneratorInterface $idGenerator, SamplerInterface $sampler, bool $trace128 = false)
2022
{
2123
$this->idGenerator = $idGenerator;
2224
$this->sampler = $sampler;
25+
$this->trace128 = $trace128;
2326
}
2427

2528
public function parent(
2629
TracerInterface $tracer,
27-
string $operationName,
28-
string $debugId,
29-
array $tags = [],
30-
array $logs = []
30+
string $operationName,
31+
string $debugId,
32+
array $tags = [],
33+
array $logs = []
3134
): SpanInterface {
3235
$spanId = $this->idGenerator->next();
3336
$traceId = $spanId;
@@ -36,8 +39,9 @@ public function parent(
3639
return new Span(
3740
$tracer,
3841
new SpanContext(
39-
(int)$traceId,
40-
(int)$spanId,
42+
$this->trace128 ? $this->idGenerator->next() : 0,
43+
$this->idGenerator->next(),
44+
$this->idGenerator->next(),
4145
0,
4246
(int)$samplerResult->getFlags()
4347
),
@@ -50,15 +54,16 @@ public function parent(
5054

5155
public function child(
5256
TracerInterface $tracer,
53-
string $operationName,
54-
SpanContext $parentContext,
55-
array $tags = [],
56-
array $logs = []
57+
string $operationName,
58+
SpanContext $parentContext,
59+
array $tags = [],
60+
array $logs = []
5761
): SpanInterface {
5862
return new Span(
5963
$tracer,
6064
new SpanContext(
61-
$parentContext->getTraceId(),
65+
$parentContext->getTraceIdHigh(),
66+
$parentContext->getTraceIdLow(),
6267
$this->idGenerator->next(),
6368
$parentContext->getSpanId(),
6469
$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)