|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ZoiloMora\ElasticAPM\Processor\MetricSetProcessor; |
|
4
|
|
|
|
|
5
|
|
|
use ZoiloMora\ElasticAPM\Events\Event; |
|
6
|
|
|
use ZoiloMora\ElasticAPM\Events\Span\Span as SpanEvent; |
|
7
|
|
|
use ZoiloMora\ElasticAPM\Events\Transaction\Transaction as TransactionEvent; |
|
8
|
|
|
|
|
9
|
|
|
final class FromEventsSpanBuilder |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var ByParentIdFinder |
|
13
|
|
|
*/ |
|
14
|
|
|
private $byParentIdFinder; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var SelfDurationCalculator |
|
18
|
|
|
*/ |
|
19
|
|
|
private $selfDurationCalculator; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param ByParentIdFinder $byParentIdFinder |
|
23
|
|
|
* @param SelfDurationCalculator $selfDurationCalculator |
|
24
|
|
|
*/ |
|
25
|
13 |
|
public function __construct( |
|
26
|
|
|
ByParentIdFinder $byParentIdFinder, |
|
27
|
|
|
SelfDurationCalculator $selfDurationCalculator |
|
28
|
|
|
) { |
|
29
|
13 |
|
$this->byParentIdFinder = $byParentIdFinder; |
|
30
|
13 |
|
$this->selfDurationCalculator = $selfDurationCalculator; |
|
31
|
13 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param TransactionEvent|SpanEvent $event |
|
35
|
|
|
* @param Event[] $events |
|
36
|
|
|
* |
|
37
|
|
|
* @return Span |
|
38
|
|
|
*/ |
|
39
|
2 |
|
public function execute($event, array $events) |
|
40
|
|
|
{ |
|
41
|
2 |
|
return new Span( |
|
42
|
2 |
|
$this->getTransactionId($event), |
|
43
|
2 |
|
$this->getType($event), |
|
44
|
2 |
|
$this->getSubType($event), |
|
45
|
2 |
|
1, |
|
46
|
2 |
|
$this->getSelfDuration($event, $events) |
|
47
|
2 |
|
); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param Event $event |
|
52
|
|
|
* @return string |
|
53
|
|
|
*/ |
|
54
|
2 |
|
private function getTransactionId($event) |
|
55
|
|
|
{ |
|
56
|
|
|
return $event instanceof SpanEvent |
|
57
|
2 |
|
? $event->transactionId() |
|
58
|
2 |
|
: $event->id(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param Event $event |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
2 |
|
private function getType($event) |
|
66
|
|
|
{ |
|
67
|
|
|
return $event instanceof SpanEvent |
|
68
|
2 |
|
? $event->type() |
|
69
|
2 |
|
: 'app'; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param Event $event |
|
74
|
|
|
* @return string |
|
75
|
|
|
*/ |
|
76
|
2 |
|
private function getSubType($event) |
|
77
|
|
|
{ |
|
78
|
|
|
return $event instanceof SpanEvent |
|
79
|
2 |
|
? $event->subtype() |
|
80
|
2 |
|
: null; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @param TransactionEvent|SpanEvent $event |
|
85
|
|
|
* @param Event[] $events |
|
86
|
|
|
* |
|
87
|
|
|
* @return double |
|
88
|
|
|
*/ |
|
89
|
2 |
|
private function getSelfDuration($event, array $events) |
|
90
|
|
|
{ |
|
91
|
|
|
/** @var TransactionEvent[]|SpanEvent[] $childrenEvents */ |
|
92
|
2 |
|
$childrenEvents = $this->byParentIdFinder->execute( |
|
93
|
2 |
|
$event->id(), |
|
94
|
|
|
$events |
|
95
|
2 |
|
); |
|
96
|
|
|
|
|
97
|
2 |
|
return $this->selfDurationCalculator->execute($event, $childrenEvents); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|