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
|
|
|
public function __construct(CollectorRegistry $collectorRegistry, StorageRegistry $storageRegistry) |
23
|
|
|
{ |
24
|
|
|
$this->collectorRegistry = $collectorRegistry; |
25
|
|
|
$this->storageRegistry = $storageRegistry; |
26
|
|
|
} |
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
|
|
|
public function materialize(string $collectorName, string $storageName): void |
38
|
|
|
{ |
39
|
|
|
$collector = $this->collectorRegistry->getCollector($collectorName); |
40
|
|
|
$storage = $this->storageRegistry->getStorage($storageName); |
41
|
|
|
|
42
|
|
|
$storage->receive($this->materializeSource($collector->collect())); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
private function materializeSource(MetricSourceInterface $source): MetricSourceInterface |
46
|
|
|
{ |
47
|
|
|
$metrics = []; |
48
|
|
|
foreach ($source->getMetrics() as $metric) { |
49
|
|
|
$metrics[] = new Metric($metric->getName(), $metric->resolve(), $metric->getTags()); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return new IterableMetricSource($metrics); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|