|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lamoda\Metric\Storage; |
|
4
|
|
|
|
|
5
|
|
|
use Lamoda\Metric\Collector\CollectorRegistry; |
|
6
|
|
|
use Lamoda\Metric\Common\Metric; |
|
7
|
|
|
use Lamoda\Metric\Common\MetricSourceInterface; |
|
8
|
|
|
use Lamoda\Metric\Common\Source\IterableMetricSource; |
|
9
|
|
|
use Lamoda\Metric\Storage\Exception\ReceiverException; |
|
10
|
|
|
|
|
11
|
|
|
final class MaterializeHelper |
|
12
|
|
|
{ |
|
13
|
|
|
/** @var CollectorRegistry */ |
|
14
|
|
|
private $collectorRegistry; |
|
15
|
|
|
/** @var StorageRegistry */ |
|
16
|
|
|
private $storageRegistry; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @param CollectorRegistry $collectorRegistry |
|
20
|
|
|
* @param StorageRegistry $storageRegistry |
|
21
|
|
|
*/ |
|
22
|
1 |
|
public function __construct(CollectorRegistry $collectorRegistry, StorageRegistry $storageRegistry) |
|
23
|
|
|
{ |
|
24
|
1 |
|
$this->collectorRegistry = $collectorRegistry; |
|
25
|
1 |
|
$this->storageRegistry = $storageRegistry; |
|
26
|
1 |
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Receives metrics from named collector to named storage. |
|
30
|
|
|
* |
|
31
|
|
|
* @param string $collectorName |
|
32
|
|
|
* @param string $storageName |
|
33
|
|
|
* |
|
34
|
|
|
* @throws ReceiverException |
|
35
|
|
|
* @throws \OutOfBoundsException |
|
36
|
|
|
*/ |
|
37
|
1 |
|
public function materialize(string $collectorName, string $storageName): void |
|
38
|
|
|
{ |
|
39
|
1 |
|
$collector = $this->collectorRegistry->getCollector($collectorName); |
|
40
|
1 |
|
$storage = $this->storageRegistry->getStorage($storageName); |
|
41
|
|
|
|
|
42
|
1 |
|
$storage->receive($this->materializeSource($collector->collect())); |
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
private function materializeSource(MetricSourceInterface $source): MetricSourceInterface |
|
46
|
|
|
{ |
|
47
|
1 |
|
$metrics = []; |
|
48
|
1 |
|
foreach ($source->getMetrics() as $metric) { |
|
49
|
1 |
|
$metrics[] = new Metric($metric->getName(), $metric->resolve(), $metric->getTags()); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
return new IterableMetricSource($metrics); |
|
53
|
|
|
} |
|
54
|
|
|
} |
|
55
|
|
|
|