| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\Sentry\Tracing; |
| 5 |
|
| 6 |
use WCPOS\Vendor\Sentry\SentrySdk; |
| 7 |
use WCPOS\Vendor\Sentry\State\Scope; |
| 8 |
use WCPOS\Vendor\Sentry\Tracing\Traits\TraceHeaderParserTrait; |
| 9 |
final class PropagationContext |
| 10 |
{ |
| 11 |
use TraceHeaderParserTrait; |
| 12 |
/** |
| 13 |
* @var TraceId The trace id |
| 14 |
*/ |
| 15 |
private $traceId; |
| 16 |
/** |
| 17 |
* @var SpanId The span id |
| 18 |
*/ |
| 19 |
private $spanId; |
| 20 |
/** |
| 21 |
* @var SpanId|null The parent span id |
| 22 |
*/ |
| 23 |
private $parentSpanId; |
| 24 |
/** |
| 25 |
* @var bool|null The parent's sampling decision |
| 26 |
*/ |
| 27 |
private $parentSampled; |
| 28 |
/** |
| 29 |
* @var float|null |
| 30 |
*/ |
| 31 |
private $sampleRand; |
| 32 |
/** |
| 33 |
* @var DynamicSamplingContext|null The dynamic sampling context |
| 34 |
*/ |
| 35 |
private $dynamicSamplingContext; |
| 36 |
private function __construct() |
| 37 |
{ |
| 38 |
} |
| 39 |
public static function fromDefaults() : self |
| 40 |
{ |
| 41 |
$context = new self(); |
| 42 |
$context->traceId = TraceId::generate(); |
| 43 |
$context->spanId = SpanId::generate(); |
| 44 |
$context->parentSpanId = null; |
| 45 |
$context->parentSampled = null; |
| 46 |
$context->sampleRand = \round(\mt_rand(0, \mt_getrandmax() - 1) / \mt_getrandmax(), 6); |
| 47 |
$context->dynamicSamplingContext = null; |
| 48 |
return $context; |
| 49 |
} |
| 50 |
public static function fromHeaders(string $sentryTraceHeader, string $baggageHeader) : self |
| 51 |
{ |
| 52 |
return self::parseTraceparentAndBaggage($sentryTraceHeader, $baggageHeader); |
| 53 |
} |
| 54 |
public static function fromEnvironment(string $sentryTrace, string $baggage) : self |
| 55 |
{ |
| 56 |
return self::parseTraceparentAndBaggage($sentryTrace, $baggage); |
| 57 |
} |
| 58 |
/** |
| 59 |
* Returns a string that can be used for the `sentry-trace` header & meta tag. |
| 60 |
*/ |
| 61 |
public function toTraceparent() : string |
| 62 |
{ |
| 63 |
return \sprintf('%s-%s', (string) $this->traceId, (string) $this->spanId); |
| 64 |
} |
| 65 |
/** |
| 66 |
* Returns a string that can be used for the W3C `traceparent` header & meta tag. |
| 67 |
* |
| 68 |
* @deprecated since version 4.12. To be removed in version 5.0. |
| 69 |
*/ |
| 70 |
public function toW3CTraceparent() : string |
| 71 |
{ |
| 72 |
return ''; |
| 73 |
} |
| 74 |
/** |
| 75 |
* Returns a string that can be used for the `baggage` header & meta tag. |
| 76 |
*/ |
| 77 |
public function toBaggage() : string |
| 78 |
{ |
| 79 |
if ($this->dynamicSamplingContext === null) { |
| 80 |
$hub = SentrySdk::getCurrentHub(); |
| 81 |
$client = $hub->getClient(); |
| 82 |
if ($client !== null) { |
| 83 |
$options = $client->getOptions(); |
| 84 |
if ($options !== null) { |
| 85 |
$hub->configureScope(function (Scope $scope) use($options) { |
| 86 |
$this->dynamicSamplingContext = DynamicSamplingContext::fromOptions($options, $scope); |
| 87 |
}); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
return (string) $this->dynamicSamplingContext; |
| 92 |
} |
| 93 |
/** |
| 94 |
* @return array{trace_id: string, span_id: string, parent_span_id?: string} |
| 95 |
*/ |
| 96 |
public function getTraceContext() : array |
| 97 |
{ |
| 98 |
$result = ['trace_id' => (string) $this->traceId, 'span_id' => (string) $this->spanId]; |
| 99 |
if ($this->parentSpanId !== null) { |
| 100 |
$result['parent_span_id'] = (string) $this->parentSpanId; |
| 101 |
} |
| 102 |
return $result; |
| 103 |
} |
| 104 |
public function getTraceId() : TraceId |
| 105 |
{ |
| 106 |
return $this->traceId; |
| 107 |
} |
| 108 |
public function setTraceId(TraceId $traceId) : void |
| 109 |
{ |
| 110 |
$this->traceId = $traceId; |
| 111 |
} |
| 112 |
public function getParentSpanId() : ?SpanId |
| 113 |
{ |
| 114 |
return $this->parentSpanId; |
| 115 |
} |
| 116 |
public function setParentSpanId(?SpanId $parentSpanId) : void |
| 117 |
{ |
| 118 |
$this->parentSpanId = $parentSpanId; |
| 119 |
} |
| 120 |
public function getSpanId() : SpanId |
| 121 |
{ |
| 122 |
return $this->spanId; |
| 123 |
} |
| 124 |
public function setSpanId(SpanId $spanId) : self |
| 125 |
{ |
| 126 |
$this->spanId = $spanId; |
| 127 |
return $this; |
| 128 |
} |
| 129 |
public function getDynamicSamplingContext() : ?DynamicSamplingContext |
| 130 |
{ |
| 131 |
return $this->dynamicSamplingContext; |
| 132 |
} |
| 133 |
public function setDynamicSamplingContext(DynamicSamplingContext $dynamicSamplingContext) : self |
| 134 |
{ |
| 135 |
$this->dynamicSamplingContext = $dynamicSamplingContext; |
| 136 |
return $this; |
| 137 |
} |
| 138 |
public function getSampleRand() : ?float |
| 139 |
{ |
| 140 |
return $this->sampleRand; |
| 141 |
} |
| 142 |
public function setSampleRand(?float $sampleRand) : self |
| 143 |
{ |
| 144 |
$this->sampleRand = $sampleRand; |
| 145 |
return $this; |
| 146 |
} |
| 147 |
private static function parseTraceparentAndBaggage(string $traceparent, string $baggage) : self |
| 148 |
{ |
| 149 |
$context = self::fromDefaults(); |
| 150 |
$parsedData = self::parseTraceAndBaggageHeaders($traceparent, $baggage); |
| 151 |
if ($parsedData['traceId'] !== null) { |
| 152 |
$context->traceId = $parsedData['traceId']; |
| 153 |
} |
| 154 |
if ($parsedData['parentSpanId'] !== null) { |
| 155 |
$context->parentSpanId = $parsedData['parentSpanId']; |
| 156 |
} |
| 157 |
if ($parsedData['parentSampled'] !== null) { |
| 158 |
$context->parentSampled = $parsedData['parentSampled']; |
| 159 |
} |
| 160 |
if ($parsedData['dynamicSamplingContext'] !== null) { |
| 161 |
$context->dynamicSamplingContext = $parsedData['dynamicSamplingContext']; |
| 162 |
} |
| 163 |
if ($parsedData['sampleRand'] !== null) { |
| 164 |
$context->sampleRand = $parsedData['sampleRand']; |
| 165 |
} |
| 166 |
return $context; |
| 167 |
} |
| 168 |
} |
| 169 |
|