Skip to content

Commit

Permalink
tests(join-spans): adds tests for shared value in joinSpan.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcchavezs committed Jun 16, 2020
1 parent 966a1c4 commit 4b4c86f
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 43 deletions.
5 changes: 4 additions & 1 deletion src/Zipkin/Recording/Span.php
Expand Up @@ -255,10 +255,13 @@ public function toArray(): array
'parentId' => $this->parentId ? $this->parentId : null,
'timestamp' => $this->timestamp,
'duration' => $this->duration,
'debug' => $this->debug,
'localEndpoint' => $this->localEndpoint->toArray(),
];

if ($this->debug === true) {
$spanAsArray['debug'] = true;
}

if ($this->shared === true) {
$spanAsArray['shared'] = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Zipkin/Reporters/Http.php
Expand Up @@ -35,7 +35,7 @@ final class Http implements Reporter
/**
* logger is only meant to be used for development purposes. Enabling
* an actual logger in production could cause a massive amount of data
* that will flood the logs.
* that will flood the logs on failure.
*
* @var LoggerInterface
*/
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/Recording/SpanTest.php
Expand Up @@ -57,7 +57,6 @@ public function testConvertingSpanToArrayHasTheExpectedValues()
'timestamp' => $timestamp,
'name' => 'test_name',
'duration' => 100,
'debug' => false,
'localEndpoint' => [
'serviceName' => 'test_service_name',
'ipv4' => '127.0.0.1',
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/Reporters/HttpTest.php
Expand Up @@ -15,7 +15,7 @@
final class HttpTest extends TestCase
{
const PAYLOAD = '[{"id":"%s","name":null,"traceId":"%s","parentId":null,'
. '"timestamp":null,"duration":null,"debug":false,"localEndpoint":{"serviceName":""}}]';
. '"timestamp":null,"duration":null,"localEndpoint":{"serviceName":""}}]';

public function testConstructorIsRetrocompatible()
{
Expand Down
88 changes: 49 additions & 39 deletions tests/Unit/TracerTest.php
Expand Up @@ -247,12 +247,12 @@ public function emptySamplingFlagsDataProvider()
public function samplingFlagsDataProvider()
{
return [
[ null, true ],
[ null, false ],
[ true, true ],
[ true, false ],
[ false, true ],
[ false, false ],
[null, true],
[null, false],
[true, true],
[true, false],
[false, true],
[false, false],
];
}

Expand Down Expand Up @@ -321,15 +321,7 @@ public function spanForScopeProvider()
*/
public function testInSpanForSuccessfullCall($sumCallable)
{
$reporter = new InMemory();
$tracer = new Tracer(
Endpoint::createAsEmpty(),
$reporter,
BinarySampler::createAsAlwaysSample(),
false,
new CurrentTraceContext,
false
);
list($tracer, $flusher) = self::createDefaultTestTracer();

$result = $tracer->inSpan(
$sumCallable,
Expand All @@ -345,8 +337,7 @@ function (SpanCustomizer $span, $output = null, ?Throwable $e = null) {
);

$this->assertEquals(3, $result);
$tracer->flush();
$spans = $reporter->flush();
$spans = $flusher();
$this->assertCount(1, $spans);

$span = $spans[0]->toArray();
Expand All @@ -361,23 +352,14 @@ function (SpanCustomizer $span, $output = null, ?Throwable $e = null) {
*/
public function testInSpanNamesAreSuccessfullyGenerated($sumCallable, $expectedName)
{
$reporter = new InMemory();
$tracer = new Tracer(
Endpoint::createAsEmpty(),
$reporter,
BinarySampler::createAsAlwaysSample(),
false,
new CurrentTraceContext,
false
);
list($tracer, $flusher) = self::createDefaultTestTracer();

$tracer->inSpan(
$sumCallable,
[1, 2]
);

$tracer->flush();
$spans = $reporter->flush();
$spans = $flusher();

$span = $spans[0]->toArray();
$this->assertEquals($expectedName, $span['name']);
Expand Down Expand Up @@ -416,15 +398,7 @@ function (int $a, int $b) {

public function testInSpanForFailingCall()
{
$reporter = new InMemory();
$tracer = new Tracer(
Endpoint::createAsEmpty(),
$reporter,
BinarySampler::createAsAlwaysSample(),
false,
new CurrentTraceContext,
false
);
list($tracer, $flusher) = self::createDefaultTestTracer();

$sum = new class() {
public function __invoke(int $a, int $b)
Expand All @@ -439,11 +413,47 @@ public function __invoke(int $a, int $b)
} catch (OutOfBoundsException $e) {
}

$tracer->flush();
$spans = $reporter->flush();
$spans = $flusher();
$this->assertCount(1, $spans);

$span = $spans[0]->toArray();
$this->assertEquals('too small values', $span['tags']['error']);
}

public function testJoinSpans()
{
$context = TraceContext::createAsRoot(DefaultSamplingFlags::createAsSampled());

list($tracer, $flusher) = self::createDefaultTestTracer();

$span = $tracer->joinSpan($context);
$span->start();
$span->finish();

$spans = $flusher();

$span = $spans[0]->toArray();
$this->assertTrue($span['shared']);
}

private static function createDefaultTestTracer(): array
{
$reporter = new InMemory();
$tracer = new Tracer(
Endpoint::createAsEmpty(),
$reporter,
BinarySampler::createAsAlwaysSample(),
false,
new CurrentTraceContext,
false
);

return [
$tracer,
function () use ($tracer, $reporter): array {
$tracer->flush();
return $reporter->flush();
},
];
}
}

0 comments on commit 4b4c86f

Please sign in to comment.