Debian Codegraph @

includes/libs/telemetry/SpanContext.php

raw symbols SCIP
1 <?php
2 namespace Wikimedia\Telemetry;
3
4 use JsonSerializable;
5
6 /**
7 * Data transfer object holding data associated with a given span.
8 *
9 * @since 1.43
10 */
11 class SpanContext implements JsonSerializable {
12
13 /**
14 * The ID of the trace this span is part of, as a hexadecimal string.
15 * @var string
16 */
17 private string $traceId;
18
19 /**
20 * The ID of this span, as a hexadecimal string.
21 * @var string
22 */
23 private string $spanId;
24
25 /**
26 * The ID of this span, as a hexadecimal string, or `null` if this is a root span.
27 * @var string|null
28 */
29 private ?string $parentSpanId;
30
31 /**
32 * A concise description of the work represented by this span.
33 * @see TracerInterface::createSpan()
34 * @var string
35 */
36 private string $name;
37
38 /**
39 * Whether the active sampler decided to sample and record this span.
40 * @var bool
41 */
42 private bool $sampled;
43
44 /**
45 * Key-value metadata associated with this span.
46 * @see Span::setAttributes()
47 * @var array
48 */
49 private array $attributes = [];
50
51 /**
52 * Describes the relationship of this span to other spans within the same trace.
53 * @see Span::setSpanKind()
54 * @var int
55 */
56 private int $spanKind = SpanInterface::SPAN_KIND_INTERNAL;
57
58 /**
59 * UNIX epoch timestamp in nanoseconds at which this span was started,
60 * or `null` if this span was not started yet.
61 * @var int|null
62 */
63 private ?int $startEpochNanos = null;
64
65 /**
66 * UNIX epoch timestamp in nanoseconds at which this span was ended,
67 * or `null` if this span was not ended yet.
68 * @var int|null
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 * Check whether the given SpanContext belongs to the same span.
146 *
147 * @param SpanContext|null $other
148 * @return bool
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 }