| 1 |
<?php |
| 2 |
namespace Wikimedia\Telemetry; |
| 3 |
|
| 4 |
use JsonSerializable; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
class SpanContext implements JsonSerializable { |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
private string $traceId; |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
private string $spanId; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
private ?string $parentSpanId; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
private string $name; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
private bool $sampled; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
private array $attributes = []; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private int $spanKind = SpanInterface::SPAN_KIND_INTERNAL; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
private ?int $startEpochNanos = null; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private ?int $endEpochNanos = null; |
| 71 |
|
| 72 |
public function __construct( |
| 73 |
string $traceId, |
| 74 |
string $spanId, |
| 75 |
?string $parentSpanId, |
| 76 |
string $name, |
| 77 |
bool $sampled |
| 78 |
) { |
| 79 |
$this->traceId = $traceId; |
| 80 |
$this->spanId = $spanId; |
| 81 |
$this->parentSpanId = $parentSpanId; |
| 82 |
$this->name = $name; |
| 83 |
$this->sampled = $sampled; |
| 84 |
} |
| 85 |
|
| 86 |
public function setEndEpochNanos( int $endEpochNanos ): void { |
| 87 |
$this->endEpochNanos = $endEpochNanos; |
| 88 |
} |
| 89 |
|
| 90 |
public function setStartEpochNanos( int $startEpochNanos ): void { |
| 91 |
$this->startEpochNanos = $startEpochNanos; |
| 92 |
} |
| 93 |
|
| 94 |
public function setSpanKind( int $spanKind ): void { |
| 95 |
$this->spanKind = $spanKind; |
| 96 |
} |
| 97 |
|
| 98 |
public function setAttributes( array $attributes ): void { |
| 99 |
$this->attributes = $attributes; |
| 100 |
} |
| 101 |
|
| 102 |
public function isSampled(): bool { |
| 103 |
return $this->sampled; |
| 104 |
} |
| 105 |
|
| 106 |
public function getSpanId(): string { |
| 107 |
return $this->spanId; |
| 108 |
} |
| 109 |
|
| 110 |
public function getTraceId(): string { |
| 111 |
return $this->traceId; |
| 112 |
} |
| 113 |
|
| 114 |
public function getParentSpanId(): ?string { |
| 115 |
return $this->parentSpanId; |
| 116 |
} |
| 117 |
|
| 118 |
public function wasStarted(): bool { |
| 119 |
return $this->startEpochNanos !== null; |
| 120 |
} |
| 121 |
|
| 122 |
public function wasEnded(): bool { |
| 123 |
return $this->endEpochNanos !== null; |
| 124 |
} |
| 125 |
|
| 126 |
public function jsonSerialize(): array { |
| 127 |
$json = [ |
| 128 |
'traceId' => $this->traceId, |
| 129 |
'parentSpanId' => $this->parentSpanId, |
| 130 |
'spanId' => $this->spanId, |
| 131 |
'name' => $this->name, |
| 132 |
'startTimeUnixNano' => $this->startEpochNanos, |
| 133 |
'endTimeUnixNano' => $this->endEpochNanos, |
| 134 |
'kind' => $this->spanKind |
| 135 |
]; |
| 136 |
|
| 137 |
if ( $this->attributes ) { |
| 138 |
$json['attributes'] = OtlpSerializer::serializeKeyValuePairs( $this->attributes ); |
| 139 |
} |
| 140 |
|
| 141 |
return $json; |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public function equals( ?SpanContext $other ): bool { |
| 151 |
if ( $other === null ) { |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
return $other->spanId === $this->spanId; |
| 156 |
} |
| 157 |
} |