diff --git a/src/Zipkin/DefaultTracing.php b/src/Zipkin/DefaultTracing.php index e33ea808..b1223962 100644 --- a/src/Zipkin/DefaultTracing.php +++ b/src/Zipkin/DefaultTracing.php @@ -27,21 +27,21 @@ final class DefaultTracing implements Tracing * @param Endpoint $localEndpoint * @param Reporter $reporter * @param Sampler $sampler - * @param bool $traceId128bits + * @param bool $usesTraceId128bits * @param bool $isNoop */ public function __construct( Endpoint $localEndpoint, Reporter $reporter, Sampler $sampler, - $traceId128bits = false, + $usesTraceId128bits = false, $isNoop = false ) { $this->tracer = new Tracer( $localEndpoint, $reporter, $sampler, - $traceId128bits, + $usesTraceId128bits, $isNoop ); diff --git a/src/Zipkin/TraceContext.php b/src/Zipkin/TraceContext.php index 73819444..d1bb65d4 100644 --- a/src/Zipkin/TraceContext.php +++ b/src/Zipkin/TraceContext.php @@ -33,13 +33,19 @@ final class TraceContext implements SamplingFlags */ private $parentId; - private function __construct($traceId, $spanId, $parentId, $isSampled, $debug) + /** + * @var bool + */ + private $usesTraceId128bits; + + private function __construct($traceId, $spanId, $parentId, $isSampled, $isDebug, $usesTraceId128bits) { $this->traceId = $traceId; $this->spanId = $spanId; $this->parentId = $parentId; $this->isSampled = $isSampled; - $this->isDebug = $debug; + $this->isDebug = $isDebug; + $this->usesTraceId128bits = $usesTraceId128bits; } /** @@ -48,11 +54,17 @@ private function __construct($traceId, $spanId, $parentId, $isSampled, $debug) * @param string|null $parentId * @param bool|null $isSampled * @param bool $isDebug + * @param bool $usesTraceId128bits * @return TraceContext - * @throws \InvalidArgumentException */ - public static function create($traceId, $spanId, $parentId = null, $isSampled = null, $isDebug = false) - { + public static function create( + $traceId, + $spanId, + $parentId = null, + $isSampled = null, + $isDebug = false, + $usesTraceId128bits = false + ) { if (!self::isValidSpanId($spanId)) { throw new InvalidArgumentException(sprintf('Invalid span id, got %s', $spanId)); } @@ -65,24 +77,25 @@ public static function create($traceId, $spanId, $parentId = null, $isSampled = throw new InvalidArgumentException(sprintf('Invalid parent span id, got %s', $parentId)); } - return new self($traceId, $spanId, $parentId, $isSampled, $isDebug); + return new self($traceId, $spanId, $parentId, $isSampled, $isDebug, $usesTraceId128bits); } /** * @param SamplingFlags|null $samplingFlags + * @param bool $usesTraceId128bits * @return TraceContext */ - public static function createAsRoot(SamplingFlags $samplingFlags = null, $traceId128bits = false) + public static function createAsRoot(SamplingFlags $samplingFlags = null, $usesTraceId128bits = false) { if ($samplingFlags === null) { $samplingFlags = DefaultSamplingFlags::createAsEmpty(); } $nextId = self::nextId(); - if ($traceId128bits) { + + $traceId = $nextId; + if ($usesTraceId128bits) { $traceId = self::traceIdWith128bits(); - } else { - $traceId = $nextId; } return new TraceContext( @@ -90,7 +103,8 @@ public static function createAsRoot(SamplingFlags $samplingFlags = null, $traceI $nextId, null, $samplingFlags->isSampled(), - $samplingFlags->isDebug() + $samplingFlags->isDebug(), + $usesTraceId128bits ); } @@ -107,7 +121,8 @@ public static function createFromParent(TraceContext $parent) $nextId, $parent->spanId, $parent->isSampled, - $parent->isDebug + $parent->isDebug, + $parent->usesTraceId128bits ); } @@ -130,9 +145,9 @@ public function isDebug() /** * @return bool */ - public function isTraceId128bits() + public function usesTraceId128bits() { - return $this->traceId128bits; + return $this->usesTraceId128bits; } /** @@ -178,7 +193,8 @@ public function withSampled($isSampled) $this->spanId, $this->parentId, $isSampled, - $this->isDebug + $this->isDebug, + $this->usesTraceId128bits ); } diff --git a/src/Zipkin/Tracer.php b/src/Zipkin/Tracer.php index 8bb05aff..2b7aaf1f 100644 --- a/src/Zipkin/Tracer.php +++ b/src/Zipkin/Tracer.php @@ -21,7 +21,7 @@ final class Tracer /** * @var bool */ - private $traceId128bits; + private $usesTraceId128bits; /** * @var Recorder @@ -32,19 +32,19 @@ final class Tracer * @param Endpoint $localEndpoint * @param Reporter $reporter * @param Sampler $sampler - * @param bool $traceId128bits + * @param bool $usesTraceId128bits * @param bool $isNoop */ public function __construct( Endpoint $localEndpoint, Reporter $reporter, Sampler $sampler, - $traceId128bits, + $usesTraceId128bits, $isNoop ) { $this->recorder = new Recorder($localEndpoint, $reporter, $isNoop); $this->sampler = $sampler; - $this->traceId128bits = $traceId128bits; + $this->usesTraceId128bits = $usesTraceId128bits; $this->isNoop = $isNoop; } @@ -147,7 +147,7 @@ private function nextContext(SamplingFlags $contextOrFlags) if ($contextOrFlags instanceof TraceContext) { $context = TraceContext::createFromParent($contextOrFlags); } else { - $context = TraceContext::createAsRoot($contextOrFlags, $this->traceId128bits); + $context = TraceContext::createAsRoot($contextOrFlags, $this->usesTraceId128bits); } if ($context->isSampled() === null) { diff --git a/src/Zipkin/TracingBuilder.php b/src/Zipkin/TracingBuilder.php index cc8b93bd..b3274d28 100644 --- a/src/Zipkin/TracingBuilder.php +++ b/src/Zipkin/TracingBuilder.php @@ -31,7 +31,7 @@ class TracingBuilder /** * @var bool */ - private $traceId128bits = false; + private $usesTraceId128bits = false; public static function create() { @@ -103,12 +103,12 @@ public function havingSampler(Sampler $sampler) /** * When true, new root spans will have 128-bit trace IDs. Defaults to false (64-bit) * - * @param $traceId128Bits + * @param $usesTraceId128bits * @return $this */ - public function havingTraceId128bits($traceId128Bits) + public function havingTraceId128bits($usesTraceId128bits) { - $this->traceId128bits = $traceId128Bits; + $this->usesTraceId128bits = $usesTraceId128bits; return $this; } @@ -136,7 +136,7 @@ public function build() $this->localEndpoint, $this->reporter, $this->sampler, - $this->traceId128bits + $this->usesTraceId128bits ); } } diff --git a/tests/Unit/TraceContextTest.php b/tests/Unit/TraceContextTest.php index 666b34a9..98692db7 100644 --- a/tests/Unit/TraceContextTest.php +++ b/tests/Unit/TraceContextTest.php @@ -17,10 +17,11 @@ final class TraceContextTest extends PHPUnit_Framework_TestCase const TEST_INVALID_PARENT_ID = 'invalid_bd7a977555f6b982'; const TEST_INVALID_SPAN_ID = 'invalid_be2d01e33cc78d97'; - public function testCreateAsRootSuccess() + /** + * @dataProvider boolProvider + */ + public function testCreateAsRootSuccess($sampled, $debug) { - $sampled = $this->randomBool(); - $debug = $this->randomBool(); $samplingFlags = DefaultSamplingFlags::create($sampled, $debug); $context = TraceContext::createAsRoot($samplingFlags); @@ -31,10 +32,28 @@ public function testCreateAsRootSuccess() $this->assertEquals($debug, $context->isDebug()); } - public function testCreateFromParentSuccess() + /** + * @dataProvider boolProvider + */ + public function testCreateAsRootSuccessWithTraceId128bits($sampled, $debug) + { + $samplingFlags = DefaultSamplingFlags::create($sampled, $debug); + $context = TraceContext::createAsRoot($samplingFlags, true); + + $this->assertNotNull($context->getTraceId()); + $this->assertTrue($context->usesTraceId128bits()); + $this->assertNotNull($context->getSpanId()); + $this->assertEquals(null, $context->getParentId()); + $this->assertEquals($sampled, $context->isSampled()); + $this->assertEquals($debug, $context->isDebug()); + $this->assertEquals(32, strlen($context->getTraceId())); + } + + /** + * @dataProvider boolProvider + */ + public function testCreateFromParentSuccess($sampled, $debug) { - $sampled = $this->randomBool(); - $debug = $this->randomBool(); $samplingFlags = DefaultSamplingFlags::create($sampled, $debug); $parentContext = TraceContext::create( self::TEST_TRACE_ID, @@ -53,13 +72,14 @@ public function testCreateFromParentSuccess() $this->assertEquals($debug, $childContext->isDebug()); } - public function testCreateFailsDueToInvalidId() + /** + * @dataProvider boolProvider + */ + public function testCreateFailsDueToInvalidId($sampled, $debug) { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Invalid trace id, got invalid_bd7a977555f6b982'); - $sampled = $this->randomBool(); - $debug = $this->randomBool(); TraceContext::create( self::TEST_INVALID_TRACE_ID, self::TEST_SPAN_ID, @@ -69,13 +89,14 @@ public function testCreateFailsDueToInvalidId() ); } - public function testCreateFailsDueToInvalidSpanId() + /** + * @dataProvider boolProvider + */ + public function testCreateFailsDueToInvalidSpanId($sampled, $debug) { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Invalid span id, got invalid_be2d01e33cc78d97'); - $sampled = $this->randomBool(); - $debug = $this->randomBool(); TraceContext::create( self::TEST_TRACE_ID, self::TEST_INVALID_SPAN_ID, @@ -85,13 +106,14 @@ public function testCreateFailsDueToInvalidSpanId() ); } - public function testCreateFailsDueToInvalidParentSpanId() + /** + * @dataProvider boolProvider + */ + public function testCreateFailsDueToInvalidParentSpanId($sampled, $debug) { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('Invalid parent span id, got invalid_bd7a977555f6b982'); - $sampled = $this->randomBool(); - $debug = $this->randomBool(); TraceContext::create( self::TEST_TRACE_ID, self::TEST_SPAN_ID, @@ -101,8 +123,13 @@ public function testCreateFailsDueToInvalidParentSpanId() ); } - private function randomBool() + public function boolProvider() { - return (mt_rand(0, 1) === 1); + return [ + [true, true], + [true, false], + [false, true], + [false, false], + ]; } } diff --git a/tests/Unit/TracerTest.php b/tests/Unit/TracerTest.php index abec0036..9f696352 100644 --- a/tests/Unit/TracerTest.php +++ b/tests/Unit/TracerTest.php @@ -49,6 +49,7 @@ public function testNewTraceSuccess() $this->assertEquals(true, $span->getContext()->isSampled()); $this->assertEquals(false, $span->getContext()->isDebug()); $this->assertNull($span->getContext()->getParentId()); + $this->assertFalse($span->getContext()->usesTraceId128bits()); $this->assertEquals($span->getContext()->getTraceId(), $span->getContext()->getSpanId()); } @@ -69,7 +70,7 @@ public function testNewTraceSuccessWith128bits() $this->assertEquals(true, $span->getContext()->isSampled()); $this->assertEquals(false, $span->getContext()->isDebug()); $this->assertNull($span->getContext()->getParentId()); - $this->assertEquals(strlen($span->getContext()->getTraceId()), 32); + $this->assertTrue($span->getContext()->usesTraceId128bits()); } public function testNewChildIsBeingCreatedAsNoop()