Skip to content

Commit 3286019

Browse files
authored
creating a decoder to treat the traceId and spandId as jaeger client does (#18)
1 parent 0e1297c commit 3286019

File tree

3 files changed

+75
-8
lines changed

3 files changed

+75
-8
lines changed

src/Support/JaegerDecoder.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf + OpenCodeCo
6+
*
7+
* @link https://opencodeco.dev
8+
* @document https://hyperf.wiki
9+
* @contact leo@opencodeco.dev
10+
* @license https://github.com/opencodeco/hyperf-metric/blob/main/LICENSE
11+
*/
12+
namespace Hyperf\Tracer\Support;
13+
14+
final class JaegerDecoder
15+
{
16+
/**
17+
* caused by https://github.com/jonahgeorge/jaeger-client-php/blob/39e35bc3168da12cf596038cd1332e700b5131e9/src/Jaeger/Mapper/SpanToJaegerMapper.php#L181
18+
*/
19+
public static function traceIdDecoder(string $traceId): string
20+
{
21+
if (strlen($traceId) == 32 || !is_numeric($traceId)) {
22+
return $traceId;
23+
}
24+
25+
return substr($traceId, 0, 16) . substr($traceId, -16, 16);
26+
}
27+
28+
public static function spanIdDecoder(string $spanId): string
29+
{
30+
if (!is_numeric($spanId) || strlen($spanId) == 16) {
31+
return $spanId;
32+
}
33+
return strtolower(dechex((int)$spanId));
34+
}
35+
}

src/Support/TextCodecOtel.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,13 @@ public function extract($carrier)
111111
*/
112112
private function spanContextToString($traceId, $spanId, $flags)
113113
{
114-
if (strlen($traceId) < 32) {
115-
$start = mb_substr($traceId, 0, 3);
116-
$end = mb_substr($traceId, -3);
117-
$middle = mb_substr($traceId, 3, mb_strlen($traceId) - 6);
118-
$traceId = $start . $middle . $middle . $end;
119-
$spanId = strtolower(dechex((int)$spanId));
120-
}
121114
$flags = str_pad($flags, 2, "0", STR_PAD_LEFT);
122-
return sprintf('%s-%s-%s-%s', self::VERSION, $traceId, $spanId, $flags);
115+
return sprintf('%s-%s-%s-%s',
116+
self::VERSION,
117+
JaegerDecoder::traceIdDecoder($traceId),
118+
JaegerDecoder::spanIdDecoder($spanId),
119+
$flags
120+
);
123121
}
124122

125123
/**

tests/JaegerDecoderTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf + OpenCodeCo
6+
*
7+
* @link https://opencodeco.dev
8+
* @document https://hyperf.wiki
9+
* @contact leo@opencodeco.dev
10+
* @license https://github.com/opencodeco/hyperf-metric/blob/main/LICENSE
11+
*/
12+
namespace HyperfTest\Tracer\Support;
13+
14+
use Hyperf\Tracer\Support\JaegerDecoder;
15+
use PHPUnit\Framework\TestCase;
16+
17+
/**
18+
* @internal
19+
* @coversNothing
20+
*/
21+
final class JaegerDecoderTest extends TestCase
22+
{
23+
public function testTraceIdDecoder(): void
24+
{
25+
self::assertSame('34876401113185248764011131852473', JaegerDecoder::traceIdDecoder('348764011131852473'));
26+
self::assertSame('a36b5a7dcb5f6a542976b89e952e69ab', JaegerDecoder::traceIdDecoder('a36b5a7dcb5f6a542976b89e952e69ab'));
27+
}
28+
29+
public function testSpanIdDecoder(): void
30+
{
31+
self::assertSame('255d1b261e0fcbc3', JaegerDecoder::spanIdDecoder('2692338002764483523'));
32+
self::assertSame('21c2405de0e90c56', JaegerDecoder::spanIdDecoder('21c2405de0e90c56'));
33+
}
34+
}

0 commit comments

Comments
 (0)