Skip to content

Commit

Permalink
Improves some variable namings.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcchavezs committed Nov 21, 2017
1 parent 7903d0f commit 37f3602
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 46 deletions.
6 changes: 3 additions & 3 deletions src/Zipkin/DefaultTracing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);

Expand Down
46 changes: 31 additions & 15 deletions src/Zipkin/TraceContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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));
}
Expand All @@ -65,32 +77,34 @@ 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(
$traceId,
$nextId,
null,
$samplingFlags->isSampled(),
$samplingFlags->isDebug()
$samplingFlags->isDebug(),
$usesTraceId128bits
);
}

Expand All @@ -107,7 +121,8 @@ public static function createFromParent(TraceContext $parent)
$nextId,
$parent->spanId,
$parent->isSampled,
$parent->isDebug
$parent->isDebug,
$parent->usesTraceId128bits
);
}

Expand All @@ -130,9 +145,9 @@ public function isDebug()
/**
* @return bool
*/
public function isTraceId128bits()
public function usesTraceId128bits()
{
return $this->traceId128bits;
return $this->usesTraceId128bits;
}

/**
Expand Down Expand Up @@ -178,7 +193,8 @@ public function withSampled($isSampled)
$this->spanId,
$this->parentId,
$isSampled,
$this->isDebug
$this->isDebug,
$this->usesTraceId128bits
);
}

Expand Down
10 changes: 5 additions & 5 deletions src/Zipkin/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class Tracer
/**
* @var bool
*/
private $traceId128bits;
private $usesTraceId128bits;

/**
* @var Recorder
Expand All @@ -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;
}

Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions src/Zipkin/TracingBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class TracingBuilder
/**
* @var bool
*/
private $traceId128bits = false;
private $usesTraceId128bits = false;

public static function create()
{
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -136,7 +136,7 @@ public function build()
$this->localEndpoint,
$this->reporter,
$this->sampler,
$this->traceId128bits
$this->usesTraceId128bits
);
}
}
61 changes: 44 additions & 17 deletions tests/Unit/TraceContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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],
];
}
}
3 changes: 2 additions & 1 deletion tests/Unit/TracerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand All @@ -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()
Expand Down

0 comments on commit 37f3602

Please sign in to comment.