diff options
Diffstat (limited to 'vendor/opentracing/opentracing/src/OpenTracing/Mock')
5 files changed, 508 insertions, 0 deletions
diff --git a/vendor/opentracing/opentracing/src/OpenTracing/Mock/MockScope.php b/vendor/opentracing/opentracing/src/OpenTracing/Mock/MockScope.php new file mode 100644 index 000000000..a814b45b5 --- /dev/null +++ b/vendor/opentracing/opentracing/src/OpenTracing/Mock/MockScope.php @@ -0,0 +1,62 @@ +<?php + +declare(strict_types=1); + +namespace OpenTracing\Mock; + +use OpenTracing\Scope; +use OpenTracing\Span; + +final class MockScope implements Scope +{ + /** + * @var Span + */ + private $span; + + /** + * @var MockScopeManager + */ + private $scopeManager; + + /** + * @var bool + */ + private $finishSpanOnClose; + + /** + * @param MockScopeManager $scopeManager + * @param Span $span + * @param bool $finishSpanOnClose + */ + public function __construct( + MockScopeManager $scopeManager, + Span $span, + bool $finishSpanOnClose + ) { + $this->scopeManager = $scopeManager; + $this->span = $span; + $this->finishSpanOnClose = $finishSpanOnClose; + } + + /** + * {@inheritdoc} + */ + public function close(): void + { + if ($this->finishSpanOnClose) { + $this->span->finish(); + } + + $this->scopeManager->deactivate($this); + } + + /** + * {@inheritdoc} + * @return Span|MockSpan + */ + public function getSpan(): Span + { + return $this->span; + } +} diff --git a/vendor/opentracing/opentracing/src/OpenTracing/Mock/MockScopeManager.php b/vendor/opentracing/opentracing/src/OpenTracing/Mock/MockScopeManager.php new file mode 100644 index 000000000..c75c71942 --- /dev/null +++ b/vendor/opentracing/opentracing/src/OpenTracing/Mock/MockScopeManager.php @@ -0,0 +1,49 @@ +<?php + +declare(strict_types=1); + +namespace OpenTracing\Mock; + +use OpenTracing\Scope; +use OpenTracing\ScopeManager; +use OpenTracing\Span; + +final class MockScopeManager implements ScopeManager +{ + /** + * @var Scope[] + */ + private $scopes = []; + + /** + * {@inheritdoc} + */ + public function activate(Span $span, bool $finishSpanOnClose = ScopeManager::DEFAULT_FINISH_SPAN_ON_CLOSE): Scope + { + $scope = new MockScope($this, $span, $finishSpanOnClose); + $this->scopes[] = $scope; + + return $scope; + } + + /** + * {@inheritdoc} + */ + public function getActive(): ?Scope + { + if (empty($this->scopes)) { + return null; + } + + return $this->scopes[count($this->scopes) - 1]; + } + + public function deactivate(MockScope $scope): void + { + foreach ($this->scopes as $scopeIndex => $scopeItem) { + if ($scope === $scopeItem) { + unset($this->scopes[$scopeIndex]); + } + } + } +} diff --git a/vendor/opentracing/opentracing/src/OpenTracing/Mock/MockSpan.php b/vendor/opentracing/opentracing/src/OpenTracing/Mock/MockSpan.php new file mode 100644 index 000000000..db53649bf --- /dev/null +++ b/vendor/opentracing/opentracing/src/OpenTracing/Mock/MockSpan.php @@ -0,0 +1,145 @@ +<?php + +declare(strict_types=1); + +namespace OpenTracing\Mock; + +use OpenTracing\Span; +use OpenTracing\SpanContext; + +final class MockSpan implements Span +{ + /** + * @var string + */ + private $operationName; + + /** + * @var SpanContext + */ + private $context; + + /** + * @var array + */ + private $tags = []; + + /** + * @var array + */ + private $logs = []; + + /** + * @var int + */ + private $startTime; + + /** + * @var int|null + */ + private $duration; + + public function __construct( + string $operationName, + SpanContext $context, + ?int $startTime = null + ) { + $this->operationName = $operationName; + $this->context = $context; + $this->startTime = $startTime ?: time(); + } + + /** + * {@inheritdoc} + */ + public function getOperationName(): string + { + return $this->operationName; + } + + /** + * {@inheritdoc} + * @return SpanContext|MockSpanContext + */ + public function getContext(): SpanContext + { + return $this->context; + } + + public function getStartTime(): ?int + { + return $this->startTime; + } + + /** + * {@inheritdoc} + */ + public function finish($finishTime = null): void + { + $finishTime = ($finishTime ?: time()); + $this->duration = $finishTime - $this->startTime; + } + + public function isFinished(): bool + { + return $this->duration !== null; + } + + public function getDuration(): ?int + { + return $this->duration; + } + + /** + * {@inheritdoc} + */ + public function overwriteOperationName(string $newOperationName): void + { + $this->operationName = (string)$newOperationName; + } + + /** + * {@inheritdoc} + */ + public function setTag(string $key, $value): void + { + $this->tags[$key] = $value; + } + + public function getTags(): array + { + return $this->tags; + } + + /** + * {@inheritdoc} + */ + public function log(array $fields = [], $timestamp = null): void + { + $this->logs[] = [ + 'timestamp' => $timestamp ?: time(), + 'fields' => $fields, + ]; + } + + public function getLogs(): array + { + return $this->logs; + } + + /** + * {@inheritdoc} + */ + public function addBaggageItem(string $key, string $value): void + { + $this->context = $this->context->withBaggageItem($key, $value); + } + + /** + * {@inheritdoc} + */ + public function getBaggageItem(string $key): ?string + { + return $this->context->getBaggageItem($key); + } +} diff --git a/vendor/opentracing/opentracing/src/OpenTracing/Mock/MockSpanContext.php b/vendor/opentracing/opentracing/src/OpenTracing/Mock/MockSpanContext.php new file mode 100644 index 000000000..d094ea0e0 --- /dev/null +++ b/vendor/opentracing/opentracing/src/OpenTracing/Mock/MockSpanContext.php @@ -0,0 +1,100 @@ +<?php + +declare(strict_types=1); + +namespace OpenTracing\Mock; + +use OpenTracing\SpanContext; +use ArrayIterator; + +final class MockSpanContext implements SpanContext +{ + /** + * @var int + */ + private $traceId; + + /** + * @var int + */ + private $spanId; + + /** + * @var bool + */ + private $isSampled; + + /** + * @var array + */ + private $items; + + private function __construct(int $traceId, int $spanId, bool $isSampled, array $items) + { + $this->traceId = $traceId; + $this->spanId = $spanId; + $this->isSampled = $isSampled; + $this->items = $items; + } + + public static function create(int $traceId, int $spanId, bool $sampled = true, array $items = []): SpanContext + { + return new self($traceId, $spanId, $sampled, $items); + } + + public static function createAsRoot(bool $sampled = true, array $items = []): SpanContext + { + $traceId = $spanId = self::nextId(); + return new self($traceId, $spanId, $sampled, $items); + } + + public static function createAsChildOf(MockSpanContext $spanContext): SpanContext + { + $spanId = self::nextId(); + return new self($spanContext->traceId, $spanId, $spanContext->isSampled, $spanContext->items); + } + + public function getTraceId(): int + { + return $this->traceId; + } + + public function getSpanId(): int + { + return $this->spanId; + } + + public function isSampled(): bool + { + return $this->isSampled; + } + + /** + * {@inheritdoc} + */ + public function getIterator(): ArrayIterator + { + return new ArrayIterator($this->items); + } + + /** + * {@inheritdoc} + */ + public function getBaggageItem(string $key): ?string + { + return array_key_exists($key, $this->items) ? $this->items[$key] : null; + } + + /** + * {@inheritdoc} + */ + public function withBaggageItem(string $key, string $value): SpanContext + { + return new self($this->traceId, $this->spanId, $this->isSampled, array_merge($this->items, [$key => $value])); + } + + private static function nextId(): int + { + return mt_rand(0, 99999); + } +} diff --git a/vendor/opentracing/opentracing/src/OpenTracing/Mock/MockTracer.php b/vendor/opentracing/opentracing/src/OpenTracing/Mock/MockTracer.php new file mode 100644 index 000000000..f4f4577a1 --- /dev/null +++ b/vendor/opentracing/opentracing/src/OpenTracing/Mock/MockTracer.php @@ -0,0 +1,152 @@ +<?php + +declare(strict_types=1); + +namespace OpenTracing\Mock; + +use OpenTracing\InvalidReferenceArgumentException; +use OpenTracing\UnsupportedFormatException; +use OpenTracing\Scope; +use OpenTracing\ScopeManager; +use OpenTracing\Span; +use OpenTracing\SpanContext; +use OpenTracing\StartSpanOptions; +use OpenTracing\Tracer; + +final class MockTracer implements Tracer +{ + /** + * @var array|MockSpan[] + */ + private $spans = []; + + /** + * @var array|callable[] + */ + private $injectors; + + /** + * @var array|callable[] + */ + private $extractors; + + /** + * @var ScopeManager + */ + private $scopeManager; + + public function __construct(array $injectors = [], array $extractors = []) + { + $this->injectors = $injectors; + $this->extractors = $extractors; + $this->scopeManager = new MockScopeManager(); + } + + /** + * {@inheritdoc} + */ + public function startActiveSpan(string $operationName, $options = []): Scope + { + if (!($options instanceof StartSpanOptions)) { + $options = StartSpanOptions::create($options); + } + + if (($activeSpan = $this->getActiveSpan()) !== null) { + $options = $options->withParent($activeSpan); + } + + $span = $this->startSpan($operationName, $options); + + return $this->scopeManager->activate($span, $options->shouldFinishSpanOnClose()); + } + + /** + * {@inheritdoc} + */ + public function startSpan(string $operationName, $options = []): Span + { + if (!($options instanceof StartSpanOptions)) { + $options = StartSpanOptions::create($options); + } + + if (empty($options->getReferences())) { + $spanContext = MockSpanContext::createAsRoot(); + } else { + $referenceContext = $options->getReferences()[0]->getSpanContext(); + if (!$referenceContext instanceof MockSpanContext) { + throw InvalidReferenceArgumentException::forInvalidContext($referenceContext); + } + $spanContext = MockSpanContext::createAsChildOf($referenceContext); + } + + $span = new MockSpan($operationName, $spanContext, $options->getStartTime()); + + foreach ($options->getTags() as $key => $value) { + $span->setTag($key, $value); + } + + $this->spans[] = $span; + + return $span; + } + + /** + * {@inheritdoc} + */ + public function inject(SpanContext $spanContext, string $format, &$carrier): void + { + if (!array_key_exists($format, $this->injectors)) { + throw UnsupportedFormatException::forFormat($format); + } + + $this->injectors[$format]($spanContext, $carrier); + } + + /** + * {@inheritdoc} + */ + public function extract(string $format, $carrier): ?SpanContext + { + if (!array_key_exists($format, $this->extractors)) { + throw UnsupportedFormatException::forFormat($format); + } + + return $this->extractors[$format]($carrier); + } + + /** + * {@inheritdoc} + */ + public function flush(): void + { + $this->spans = []; + } + + /** + * @return array|MockSpan[] + */ + public function getSpans(): array + { + return $this->spans; + } + + /** + * {@inheritdoc} + */ + public function getScopeManager(): ScopeManager + { + return $this->scopeManager; + } + + /** + * {@inheritdoc} + */ + public function getActiveSpan(): ?Span + { + if (null !== ($activeScope = $this->scopeManager->getActive())) { + return $activeScope->getSpan(); + } + + return null; + } +} |