-
Notifications
You must be signed in to change notification settings - Fork 160
/
Copy pathSpanContext.php
190 lines (171 loc) · 4.89 KB
/
SpanContext.php
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace DDTrace;
use ArrayIterator;
use DDTrace\Contracts\SpanContext as SpanContextInterface;
use DDTrace\Data\SpanContext as SpanContextData;
final class SpanContext extends SpanContextData
{
public function __construct(
$traceId,
$spanId,
$parentId = null,
array $baggageItems = [],
$isDistributedTracingActivationContext = false
) {
$this->traceId = $traceId;
$this->spanId = $spanId;
$this->parentId = $parentId ?: null;
$this->baggageItems = $baggageItems;
$this->isDistributedTracingActivationContext = $isDistributedTracingActivationContext;
}
public static function createAsChild(SpanContextInterface $parentContext, $startTime = null)
{
// @phpstan-ignore-next-line
$origin = \property_exists($parentContext, 'origin') ? $parentContext->origin : null;
if (!$parentContext->isDistributedTracingActivationContext() || !active_span()) {
if ($parentContext->isDistributedTracingActivationContext()) {
set_distributed_tracing_context(
$parentContext->getTraceId(),
$parentContext->getSpanId(),
$origin
);
}
if ($startTime) {
start_span($startTime);
} else {
start_span(); // we'll peek at the span stack top later
}
}
$instance = new self(
$parentContext->getTraceId(),
active_span()->id,
$parentContext->getSpanId(),
$parentContext->getAllBaggageItems(),
false
);
$instance->parentContext = $parentContext;
$instance->setPropagatedPrioritySampling($parentContext->getPropagatedPrioritySampling());
if ($origin) {
$instance->origin = $origin;
}
return $instance;
}
public static function createAsRoot(array $baggageItems = [], $startTime = null)
{
// with peek the current span id for the existing root span
if (!active_span()) {
if ($startTime) {
start_span($startTime);
} else {
start_span(); // we'll peek at the span stack top later
}
}
$nextId = active_span()->id;
return new self(
$nextId,
$nextId,
null,
$baggageItems,
false
);
}
/**
* {@inheritdoc}
*/
public function getTraceId()
{
return $this->traceId;
}
/**
* {@inheritdoc}
*/
public function getSpanId()
{
return $this->spanId;
}
/**
* {@inheritdoc}
*/
public function getParentId()
{
return $this->parentId;
}
/**
* {@inheritdoc}
*/
#[\ReturnTypeWillChange]
public function getIterator()
{
return new ArrayIterator($this->baggageItems);
}
/**
* {@inheritdoc}
*/
public function getPropagatedPrioritySampling()
{
return $this->propagatedPrioritySampling;
}
/**
* {@inheritdoc}
*/
public function setPropagatedPrioritySampling($propagatedPrioritySampling)
{
$this->propagatedPrioritySampling = $propagatedPrioritySampling;
}
/**
* {@inheritdoc}
*/
public function getBaggageItem($key)
{
return array_key_exists($key, $this->baggageItems)
? $this->baggageItems[$key]
: null;
}
/**
* {@inheritdoc}
*/
public function withBaggageItem($key, $value)
{
return new self(
$this->traceId,
$this->spanId,
$this->parentId,
array_merge($this->baggageItems, [$key => $value])
);
}
/**
* {@inheritdoc}
*/
public function getAllBaggageItems()
{
return $this->baggageItems;
}
public function isEqual(SpanContextInterface $spanContext)
{
if ($spanContext instanceof SpanContextData) {
return $this->traceId === $spanContext->traceId
&& $this->spanId === $spanContext->spanId
&& $this->parentId === $spanContext->parentId
&& $this->baggageItems === $spanContext->baggageItems;
}
return
$this->traceId === $spanContext->getTraceId()
&& $this->spanId === $spanContext->getSpanId()
&& $this->parentId === $spanContext->getParentId()
&& $this->baggageItems === $spanContext->getAllBaggageItems();
}
/**
* {@inheritdoc}
*/
public function isDistributedTracingActivationContext()
{
return $this->isDistributedTracingActivationContext;
}
/**
* {@inheritdoc}
*/
public function isHostRoot()
{
return $this->parentContext === null || $this->parentContext->isDistributedTracingActivationContext();
}
}