1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
<?php
namespace Wikimedia\Telemetry;
use JsonSerializable;
/**
* Data transfer object holding data associated with a given span.
*
* @since 1.43
*/
class SpanContext implements JsonSerializable {
/**
* The ID of the trace this span is part of, as a hexadecimal string.
* @var string
*/
private string $traceId;
/**
* The ID of this span, as a hexadecimal string.
* @var string
*/
private string $spanId;
/**
* The ID of this span, as a hexadecimal string, or `null` if this is a root span.
* @var string|null
*/
private ?string $parentSpanId;
/**
* A concise description of the work represented by this span.
* @see TracerInterface::createSpan()
* @var string
*/
private string $name;
/**
* Whether the active sampler decided to sample and record this span.
* @var bool
*/
private bool $sampled;
/**
* Key-value metadata associated with this span.
* @see Span::setAttributes()
* @var array
*/
private array $attributes = [];
/**
* Describes the relationship of this span to other spans within the same trace.
* @see Span::setSpanKind()
* @var int
*/
private int $spanKind = SpanInterface::SPAN_KIND_INTERNAL;
/**
* UNIX epoch timestamp in nanoseconds at which this span was started,
* or `null` if this span was not started yet.
* @var int|null
*/
private ?int $startEpochNanos = null;
/**
* UNIX epoch timestamp in nanoseconds at which this span was ended,
* or `null` if this span was not ended yet.
* @var int|null
*/
private ?int $endEpochNanos = null;
public function __construct(
string $traceId,
string $spanId,
?string $parentSpanId,
string $name,
bool $sampled
) {
$this->traceId = $traceId;
$this->spanId = $spanId;
$this->parentSpanId = $parentSpanId;
$this->name = $name;
$this->sampled = $sampled;
}
public function setEndEpochNanos( int $endEpochNanos ): void {
$this->endEpochNanos = $endEpochNanos;
}
public function setStartEpochNanos( int $startEpochNanos ): void {
$this->startEpochNanos = $startEpochNanos;
}
public function setSpanKind( int $spanKind ): void {
$this->spanKind = $spanKind;
}
public function setAttributes( array $attributes ): void {
$this->attributes = $attributes;
}
public function isSampled(): bool {
return $this->sampled;
}
public function getSpanId(): string {
return $this->spanId;
}
public function getTraceId(): string {
return $this->traceId;
}
public function getParentSpanId(): ?string {
return $this->parentSpanId;
}
public function wasStarted(): bool {
return $this->startEpochNanos !== null;
}
public function wasEnded(): bool {
return $this->endEpochNanos !== null;
}
public function jsonSerialize(): array {
$json = [
'traceId' => $this->traceId,
'parentSpanId' => $this->parentSpanId,
'spanId' => $this->spanId,
'name' => $this->name,
'startTimeUnixNano' => $this->startEpochNanos,
'endTimeUnixNano' => $this->endEpochNanos,
'kind' => $this->spanKind
];
if ( $this->attributes ) {
$json['attributes'] = OtlpSerializer::serializeKeyValuePairs( $this->attributes );
}
return $json;
}
/**
* Check whether the given SpanContext belongs to the same span.
*
* @param SpanContext|null $other
* @return bool
*/
public function equals( ?SpanContext $other ): bool {
if ( $other === null ) {
return false;
}
return $other->spanId === $this->spanId;
}
}
|