Skip to content
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

Add root span/scope tracking #241

Merged
merged 6 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ All notable changes to this project will be documented in this file - [read more

### Added
- Request init hook configuration allowing running arbitrary code before actual request execution #175
- `Tracer::startRootSpan()` to track the root `Scope` instance which can be accessed with `Tracer::getRootScope()` #241

### Changed

Expand Down
15 changes: 15 additions & 0 deletions src/DDTrace/Contracts/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,19 @@ public function flush();
* @return mixed
*/
public function getPrioritySampling();

/**
* This behaves just like Tracer::startActiveSpan(), but it saves the Scope instance
* on the tracer to be accessed later by Tracer::getRootScope().
*
* @param string $operationName
* @param array $options
* @return Scope
*/
public function startRootSpan($operationName, $options = []);

/**
* @return Scope|null
*/
public function getRootScope();
}
17 changes: 16 additions & 1 deletion src/DDTrace/NoopTracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ public function startSpan($operationName, $options = [])
*/
public function startActiveSpan($operationName, $finishSpanOnClose = true, $options = [])
{

return NoopScope::create();
}

Expand Down Expand Up @@ -79,4 +78,20 @@ public function getPrioritySampling()
{
return null;
}

/**
* {@inheritdoc}
*/
public function startRootSpan($operationName, $options = [])
{
return NoopScope::create();
}

/**
* {@inheritdoc}
*/
public function getRootScope()
{
return NoopScope::create();
}
}
22 changes: 22 additions & 0 deletions src/DDTrace/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use DDTrace\Transport\Http;
use DDTrace\Transport\Noop as NoopTransport;
use DDTrace\Exceptions\UnsupportedFormat;
use DDTrace\Contracts\Scope as ScopeInterface;
use DDTrace\Contracts\SpanContext as SpanContextInterface;
use DDTrace\Contracts\Tracer as TracerInterface;

Expand Down Expand Up @@ -63,6 +64,11 @@ final class Tracer implements TracerInterface
*/
private $scopeManager;

/**
* @var ScopeInterface|null
*/
private $rootScope;

/**
* @var Configuration
*/
Expand Down Expand Up @@ -151,6 +157,22 @@ public function startSpan($operationName, $options = [])
return $span;
}

/**
* {@inheritdoc}
*/
public function startRootSpan($operationName, $options = [])
{
return $this->rootScope = $this->startActiveSpan($operationName, $options);
}

/**
* {@inheritdoc}
*/
public function getRootScope()
{
return $this->rootScope;
}

/**
* {@inheritdoc}
*/
Expand Down
13 changes: 13 additions & 0 deletions tests/Unit/TracerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,17 @@ public function testUnfinishedSpansCanBeFinishedOnFlush()
$this->assertSame('root', $sent[0][0]->getOperationName());
$this->assertSame('child', $sent[0][1]->getOperationName());
}

public function testSpanStartedAtRootCanBeAccessedLater()
{
$tracer = new Tracer(new NoopTransport());
$scope = $tracer->startRootSpan(self::OPERATION_NAME);
$this->assertSame($scope, $tracer->getRootScope());
}

public function testIfNoRootScopeExistsItWillBeNull()
{
$tracer = new Tracer(new NoopTransport());
$this->assertNull($tracer->getRootScope());
}
}