-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathConfigurationFactory.php
More file actions
173 lines (154 loc) · 7 KB
/
ConfigurationFactory.php
File metadata and controls
173 lines (154 loc) · 7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
<?php
declare(strict_types=1);
namespace OpenTelemetry\Config\SDK\Configuration;
use function class_exists;
use Exception;
use function getcwd;
use function is_file;
use OpenTelemetry\API\Configuration\Config\ComponentPlugin;
use OpenTelemetry\API\Configuration\Config\ComponentProvider;
use OpenTelemetry\Config\SDK\Configuration\Environment\EnvReader;
use OpenTelemetry\Config\SDK\Configuration\Environment\EnvResourceChecker;
use OpenTelemetry\Config\SDK\Configuration\Internal\CompiledConfigurationFactory;
use OpenTelemetry\Config\SDK\Configuration\Internal\ComponentProviderRegistry;
use OpenTelemetry\Config\SDK\Configuration\Internal\ConfigurationLoader;
use OpenTelemetry\Config\SDK\Configuration\Internal\EnvSubstitutionNormalization;
use OpenTelemetry\Config\SDK\Configuration\Internal\NodeDefinition\ArrayNodeDefinition;
use OpenTelemetry\Config\SDK\Configuration\Internal\NodeDefinition\BooleanNodeDefinition;
use OpenTelemetry\Config\SDK\Configuration\Internal\NodeDefinition\FloatNodeDefinition;
use OpenTelemetry\Config\SDK\Configuration\Internal\NodeDefinition\IntegerNodeDefinition;
use OpenTelemetry\Config\SDK\Configuration\Internal\NodeDefinition\ScalarNodeDefinition;
use OpenTelemetry\Config\SDK\Configuration\Internal\NodeDefinition\StringNodeDefinition;
use OpenTelemetry\Config\SDK\Configuration\Internal\NodeDefinition\VariableNodeDefinition;
use OpenTelemetry\Config\SDK\Configuration\Internal\ResourceCollection;
use OpenTelemetry\Config\SDK\Configuration\Internal\TrackingEnvReader;
use OpenTelemetry\Config\SDK\Configuration\Loader\YamlExtensionFileLoader;
use OpenTelemetry\Config\SDK\Configuration\Loader\YamlSymfonyFileLoader;
use function serialize;
use function sprintf;
use Symfony\Component\Config\Definition\Builder\NodeBuilder;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader\DelegatingLoader;
use Symfony\Component\Config\Loader\LoaderResolver;
use Symfony\Component\Config\Resource\SelfCheckingResourceChecker;
use Symfony\Component\Config\ResourceCheckerConfigCache;
use Symfony\Component\VarExporter\VarExporter;
use Throwable;
use function var_export;
/**
* @template T
*/
final readonly class ConfigurationFactory
{
private CompiledConfigurationFactory $compiledFactory;
/**
* @param iterable<ComponentProvider> $componentProviders
* @param ComponentProvider<T> $rootComponent
* @param EnvReader $envReader
*/
public function __construct(
private iterable $componentProviders,
private ComponentProvider $rootComponent,
private EnvReader $envReader,
) {
$this->compiledFactory = $this->compileFactory();
}
/**
* @param array $configs configs to process
* @param ResourceCollection|null $resources resources that can be used for cache invalidation
* @throws InvalidConfigurationException if the configuration is invalid
* @return ComponentPlugin<T> processed component plugin
*/
public function process(array $configs, ?ResourceCollection $resources = null): ComponentPlugin
{
return $this->compiledFactory
->process($configs, $resources);
}
/**
* @param string|list<string> $file path(s) to parse
* @param string|null $cacheFile path to cache parsed configuration to
* @param bool $debug will check for cache freshness if debug mode enabled
* @throws Exception if loading of a configuration file fails for any reason
* @throws InvalidConfigurationException if the configuration is invalid
* @throws Throwable if a cache file is given and a non-serializable component provider is used
* @return ComponentPlugin parsed component plugin
*
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/configuration/file-configuration.md#parse
* @psalm-suppress PossiblyNullReference
*/
public function parseFile(
string|array $file,
?string $cacheFile = null,
bool $debug = true,
): ComponentPlugin {
$cache = null;
$resources = null;
if ($cacheFile !== null) {
$cache = new ResourceCheckerConfigCache($cacheFile, [
new SelfCheckingResourceChecker(),
new EnvResourceChecker($this->envReader),
]);
if (is_file($cache->getPath())
&& ($configuration = @include $cache->getPath()) instanceof ComponentPlugin
&& (!$debug || $cache->isFresh())) {
return $configuration;
}
$resources = new ResourceCollection();
$resources->addClassResource(ComponentPlugin::class);
$resources->addClassResource(VarExporter::class);
}
$loader = new ConfigurationLoader($resources);
$cwd = getcwd() ?: [];
$locator = new FileLocator($cwd);
$fileLoader = new DelegatingLoader(new LoaderResolver([
new YamlSymfonyFileLoader($loader, $locator),
new YamlExtensionFileLoader($loader, $locator),
]));
foreach ((array) $file as $path) {
$fileLoader->load($path);
}
$configuration = $this->compiledFactory
->process($loader->getConfigurations(), $resources);
$cache?->write(
class_exists(VarExporter::class)
? sprintf('<?php return %s;', VarExporter::export($configuration))
: sprintf('<?php return unserialize(%s);', var_export(serialize($configuration), true)),
$resources->toArray()
);
return $configuration;
}
private function compileFactory(): CompiledConfigurationFactory
{
$envReader = new TrackingEnvReader($this->envReader);
$normalizations = [
// Parse MUST perform environment variable substitution.
new EnvSubstitutionNormalization($envReader),
];
$builder = new NodeBuilder();
$builder->setNodeClass('variable', VariableNodeDefinition::class);
$builder->setNodeClass('scalar', ScalarNodeDefinition::class);
$builder->setNodeClass('boolean', BooleanNodeDefinition::class);
$builder->setNodeClass('integer', IntegerNodeDefinition::class);
$builder->setNodeClass('float', FloatNodeDefinition::class);
$builder->setNodeClass('array', ArrayNodeDefinition::class);
$builder->setNodeClass('string', StringNodeDefinition::class);
$registry = new ComponentProviderRegistry($normalizations, $builder);
foreach ($this->componentProviders as $provider) {
$registry->register($provider);
}
$root = $this->rootComponent->getConfig($registry, $builder);
foreach ($normalizations as $normalization) {
$normalization->apply($root);
}
$node = $root->getNode(forceRootNode: true);
return new CompiledConfigurationFactory(
$this->rootComponent,
$node,
[
$registry,
$envReader,
],
);
}
}