|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace TechDeCo\ElasticApmAgent\Convenience; |
|
5
|
|
|
|
|
6
|
|
|
use Ramsey\Uuid\Uuid; |
|
7
|
|
|
use Ramsey\Uuid\UuidInterface; |
|
8
|
|
|
use TechDeCo\ElasticApmAgent\Message\Context; |
|
9
|
|
|
use TechDeCo\ElasticApmAgent\Message\Span; |
|
10
|
|
|
use TechDeCo\ElasticApmAgent\Message\Timestamp; |
|
11
|
|
|
use TechDeCo\ElasticApmAgent\Message\Transaction; |
|
12
|
|
|
use function microtime; |
|
13
|
|
|
|
|
14
|
|
|
final class OpenTransaction |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* @var float |
|
18
|
|
|
*/ |
|
19
|
|
|
private $startOfTransaction; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var UuidInterface |
|
23
|
|
|
*/ |
|
24
|
|
|
private $id; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string |
|
28
|
|
|
*/ |
|
29
|
|
|
private $name; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var Timestamp |
|
33
|
|
|
*/ |
|
34
|
|
|
private $timestamp; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var Span[] |
|
38
|
|
|
*/ |
|
39
|
|
|
private $spanList = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
private $type; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var mixed[] |
|
48
|
|
|
*/ |
|
49
|
|
|
private $markList = []; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var UuidInterface |
|
53
|
|
|
*/ |
|
54
|
|
|
private $correlationId; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @var Context |
|
58
|
|
|
*/ |
|
59
|
|
|
private $context; |
|
60
|
|
|
|
|
61
|
76 |
|
public function __construct( |
|
62
|
|
|
UuidInterface $id, |
|
63
|
|
|
string $name, |
|
64
|
|
|
Timestamp $timestamp, |
|
65
|
|
|
string $type, |
|
66
|
|
|
?UuidInterface $correlationId = null |
|
67
|
|
|
) { |
|
68
|
76 |
|
$this->id = $id; |
|
69
|
76 |
|
$this->name = $name; |
|
70
|
76 |
|
$this->timestamp = $timestamp; |
|
71
|
76 |
|
$this->type = $type; |
|
72
|
76 |
|
$this->startOfTransaction = microtime(true); |
|
73
|
76 |
|
$this->correlationId = $correlationId ?? Uuid::uuid4(); |
|
74
|
76 |
|
$this->context = (new Context()) |
|
75
|
76 |
|
->withTag(ContextTags::CORRELATION_ID, $this->correlationId->toString()); |
|
76
|
76 |
|
} |
|
77
|
|
|
|
|
78
|
13 |
|
public function getId(): UuidInterface |
|
79
|
|
|
{ |
|
80
|
13 |
|
return $this->id; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
58 |
|
public function addSpan(Span $span): void |
|
84
|
|
|
{ |
|
85
|
58 |
|
$this->spanList[] = $span; |
|
86
|
58 |
|
} |
|
87
|
|
|
|
|
88
|
29 |
|
public function addMark(string $group, string $event, float $timestamp): void |
|
89
|
|
|
{ |
|
90
|
29 |
|
if (! isset($this->markList[$group])) { |
|
91
|
29 |
|
$this->markList[$group] = []; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
29 |
|
$this->markList[$group][$event] = $timestamp; |
|
95
|
29 |
|
} |
|
96
|
|
|
|
|
97
|
4 |
|
public function getContext(): Context |
|
98
|
|
|
{ |
|
99
|
4 |
|
return $this->context; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
4 |
|
public function setContext(Context $context): void |
|
103
|
|
|
{ |
|
104
|
4 |
|
$this->context = $context; |
|
105
|
4 |
|
} |
|
106
|
|
|
|
|
107
|
34 |
|
public function toTransaction(): Transaction |
|
108
|
|
|
{ |
|
109
|
34 |
|
$transaction = new Transaction( |
|
110
|
34 |
|
(microtime(true) - $this->startOfTransaction) * 1000, |
|
111
|
34 |
|
$this->id, |
|
112
|
34 |
|
$this->name, |
|
113
|
34 |
|
$this->timestamp, |
|
114
|
34 |
|
$this->type |
|
115
|
|
|
); |
|
116
|
34 |
|
$transaction = $transaction->withSpan(...$this->spanList) |
|
117
|
34 |
|
->inContext($this->context); |
|
118
|
|
|
|
|
119
|
34 |
|
foreach ($this->markList as $group => $eventList) { |
|
120
|
15 |
|
foreach ($eventList as $event => $timestamp) { |
|
121
|
15 |
|
$transaction = $transaction->marking($group, $event, $timestamp); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
34 |
|
return $transaction; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return float The offset in microseconds with microsecond precision |
|
130
|
|
|
*/ |
|
131
|
30 |
|
public function getStartOffset(): float |
|
132
|
|
|
{ |
|
133
|
30 |
|
return (microtime(true) - $this->startOfTransaction) * 1000; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
7 |
|
public function getCorrelationId(): UuidInterface |
|
137
|
|
|
{ |
|
138
|
7 |
|
return $this->correlationId; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|