|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Lamoda\Metric\MetricBundle\DependencyInjection\DefinitionFactory; |
|
4
|
|
|
|
|
5
|
|
|
use Lamoda\Metric\Collector\MergingCollector; |
|
6
|
|
|
use Lamoda\Metric\Collector\SingleSourceCollector; |
|
7
|
|
|
use Lamoda\Metric\Collector\TaggingCollectorDecorator; |
|
8
|
|
|
use Lamoda\Metric\Common\Source\IterableMetricSource; |
|
9
|
|
|
use Lamoda\Metric\Common\Source\MergingMetricSource; |
|
10
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
11
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
12
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @internal |
|
16
|
|
|
*/ |
|
17
|
|
|
final class Collector |
|
18
|
|
|
{ |
|
19
|
|
|
const TAG = 'lamoda_metrics.collector'; |
|
20
|
|
|
const ALIAS_ATTRIBUTE = 'alias'; |
|
21
|
|
|
|
|
22
|
|
|
const ID_PREFIX = 'lamoda_metrics.collector.'; |
|
23
|
|
|
|
|
24
|
|
|
const COLLECTOR_TYPE_SERVICE = 'service'; |
|
25
|
|
|
const COLLECTOR_TYPE_PRECONFIGURED = 'sources'; |
|
26
|
|
|
const COLLECTOR_TYPE_MERGING = 'merge'; |
|
27
|
|
|
|
|
28
|
|
|
const TYPES = [ |
|
29
|
|
|
self::COLLECTOR_TYPE_SERVICE, |
|
30
|
|
|
self::COLLECTOR_TYPE_PRECONFIGURED, |
|
31
|
|
|
self::COLLECTOR_TYPE_MERGING, |
|
32
|
|
|
]; |
|
33
|
|
|
const REGISTRY_ID = 'lamoda_metrics.collector_registry'; |
|
34
|
|
|
|
|
35
|
4 |
|
public static function createId(string $name): string |
|
36
|
|
|
{ |
|
37
|
4 |
|
return self::ID_PREFIX . $name; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
4 |
|
public static function register(ContainerBuilder $container, string $name, array $config) |
|
41
|
|
|
{ |
|
42
|
4 |
|
switch ($config['type']) { |
|
43
|
4 |
|
case self::COLLECTOR_TYPE_PRECONFIGURED: |
|
44
|
4 |
|
self::registerPreconfigured($container, $name, $config); |
|
45
|
4 |
|
break; |
|
46
|
|
|
case self::COLLECTOR_TYPE_MERGING: |
|
47
|
|
|
self::registerMerging($container, $name, $config); |
|
48
|
|
|
break; |
|
49
|
|
|
case self::COLLECTOR_TYPE_SERVICE: |
|
50
|
|
|
$container->getDefinition(self::REGISTRY_ID)->addMethodCall( |
|
51
|
|
|
'register', |
|
52
|
|
|
[$name, self::createReference($name)] |
|
53
|
|
|
); |
|
54
|
|
|
$container->setAlias(self::createId($name), $config['id']); |
|
55
|
|
|
break; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
4 |
|
if (!empty($config['default_tags'])) { |
|
59
|
4 |
|
self::decorateWithDefaultTags($container, $name, $config); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
4 |
|
self::addToRegistry($container, $name); |
|
63
|
4 |
|
} |
|
64
|
|
|
|
|
65
|
4 |
|
public static function createReference(string $name): Reference |
|
66
|
|
|
{ |
|
67
|
4 |
|
return new Reference(self::createId($name)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
private static function registerMerging(ContainerBuilder $container, string $name, array $config) |
|
71
|
|
|
{ |
|
72
|
|
|
$definition = $container->register(self::createId($name), MergingCollector::class); |
|
73
|
|
|
$collectorNames = $config['collectors']; |
|
74
|
|
|
$refs = array_map([self::class, 'createReference'], $collectorNames); |
|
75
|
|
|
$definition->setArguments([$refs, $config['tags']]); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
4 |
|
private static function registerPreconfigured(ContainerBuilder $container, string $name, array $config) |
|
79
|
|
|
{ |
|
80
|
4 |
|
$definition = $container->register(self::createId($name), SingleSourceCollector::class); |
|
81
|
4 |
|
$sources = []; |
|
82
|
4 |
|
foreach ($config['sources'] as $sourceAlias) { |
|
83
|
4 |
|
$sources[] = Source::createReference($sourceAlias); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
4 |
|
if (!empty($config['metrics'])) { |
|
87
|
|
|
$metricServices = []; |
|
88
|
|
|
foreach ($config['metrics'] as $metricService) { |
|
89
|
|
|
$metricServices[] = new Reference($metricService); |
|
90
|
|
|
} |
|
91
|
|
|
$sources[] = new Definition(IterableMetricSource::class, [$metricServices]); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
4 |
|
$definition->setArguments( |
|
95
|
4 |
|
[new Definition(MergingMetricSource::class, $sources)] |
|
96
|
|
|
); |
|
97
|
4 |
|
} |
|
98
|
|
|
|
|
99
|
4 |
|
private static function decorateWithDefaultTags(ContainerBuilder $container, string $name, array $config): void |
|
100
|
|
|
{ |
|
101
|
4 |
|
$decoratorId = self::createId($name) . '.default_tags'; |
|
102
|
|
|
|
|
103
|
4 |
|
$container->register($decoratorId, TaggingCollectorDecorator::class) |
|
104
|
4 |
|
->setDecoratedService(self::createId($name)) |
|
105
|
4 |
|
->setArguments([new Reference($decoratorId . '.inner'), $config['default_tags']]); |
|
106
|
4 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param ContainerBuilder $container |
|
110
|
|
|
* @param string $name |
|
111
|
|
|
*/ |
|
112
|
4 |
|
private static function addToRegistry(ContainerBuilder $container, string $name): void |
|
113
|
|
|
{ |
|
114
|
4 |
|
$registry = $container->getDefinition(Collector::REGISTRY_ID); |
|
115
|
4 |
|
$registry->addMethodCall('register', [$name, self::createReference($name)]); |
|
116
|
4 |
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|