Skip to content

Commit

Permalink
Merge pull request #67 from person-unko/fix-ignore-active-span
Browse files Browse the repository at this point in the history
Added check for ignore_active_span option when starting a span
  • Loading branch information
jcchavezs authored Nov 3, 2023
2 parents bb8f1c5 + b8fe5c6 commit 6bd908f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
8 changes: 6 additions & 2 deletions src/ZipkinOpenTracing/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ public function startActiveSpan(string $operationName, $options = []): OTScope
$options = StartSpanOptions::create($options);
}

if (!$this->hasParentInOptions($options) && $this->getActiveSpan() !== null) {
if (!$options->shouldIgnoreActiveSpan()
&& !$this->hasParentInOptions($options)
&& $this->getActiveSpan() !== null) {
$parent = $this->getActiveSpan()->getContext();
$options = $options->withParent($parent);
}
Expand All @@ -106,7 +108,9 @@ public function startSpan(string $operationName, $options = []): OTSpan
$options = StartSpanOptions::create($options);
}

if (!$this->hasParentInOptions($options) && $this->getActiveSpan() !== null) {
if (!$options->shouldIgnoreActiveSpan()
&& !$this->hasParentInOptions($options)
&& $this->getActiveSpan() !== null) {
$parent = $this->getActiveSpan()->getContext();
$options = $options->withParent($parent);
}
Expand Down
34 changes: 17 additions & 17 deletions tests/Unit/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ public function __construct(array $headers = [])
/**
* @return string[][]
*/
public function getHeaders()
public function getHeaders(): array
{
return $this->headers;
}

/**
* @return bool
*/
public function hasHeader($name)
public function hasHeader($name): bool
{
foreach ($this->headers as $key => $value) {
if ($key === strtolower($name)) {
Expand All @@ -46,7 +46,7 @@ public function hasHeader($name)
/**
* @return string[]
*/
public function getHeader($name)
public function getHeader($name): array
{
foreach ($this->headers as $key => $value) {
if ($key === strtolower($name)) {
Expand All @@ -57,7 +57,7 @@ public function getHeader($name)
return [];
}

public function withHeader($name, $value)
public function withHeader($name, $value): \Psr\Http\Message\MessageInterface
{
$this->headers[strtolower($name)] = [$value];
return $this;
Expand All @@ -67,67 +67,67 @@ public function withHeader($name, $value)
// parsing of headers
//
// phpcs:disable
public function withAddedHeader($name, $value)
public function withAddedHeader($name, $value): \Psr\Http\Message\MessageInterface
{
throw new LogicException('not implemented');
}

public function getProtocolVersion()
public function getProtocolVersion(): string
{
throw new LogicException('not implemented');
}

public function withProtocolVersion($version)
public function withProtocolVersion($version): \Psr\Http\Message\MessageInterface
{
throw new LogicException('not implemented');
}

public function getHeaderLine($name)
public function getHeaderLine($name): string
{
throw new LogicException('not implemented');
}

public function withoutHeader($name)
public function withoutHeader($name): \Psr\Http\Message\MessageInterface
{
throw new LogicException('not implemented');
}

public function getBody()
public function getBody(): StreamInterface
{
throw new LogicException('not implemented');
}

public function withBody(StreamInterface $body)
public function withBody(StreamInterface $body): \Psr\Http\Message\MessageInterface
{
throw new LogicException('not implemented');
}

public function getRequestTarget()
public function getRequestTarget(): string
{
throw new LogicException('not implemented');
}

public function withRequestTarget($requestTarget)
public function withRequestTarget($requestTarget): RequestInterface
{
throw new LogicException('not implemented');
}

public function getMethod()
public function getMethod(): string
{
throw new LogicException('not implemented');
}

public function withMethod($method)
public function withMethod($method): RequestInterface
{
throw new LogicException('not implemented');
}

public function getUri()
public function getUri(): UriInterface
{
throw new LogicException('not implemented');
}

public function withUri(UriInterface $uri, $preserveHost = false)
public function withUri(UriInterface $uri, $preserveHost = false): RequestInterface
{
throw new LogicException('not implemented');
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Unit/TracerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,40 @@ public function testStartSpanWithNoParentSuccess()
$this->assertInstanceOf(Span::class, $span);
}

public function testStartSpanWithIgnoreActiveSpan()
{
$tracing = TracingBuilder::create()->build();
$tracer = new Tracer($tracing);
$firstSpan = $tracer->startActiveSpan(self::OPERATION_NAME);

$secondSpan = $tracer->startSpan(self::OPERATION_NAME, [
'ignore_active_span' => true,
]);

$firstContext = $firstSpan->getSpan()->getContext()->getContext();
$secondContext = $secondSpan->getContext()->getContext();

$this->assertNotEquals($firstContext->getTraceId(), $secondContext->getTraceId());
$this->assertTrue($secondContext->getParentId() === null);
}

public function testStartActiveSpanWithIgnoreActiveSpan()
{
$tracing = TracingBuilder::create()->build();
$tracer = new Tracer($tracing);
$firstSpan = $tracer->startActiveSpan(self::OPERATION_NAME);

$secondSpan = $tracer->startActiveSpan(self::OPERATION_NAME, [
'ignore_active_span' => true,
]);

$firstContext = $firstSpan->getSpan()->getContext()->getContext();
$secondContext = $secondSpan->getSpan()->getContext()->getContext();

$this->assertNotEquals($firstContext->getTraceId(), $secondContext->getTraceId());
$this->assertTrue($secondContext->getParentId() === null);
}

public function testStartSpanWithParentSuccess()
{
$tracing = TracingBuilder::create()->build();
Expand Down

0 comments on commit 6bd908f

Please sign in to comment.