-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTraceSpan.php
More file actions
70 lines (63 loc) · 2.32 KB
/
TraceSpan.php
File metadata and controls
70 lines (63 loc) · 2.32 KB
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
<?php
declare(strict_types=1);
namespace Arcp\Messages\Telemetry;
use Arcp\Envelope\MessageType;
use Arcp\Errors\InvalidArgumentException;
/** RFC §17.1 — typed span event. The envelope already carries trace_id/span_id. */
final readonly class TraceSpan extends MessageType
{
/** @param array<string, mixed> $attributes */
public function __construct(
public string $name,
public \DateTimeImmutable $startedAt,
public \DateTimeImmutable $endedAt,
public array $attributes = [],
public string $status = 'ok',
) {
if ($name === '') {
throw new InvalidArgumentException('span name missing');
}
}
#[\Override]
public static function typeName(): string
{
return 'trace.span';
}
#[\Override]
public function toArray(): array
{
$out = [
'name' => $this->name,
'started_at' => $this->startedAt->format(\DateTimeInterface::RFC3339_EXTENDED),
'ended_at' => $this->endedAt->format(\DateTimeInterface::RFC3339_EXTENDED),
'status' => $this->status,
];
if ($this->attributes !== []) {
$out['attributes'] = $this->attributes;
}
return $out;
}
#[\Override]
public static function fromArray(array $data): static
{
$name = $data['name'] ?? throw new InvalidArgumentException('name missing');
$start = $data['started_at'] ?? throw new InvalidArgumentException('started_at missing');
$end = $data['ended_at'] ?? throw new InvalidArgumentException('ended_at missing');
if (!\is_string($name) || !\is_string($start) || !\is_string($end)) {
throw new InvalidArgumentException('name/started_at/ended_at must be strings');
}
$attrs = [];
if (isset($data['attributes'])) {
if (!\is_array($data['attributes'])) {
throw new InvalidArgumentException('attributes must be object');
}
/** @var array<string, mixed> $attrs */
$attrs = $data['attributes'];
}
$status = 'ok';
if (isset($data['status']) && \is_string($data['status'])) {
$status = $data['status'];
}
return new static($name, new \DateTimeImmutable($start), new \DateTimeImmutable($end), $attrs, $status);
}
}