-
Notifications
You must be signed in to change notification settings - Fork 147
/
Span.php
331 lines (286 loc) · 6.63 KB
/
Span.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<?php
namespace DDTrace;
use BadMethodCallException;
use DDTrace\Exceptions\InvalidSpanArgument;
use Exception;
use InvalidArgumentException;
use OpenTracing\SpanContext as OpenTracingContext;
use Throwable;
use OpenTracing\Span as OpenTracingSpan;
final class Span implements OpenTracingSpan
{
/**
* Operation Name is the name of the operation being measured. Some examples
* might be "http.handler", "fileserver.upload" or "video.decompress".
* Name should be set on every span.
*
* @var string
*/
private $operationName;
/**
* @var OpenTracingContext
*/
private $context;
/**
* Resource is a query to a service. A web application might use
* resources like "/user/{user_id}". A sql database might use resources
* like "select * from user where id = ?".
*
* You can track thousands of resources (not millions or billions) so
* prefer normalized resources like "/user/{id}" to "/user/123".
*
* Resources should only be set on an app's top level spans.
*
* @var string
*/
private $resource;
/**
* Service is the name of the process doing a particular job. Some
* examples might be "user-database" or "datadog-web-app". Services
* will be inherited from parents, so only set this in your app's
* top level span.
*
* @var string
*/
private $service;
/**
* Protocol associated with the span
*
* @var string|null
*/
private $type;
/**
* @var int
*/
private $startTime;
/**
* @var int|null
*/
private $duration;
/**
* @var array
*/
private $tags = [];
/**
* @var bool
*/
private $hasError = false;
/**
* Span constructor.
* @param string $operationName
* @param SpanContext $context
* @param string $service
* @param string $resource
* @param int|null $startTime
*/
public function __construct(
$operationName,
SpanContext $context,
$service,
$resource,
$startTime = null
) {
$this->context = $context;
$this->operationName = (string) $operationName;
$this->service = (string) $service;
$this->resource = (string) $resource;
$this->startTime = $startTime ?: Time\now();
}
/**
* @return string
*/
public function getTraceId()
{
return $this->context->getTraceId();
}
/**
* @return string
*/
public function getSpanId()
{
return $this->context->getSpanId();
}
/**
* @return null|string
*/
public function getParentId()
{
return $this->context->getParentId();
}
/**
* {@inheritdoc}
*/
public function overwriteOperationName($operationName)
{
$this->operationName = $operationName;
}
/**
* @return string
*/
public function getResource()
{
return $this->resource;
}
/**
* @return string
*/
public function getService()
{
return $this->service;
}
/**
* @return string|null
*/
public function getType()
{
return $this->type;
}
/**
* @return int
*/
public function getStartTime()
{
return $this->startTime;
}
/**
* @return int
*/
public function getDuration()
{
return $this->duration;
}
/**
* {@inheritdoc}
*/
public function setTag($key, $value)
{
if ($this->isFinished()) {
return;
}
if ($key !== (string) $key) {
throw InvalidSpanArgument::forTagKey($key);
}
if ($key === Tags\SERVICE_NAME) {
$this->service = $value;
return;
}
if ($key === Tags\RESOURCE_NAME) {
$this->resource = $value;
return;
}
if ($key === Tags\SPAN_TYPE) {
$this->type = $value;
return;
}
$this->tags[$key] = (string) $value;
}
/**
* @param string $key
* @return string|null
*/
public function getTag($key)
{
if (array_key_exists($key, $this->tags)) {
return $this->tags[$key];
}
return null;
}
/**
* @return array
*/
public function getAllTags()
{
return $this->tags;
}
public function setResource($resource)
{
$this->resource = (string) $resource;
}
/**
* Stores a Throwable object within the span tags. The error status is
* updated and the error.Error() string is included with a default tag key.
* If the Span has been finished, it will not be modified by this method.
*
* @param Throwable|Exception $error
* @throws InvalidArgumentException
*/
public function setError($error)
{
if ($this->isFinished()) {
return;
}
if (($error instanceof Exception) || ($error instanceof Throwable)) {
$this->hasError = true;
$this->tags[Tags\ERROR_MSG] = $error->getMessage();
$this->tags[Tags\ERROR_TYPE] = get_class($error);
$this->tags[Tags\ERROR_STACK] = $error->getTraceAsString();
return;
}
throw InvalidSpanArgument::forError($error);
}
public function hasError()
{
return $this->hasError;
}
/**
* {@inheritdoc}
*/
public function finish($finishTime = null)
{
if ($this->isFinished()) {
return;
}
$this->duration = ($finishTime ?: Time\now()) - $this->startTime;
}
/**
* @param Throwable|Exception $error
* @return void
*/
public function finishWithError($error)
{
$this->setError($error);
$this->finish();
}
/**
* @return bool
*/
public function isFinished()
{
return $this->duration !== null;
}
/**
* {@inheritdoc}
*/
public function getOperationName()
{
return $this->operationName;
}
/**
* {@inheritdoc}
*/
public function getContext()
{
return $this->context;
}
/**
* {@inheritdoc}
*/
public function log(array $fields = [], $timestamp = null)
{
throw new BadMethodCallException('not implemented');
}
/**
* {@inheritdoc}
*/
public function addBaggageItem($key, $value)
{
$this->context = $this->context->withBaggageItem($key, $value);
}
/**
* {@inheritdoc}
*/
public function getBaggageItem($key)
{
return $this->context->getBaggageItem($key);
}
}