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

Add test based on the Jaeger spec #645

Merged
merged 5 commits into from
Mar 31, 2022
Merged
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
33 changes: 28 additions & 5 deletions tests/Unit/Contrib/IdConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,45 @@
*/
class IdConverterTest extends TestCase
{
public function test_correctly_converted_span_id()
//Based on this section of the Jaeger spec https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk_exporters/jaeger.md#ids
public function test_correctly_converts_example_from_spec()
{
$hex = 'FF00000000000000';

$this->assertEquals(-72057594037927936, IdConverter::convertOtelToJaegerSpanId($hex));
brettmc marked this conversation as resolved.
Show resolved Hide resolved
}

public function test_correctly_converts_random_span_id()
{
// 16 char hex string
$hex = bin2hex(random_bytes(8));

$this->assertEquals($hex, dechex(IdConverter::convertOtelToJaegerSpanId($hex)));
$this->assertEquals($hex, $this->dechexWithLeadingZeroes(IdConverter::convertOtelToJaegerSpanId($hex)));
}

public function test_correctly_converted_trace_id()
public function test_correctly_converts_trace_id()
{
$hex = '0102030405060708090a0b0c0d0e0f10';

$traceId = IdConverter::convertOtelToJaegerTraceIds($hex);

$this->assertEquals(72623859790382856, $traceId['traceIdHigh']);
$this->assertEquals(651345242494996240, $traceId['traceIdLow']);
}

public function test_correctly_converts_random_trace_id()
{
// 32 char hex string
$hex = bin2hex(random_bytes(16));

$traceId = IdConverter::convertOtelToJaegerTraceIds($hex);
$convertedTraceId = dechex((int) $traceId['traceIdHigh']);
$convertedTraceId .= dechex((int) $traceId['traceIdLow']);
$convertedTraceId = $this->dechexWithLeadingZeroes($traceId['traceIdHigh']);
$convertedTraceId .= $this->dechexWithLeadingZeroes($traceId['traceIdLow']);
$this->assertEquals($hex, $convertedTraceId);
}

private function dechexWithLeadingZeroes(int $num)
{
return str_pad(dechex($num), 16, '0', STR_PAD_LEFT);
}
}