Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpanContext
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 14
306
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 setEndEpochNanos
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setStartEpochNanos
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setSpanKind
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setSpanStatus
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setAttributes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isSampled
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getSpanId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTraceId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getParentSpanId
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 wasStarted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 wasEnded
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 jsonSerialize
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
12
 equals
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2namespace Wikimedia\Telemetry;
3
4use JsonSerializable;
5
6/**
7 * Data transfer object holding data associated with a given span.
8 *
9 * @since 1.43
10 */
11class 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     * The success or failure of this span.
60     * @see Span::setSpanStatus()
61     * @var int
62     */
63    private int $spanStatus = SpanInterface::SPAN_STATUS_UNSET;
64
65    /**
66     * UNIX epoch timestamp in nanoseconds at which this span was started,
67     * or `null` if this span was not started yet.
68     * @var int|null
69     */
70    private ?int $startEpochNanos = null;
71
72    /**
73     * UNIX epoch timestamp in nanoseconds at which this span was ended,
74     * or `null` if this span was not ended yet.
75     * @var int|null
76     */
77    private ?int $endEpochNanos = null;
78
79    public function __construct(
80        string $traceId,
81        string $spanId,
82        ?string $parentSpanId,
83        string $name,
84        bool $sampled
85    ) {
86        $this->traceId = $traceId;
87        $this->spanId = $spanId;
88        $this->parentSpanId = $parentSpanId;
89        $this->name = $name;
90        $this->sampled = $sampled;
91    }
92
93    public function setEndEpochNanos( int $endEpochNanos ): void {
94        $this->endEpochNanos = $endEpochNanos;
95    }
96
97    public function setStartEpochNanos( int $startEpochNanos ): void {
98        $this->startEpochNanos = $startEpochNanos;
99    }
100
101    public function setSpanKind( int $spanKind ): void {
102        $this->spanKind = $spanKind;
103    }
104
105    public function setSpanStatus( int $status ): void {
106        $this->spanStatus = $status;
107    }
108
109    public function setAttributes( array $attributes ): void {
110        $this->attributes = array_merge( $this->attributes, $attributes );
111    }
112
113    public function isSampled(): bool {
114        return $this->sampled;
115    }
116
117    public function getSpanId(): string {
118        return $this->spanId;
119    }
120
121    public function getTraceId(): string {
122        return $this->traceId;
123    }
124
125    public function getParentSpanId(): ?string {
126        return $this->parentSpanId;
127    }
128
129    public function wasStarted(): bool {
130        return $this->startEpochNanos !== null;
131    }
132
133    public function wasEnded(): bool {
134        return $this->endEpochNanos !== null;
135    }
136
137    public function jsonSerialize(): array {
138        $json = [
139            'traceId' => $this->traceId,
140            'parentSpanId' => $this->parentSpanId,
141            'spanId' => $this->spanId,
142            'name' => $this->name,
143            'startTimeUnixNano' => $this->startEpochNanos,
144            'endTimeUnixNano' => $this->endEpochNanos,
145            'kind' => $this->spanKind
146        ];
147
148        if ( $this->spanStatus !== SpanInterface::SPAN_STATUS_UNSET ) {
149            $json['status'] = [ 'code' => $this->spanStatus ];
150        }
151
152        if ( $this->attributes ) {
153            $json['attributes'] = OtlpSerializer::serializeKeyValuePairs( $this->attributes );
154        }
155
156        return $json;
157    }
158
159    /**
160     * Check whether the given SpanContext belongs to the same span.
161     *
162     * @param SpanContext|null $other
163     * @return bool
164     */
165    public function equals( ?SpanContext $other ): bool {
166        if ( $other === null ) {
167            return false;
168        }
169
170        return $other->spanId === $this->spanId;
171    }
172}