| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\Sentry\Tracing; |
| 5 |
|
| 6 |
use WCPOS\Vendor\Sentry\Tracing\Traits\TraceHeaderParserTrait; |
| 7 |
final class TransactionContext extends SpanContext |
| 8 |
{ |
| 9 |
use TraceHeaderParserTrait; |
| 10 |
public const DEFAULT_NAME = '<unlabeled transaction>'; |
| 11 |
/** |
| 12 |
* @var string Name of the transaction |
| 13 |
*/ |
| 14 |
private $name; |
| 15 |
/** |
| 16 |
* @var bool|null The parent's sampling decision |
| 17 |
*/ |
| 18 |
private $parentSampled; |
| 19 |
/** |
| 20 |
* @var TransactionMetadata The transaction metadata |
| 21 |
*/ |
| 22 |
private $metadata; |
| 23 |
/** |
| 24 |
* Constructor. |
| 25 |
* |
| 26 |
* @param string $name The name of the transaction |
| 27 |
* @param bool|null $parentSampled The parent's sampling decision |
| 28 |
* @param TransactionMetadata|null $metadata The transaction metadata |
| 29 |
*/ |
| 30 |
public function __construct(string $name = self::DEFAULT_NAME, ?bool $parentSampled = null, ?TransactionMetadata $metadata = null) |
| 31 |
{ |
| 32 |
$this->name = $name; |
| 33 |
$this->parentSampled = $parentSampled; |
| 34 |
$this->metadata = $metadata ?? new TransactionMetadata(); |
| 35 |
} |
| 36 |
/** |
| 37 |
* @return self |
| 38 |
*/ |
| 39 |
public static function make() |
| 40 |
{ |
| 41 |
return new self(); |
| 42 |
} |
| 43 |
/** |
| 44 |
* Gets the name of the transaction. |
| 45 |
*/ |
| 46 |
public function getName() : string |
| 47 |
{ |
| 48 |
return $this->name; |
| 49 |
} |
| 50 |
/** |
| 51 |
* Sets the name of the transaction. |
| 52 |
* |
| 53 |
* @param string $name The name |
| 54 |
*/ |
| 55 |
public function setName(string $name) : self |
| 56 |
{ |
| 57 |
$this->name = $name; |
| 58 |
return $this; |
| 59 |
} |
| 60 |
/** |
| 61 |
* Gets the parent's sampling decision. |
| 62 |
*/ |
| 63 |
public function getParentSampled() : ?bool |
| 64 |
{ |
| 65 |
return $this->parentSampled; |
| 66 |
} |
| 67 |
/** |
| 68 |
* Sets the parent's sampling decision. |
| 69 |
* |
| 70 |
* @param bool|null $parentSampled The decision |
| 71 |
*/ |
| 72 |
public function setParentSampled(?bool $parentSampled) : self |
| 73 |
{ |
| 74 |
$this->parentSampled = $parentSampled; |
| 75 |
return $this; |
| 76 |
} |
| 77 |
/** |
| 78 |
* Gets the transaction metadata. |
| 79 |
*/ |
| 80 |
public function getMetadata() : TransactionMetadata |
| 81 |
{ |
| 82 |
return $this->metadata; |
| 83 |
} |
| 84 |
/** |
| 85 |
* Sets the transaction metadata. |
| 86 |
* |
| 87 |
* @param TransactionMetadata $metadata The transaction metadata |
| 88 |
*/ |
| 89 |
public function setMetadata(TransactionMetadata $metadata) : self |
| 90 |
{ |
| 91 |
$this->metadata = $metadata; |
| 92 |
return $this; |
| 93 |
} |
| 94 |
/** |
| 95 |
* Sets the transaction source. |
| 96 |
* |
| 97 |
* @param TransactionSource $transactionSource The transaction source |
| 98 |
*/ |
| 99 |
public function setSource(TransactionSource $transactionSource) : self |
| 100 |
{ |
| 101 |
$this->metadata->setSource($transactionSource); |
| 102 |
return $this; |
| 103 |
} |
| 104 |
/** |
| 105 |
* Returns a context populated with the data of the given environment variables. |
| 106 |
* |
| 107 |
* @param string $sentryTrace The sentry-trace value from the environment |
| 108 |
* @param string $baggage The baggage header value from the environment |
| 109 |
*/ |
| 110 |
public static function fromEnvironment(string $sentryTrace, string $baggage) : self |
| 111 |
{ |
| 112 |
return self::parseTraceAndBaggage($sentryTrace, $baggage); |
| 113 |
} |
| 114 |
/** |
| 115 |
* Returns a context populated with the data of the given headers. |
| 116 |
* |
| 117 |
* @param string $sentryTraceHeader The sentry-trace header from an incoming request |
| 118 |
* @param string $baggageHeader The baggage header from an incoming request |
| 119 |
*/ |
| 120 |
public static function fromHeaders(string $sentryTraceHeader, string $baggageHeader) : self |
| 121 |
{ |
| 122 |
return self::parseTraceAndBaggage($sentryTraceHeader, $baggageHeader); |
| 123 |
} |
| 124 |
private static function parseTraceAndBaggage(string $sentryTrace, string $baggage) : self |
| 125 |
{ |
| 126 |
$context = new self(); |
| 127 |
$parsedData = self::parseTraceAndBaggageHeaders($sentryTrace, $baggage); |
| 128 |
if ($parsedData['traceId'] !== null) { |
| 129 |
$context->traceId = $parsedData['traceId']; |
| 130 |
} |
| 131 |
if ($parsedData['parentSpanId'] !== null) { |
| 132 |
$context->parentSpanId = $parsedData['parentSpanId']; |
| 133 |
} |
| 134 |
if ($parsedData['parentSampled'] !== null) { |
| 135 |
$context->parentSampled = $parsedData['parentSampled']; |
| 136 |
} |
| 137 |
if ($parsedData['dynamicSamplingContext'] !== null) { |
| 138 |
$context->getMetadata()->setDynamicSamplingContext($parsedData['dynamicSamplingContext']); |
| 139 |
} |
| 140 |
if ($parsedData['parentSamplingRate'] !== null) { |
| 141 |
$context->getMetadata()->setParentSamplingRate($parsedData['parentSamplingRate']); |
| 142 |
} |
| 143 |
if ($parsedData['sampleRand'] !== null) { |
| 144 |
$context->getMetadata()->setSampleRand($parsedData['sampleRand']); |
| 145 |
} |
| 146 |
return $context; |
| 147 |
} |
| 148 |
} |
| 149 |
|