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

Adds support for closeSpanOnFinish. #12

Closed
wants to merge 1 commit into from
Closed
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
23 changes: 21 additions & 2 deletions src/DDTrace/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use InvalidArgumentException;
use OpenTracing\SpanContext as OpenTracingContext;
use Throwable;
use OpenTracing\ScopeManager as OTScopeManager;
use OpenTracing\Span as OpenTracingSpan;

final class Span implements OpenTracingSpan
Expand Down Expand Up @@ -77,27 +78,41 @@ final class Span implements OpenTracingSpan
*/
private $hasError = false;

/**
* @var bool
*/
private $closeSpanOnFinish = false;

/**
* @var ScopeManager
*/
private $scopeManager;

/**
* Span constructor.
* @param OTScopeManager $scopeManager
* @param string $operationName
* @param SpanContext $context
* @param string $service
* @param string $resource
* @param int|null $startTime
* @param bool $closeSpanOnFinish
*/
public function __construct(
OTScopeManager $scopeManager,
$operationName,
SpanContext $context,
$service,
$resource,
$startTime = null
$startTime = null,
$closeSpanOnFinish = false
) {
$this->scopeManager = $scopeManager;
$this->context = $context;
$this->operationName = (string) $operationName;
$this->service = (string) $service;
$this->resource = (string) $resource;
$this->startTime = $startTime ?: Time\now();
$this->closeSpanOnFinish = $closeSpanOnFinish;
}

/**
Expand Down Expand Up @@ -269,6 +284,10 @@ public function finish($finishTime = null)
}

$this->duration = ($finishTime ?: Time\now()) - $this->startTime;

if ($this->closeSpanOnFinish && null !== ($scope = $this->scopeManager->getScope($this))) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this logic is not tested.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right.

$scope->close();
}
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/DDTrace/Tracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,13 @@ public function startSpan($operationName, $options = [])
}

$span = new Span(
$this->scopeManager,
$operationName,
$context,
$this->config['service_name'],
array_key_exists('resource', $this->config) ? $this->config['resource'] : $operationName,
$options->getStartTime()
$options->getStartTime(),
$options->getCloseSpanOnFinish()
);

$tags = $options->getTags() + $this->config['global_tags'];
Expand Down
3 changes: 3 additions & 0 deletions tests/Unit/Encoders/JsonTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace DDTrace\Tests\Unit\Encoders;

use DDTrace\Encoders\Json;
use DDTrace\ScopeManager;
use DDTrace\Span;
use DDTrace\SpanContext;
use OpenTracing\NoopScopeManager;
use PHPUnit_Framework_TestCase;

final class JsonTest extends PHPUnit_Framework_TestCase
Expand All @@ -20,6 +22,7 @@ public function testEncodeTracesSuccess()

$context = new SpanContext('160e7072ff7bd5f1', '160e7072ff7bd5f2');
$span = new Span(
new NoopScopeManager(),
'test_name',
$context,
'test_service',
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/SpanTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use DDTrace\Tags;
use DDTrace\Span;
use Exception;
use OpenTracing\NoopScopeManager;
use PHPUnit_Framework_TestCase;

final class SpanTest extends PHPUnit_Framework_TestCase
Expand Down Expand Up @@ -101,6 +102,7 @@ private function createSpan()
$context = SpanContext::createAsRoot();

$span = new Span(
new NoopScopeManager(),
self::OPERATION_NAME,
$context,
self::SERVICE,
Expand Down