|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace OpenTracing\Mock; |
| 4 | + |
| 5 | +use OpenTracing\ScopeManager; |
| 6 | +use OpenTracing\Span; |
| 7 | +use OpenTracing\SpanContext; |
| 8 | + |
| 9 | +final class MockSpan implements Span |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @var string |
| 13 | + */ |
| 14 | + private $operationName; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var MockSpanContext |
| 18 | + */ |
| 19 | + private $context; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var array |
| 23 | + */ |
| 24 | + private $tags = []; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var array |
| 28 | + */ |
| 29 | + private $logs = []; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var int |
| 33 | + */ |
| 34 | + private $startTime; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var int|null |
| 38 | + */ |
| 39 | + private $duration; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var bool |
| 43 | + */ |
| 44 | + private $closeOnFinish; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var ScopeManager |
| 48 | + */ |
| 49 | + private $scopeManager; |
| 50 | + |
| 51 | + public function __construct( |
| 52 | + ScopeManager $scopeManager, |
| 53 | + $operationName, |
| 54 | + MockSpanContext $context, |
| 55 | + $startTime = null, |
| 56 | + $closeOnFinish = false |
| 57 | + ) { |
| 58 | + $this->scopeManager = $scopeManager; |
| 59 | + $this->operationName = $operationName; |
| 60 | + $this->context = $context; |
| 61 | + $this->startTime = $startTime ?: time(); |
| 62 | + $this->closeOnFinish = $closeOnFinish; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * {@inheritdoc} |
| 67 | + */ |
| 68 | + public function getOperationName() |
| 69 | + { |
| 70 | + return $this->operationName; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * {@inheritdoc} |
| 75 | + * @return SpanContext|MockSpanContext |
| 76 | + */ |
| 77 | + public function getContext() |
| 78 | + { |
| 79 | + return $this->context; |
| 80 | + } |
| 81 | + |
| 82 | + public function getStartTime() |
| 83 | + { |
| 84 | + return $this->startTime; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * {@inheritdoc} |
| 89 | + */ |
| 90 | + public function finish($finishTime = null) |
| 91 | + { |
| 92 | + $finishTime = ($finishTime ?: time()); |
| 93 | + $this->duration = $finishTime - $this->startTime; |
| 94 | + |
| 95 | + if (!$this->closeOnFinish) { |
| 96 | + return; |
| 97 | + } |
| 98 | + |
| 99 | + if (($scope = $this->scopeManager->getScope($this)) !== null) { |
| 100 | + $scope->close(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + public function isFinished() |
| 105 | + { |
| 106 | + return $this->duration !== null; |
| 107 | + } |
| 108 | + |
| 109 | + public function getDuration() |
| 110 | + { |
| 111 | + return $this->duration; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * {@inheritdoc} |
| 116 | + */ |
| 117 | + public function overwriteOperationName($newOperationName) |
| 118 | + { |
| 119 | + $this->operationName = (string) $newOperationName; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * {@inheritdoc} |
| 124 | + */ |
| 125 | + public function setTag($key, $value) |
| 126 | + { |
| 127 | + $this->tags[$key] = $value; |
| 128 | + } |
| 129 | + |
| 130 | + public function getTags() |
| 131 | + { |
| 132 | + return $this->tags; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * {@inheritdoc} |
| 137 | + */ |
| 138 | + public function log(array $fields = [], $timestamp = null) |
| 139 | + { |
| 140 | + $this->logs[] = [ |
| 141 | + 'timestamp' => $timestamp ?: time(), |
| 142 | + 'fields' => $fields, |
| 143 | + ]; |
| 144 | + } |
| 145 | + |
| 146 | + public function getLogs() |
| 147 | + { |
| 148 | + return $this->logs; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * {@inheritdoc} |
| 153 | + */ |
| 154 | + public function addBaggageItem($key, $value) |
| 155 | + { |
| 156 | + $this->context = $this->context->withBaggageItem($key, $value); |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * {@inheritdoc} |
| 161 | + */ |
| 162 | + public function getBaggageItem($key) |
| 163 | + { |
| 164 | + return $this->context->getBaggageItem($key); |
| 165 | + } |
| 166 | +} |
0 commit comments