-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from opentracing/21_adds_mock_tracer
[#21] Adds mock tracer.
- Loading branch information
Showing
18 changed files
with
877 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace OpenTracing\Mock; | ||
|
||
use OpenTracing\Scope; | ||
use OpenTracing\Span; | ||
|
||
final class MockScope implements Scope | ||
{ | ||
/** | ||
* @var Span | ||
*/ | ||
private $span; | ||
|
||
/** | ||
* @var MockScopeManager | ||
*/ | ||
private $scopeManager; | ||
|
||
public function __construct(MockScopeManager $scopeManager, Span $span) | ||
{ | ||
$this->scopeManager = $scopeManager; | ||
$this->span = $span; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function close() | ||
{ | ||
$this->scopeManager->deactivate($this); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* @return Span|MockSpan | ||
*/ | ||
public function getSpan() | ||
{ | ||
return $this->span; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace OpenTracing\Mock; | ||
|
||
use OpenTracing\Scope; | ||
use OpenTracing\ScopeManager; | ||
use OpenTracing\Span; | ||
|
||
final class MockScopeManager implements ScopeManager | ||
{ | ||
/** | ||
* @var array|Scope[] | ||
*/ | ||
private $scopes = []; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function activate(Span $span) | ||
{ | ||
$this->scopes[] = new MockScope($this, $span); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getActive() | ||
{ | ||
if (empty($this->scopes)) { | ||
return null; | ||
} | ||
|
||
return $this->scopes[count($this->scopes) - 1]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* @param Span|MockSpan $span | ||
*/ | ||
public function getScope(Span $span) | ||
{ | ||
$scopeLength = count($this->scopes); | ||
|
||
for ($i = 0; $i < $scopeLength; $i++) { | ||
if ($span === $this->scopes[$i]->getSpan()) { | ||
return $this->scopes[$i]; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public function deactivate(MockScope $scope) | ||
{ | ||
$scopeLength = count($this->scopes); | ||
|
||
for ($i = 0; $i < $scopeLength; $i++) { | ||
if ($scope === $this->scopes[$i]) { | ||
unset($this->scopes[$i]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
<?php | ||
|
||
namespace OpenTracing\Mock; | ||
|
||
use OpenTracing\ScopeManager; | ||
use OpenTracing\Span; | ||
use OpenTracing\SpanContext; | ||
|
||
final class MockSpan implements Span | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $operationName; | ||
|
||
/** | ||
* @var MockSpanContext | ||
*/ | ||
private $context; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $tags = []; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $logs = []; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $startTime; | ||
|
||
/** | ||
* @var int|null | ||
*/ | ||
private $duration; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $closeOnFinish; | ||
|
||
/** | ||
* @var ScopeManager | ||
*/ | ||
private $scopeManager; | ||
|
||
public function __construct( | ||
ScopeManager $scopeManager, | ||
$operationName, | ||
MockSpanContext $context, | ||
$startTime = null, | ||
$closeOnFinish = false | ||
) { | ||
$this->scopeManager = $scopeManager; | ||
$this->operationName = $operationName; | ||
$this->context = $context; | ||
$this->startTime = $startTime ?: time(); | ||
$this->closeOnFinish = $closeOnFinish; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getOperationName() | ||
{ | ||
return $this->operationName; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* @return SpanContext|MockSpanContext | ||
*/ | ||
public function getContext() | ||
{ | ||
return $this->context; | ||
} | ||
|
||
public function getStartTime() | ||
{ | ||
return $this->startTime; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function finish($finishTime = null) | ||
{ | ||
$finishTime = ($finishTime ?: time()); | ||
$this->duration = $finishTime - $this->startTime; | ||
|
||
if (!$this->closeOnFinish) { | ||
return; | ||
} | ||
|
||
if (($scope = $this->scopeManager->getScope($this)) !== null) { | ||
$scope->close(); | ||
} | ||
} | ||
|
||
public function isFinished() | ||
{ | ||
return $this->duration !== null; | ||
} | ||
|
||
public function getDuration() | ||
{ | ||
return $this->duration; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function overwriteOperationName($newOperationName) | ||
{ | ||
$this->operationName = (string) $newOperationName; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setTag($key, $value) | ||
{ | ||
$this->tags[$key] = $value; | ||
} | ||
|
||
public function getTags() | ||
{ | ||
return $this->tags; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function log(array $fields = [], $timestamp = null) | ||
{ | ||
$this->logs[] = [ | ||
'timestamp' => $timestamp ?: time(), | ||
'fields' => $fields, | ||
]; | ||
} | ||
|
||
public function getLogs() | ||
{ | ||
return $this->logs; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function addBaggageItem($key, $value) | ||
{ | ||
$this->context = $this->context->withBaggageItem($key, $value); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getBaggageItem($key) | ||
{ | ||
return $this->context->getBaggageItem($key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php | ||
|
||
namespace OpenTracing\Mock; | ||
|
||
use ArrayIterator; | ||
use OpenTracing\SpanContext; | ||
|
||
final class MockSpanContext implements SpanContext | ||
{ | ||
/** | ||
* @var int | ||
*/ | ||
private $traceId; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $spanId; | ||
|
||
/** | ||
* @var bool | ||
*/ | ||
private $isSampled; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $items; | ||
|
||
private function __construct($traceId, $spanId, $isSampled, array $items) | ||
{ | ||
$this->traceId = $traceId; | ||
$this->spanId = $spanId; | ||
$this->isSampled = $isSampled; | ||
$this->items = $items; | ||
} | ||
|
||
public static function create($traceId, $spanId, $sampled = true, array $items = []) | ||
{ | ||
return new self($traceId, $spanId, $sampled, $items); | ||
} | ||
|
||
public static function createAsRoot($sampled = true, array $items = []) | ||
{ | ||
$traceId = $spanId = self::nextId(); | ||
return new self($traceId, $spanId, $sampled, $items); | ||
} | ||
|
||
public static function createAsChildOf(MockSpanContext $spanContext) | ||
{ | ||
$spanId = self::nextId(); | ||
return new self($spanContext->traceId, $spanId, $spanContext->isSampled, $spanContext->items); | ||
} | ||
|
||
public function getTraceId() | ||
{ | ||
return $this->traceId; | ||
} | ||
|
||
public function getSpanId() | ||
{ | ||
return $this->spanId; | ||
} | ||
|
||
public function isSampled() | ||
{ | ||
return $this->isSampled; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getIterator() | ||
{ | ||
return new ArrayIterator($this->items); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getBaggageItem($key) | ||
{ | ||
return array_key_exists($key, $this->items) ? $this->items[$key] : null; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function withBaggageItem($key, $value) | ||
{ | ||
return new self($this->traceId, $this->spanId, $this->isSampled, array_merge($this->items, [$key => $value])); | ||
} | ||
|
||
private static function nextId() | ||
{ | ||
return mt_rand(0, 99999); | ||
} | ||
} |
Oops, something went wrong.