-
Notifications
You must be signed in to change notification settings - Fork 59
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
Allow using custom Recorder and add option to always record spans #158
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,20 +127,27 @@ final class Span | |
*/ | ||
private $localEndpoint; | ||
|
||
/** | ||
* @var bool|null | ||
*/ | ||
private $isSampled; | ||
|
||
private function __construct( | ||
string $traceId, | ||
?string $parentId, | ||
string $spanId, | ||
bool $debug, | ||
bool $shared, | ||
Endpoint $localEndpoint | ||
Endpoint $localEndpoint, | ||
?bool $isSampled = false | ||
) { | ||
$this->traceId = $traceId; | ||
$this->parentId = $parentId; | ||
$this->spanId = $spanId; | ||
$this->debug = $debug; | ||
$this->shared = $shared; | ||
$this->localEndpoint = $localEndpoint; | ||
$this->isSampled = $isSampled; | ||
} | ||
|
||
/** | ||
|
@@ -156,7 +163,8 @@ public static function createFromContext(TraceContext $context, Endpoint $localE | |
$context->getSpanId(), | ||
$context->isDebug(), | ||
$context->isShared(), | ||
$localEndpoint | ||
$localEndpoint, | ||
$context->isSampled() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ps one slight thing here is that in brave the "handler" has 2 arguments. since it has the context as one of the args, we don't need to also copy isSampled to the span.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not saying to change this, just mentioning a reference. it is fine to also copy it to the span, just it should be immutable (appears to be the case!) |
||
); | ||
} | ||
|
||
|
@@ -225,6 +233,14 @@ public function setRemoteEndpoint(Endpoint $remoteEndpoint): void | |
$this->remoteEndpoint = $remoteEndpoint; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isSampled(): bool | ||
{ | ||
return $this->isSampled === true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got a question here @adriancole @drolando: what we are saying here is whether this is sampled or not (and then dispatch the span to different reporters accordingly) but what about debug? if something is sampled=false but debug=true then we will dispatch it to the local sampled reporter, isn't? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably it is better we change this into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Giving a second thought, I think it is responsability of the user to decide what to do when sampled=false and debug=true. |
||
} | ||
|
||
/** | ||
* Completes and reports the span | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
use Zipkin\Tracing; | ||
use Zipkin\Endpoint; | ||
use Zipkin\Reporters\Noop; | ||
use Zipkin\Reporters\InMemory; | ||
use Zipkin\TracingBuilder; | ||
use Zipkin\Propagation\Getter; | ||
use Zipkin\Propagation\Setter; | ||
|
@@ -85,4 +86,58 @@ private function randomBool() | |
{ | ||
return (bool) mt_rand(0, 1); | ||
} | ||
|
||
public function testAlwaysEmitSpans() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will also change this test into |
||
{ | ||
// If `alwaysReportingSpans(true)` is called, we should be emitting the | ||
// spans even if the trace isn't sampled | ||
$endpoint = Endpoint::createAsEmpty(); | ||
$reporter = new InMemory(); | ||
$sampler = BinarySampler::createAsNeverSample(); | ||
|
||
$tracing = TracingBuilder::create() | ||
->havingLocalServiceName(self::SERVICE_NAME) | ||
->havingLocalEndpoint($endpoint) | ||
->havingReporter($reporter) | ||
->havingSampler($sampler) | ||
->alwaysReportingSpans(true) | ||
->build(); | ||
$tracer = $tracing->getTracer(); | ||
|
||
$span = $tracer->newTrace(); | ||
$span->setName('test'); | ||
$span->start(); | ||
$span->finish(); | ||
|
||
$tracer->flush(); | ||
$spans = $reporter->flush(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't fully understand why we are using another word. Ex alwaysReportSpans would make sense to have reporter report. alwaysEmitSpans would makes sense to have emitter emit :) here's a related post on naming the TL;DR; being that we make enough words at this point https://publicobject.com/2020/06/06/synonyms-are-bad/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oh yeah that's a better name |
||
$this->assertCount(1, $spans); | ||
$this->assertFalse($spans[0]->isSampled()); | ||
} | ||
|
||
public function testDontEmitByDefault() | ||
{ | ||
// By default, let's verify that we don't emit any span if the | ||
// trace isn't sampled. | ||
$endpoint = Endpoint::createAsEmpty(); | ||
$reporter = new InMemory(); | ||
$sampler = BinarySampler::createAsNeverSample(); | ||
|
||
$tracing = TracingBuilder::create() | ||
->havingLocalServiceName(self::SERVICE_NAME) | ||
->havingLocalEndpoint($endpoint) | ||
->havingReporter($reporter) | ||
->havingSampler($sampler) | ||
->build(); | ||
$tracer = $tracing->getTracer(); | ||
|
||
$span = $tracer->newTrace(); | ||
$span->setName('test'); | ||
$span->start(); | ||
$span->finish(); | ||
|
||
$tracer->flush(); | ||
$spans = $reporter->flush(); | ||
$this->assertCount(0, $spans); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will also move this before endpoint as we changed the order already.