-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PoC: Configuration\CompositeResolver and SPI discovery #1523
base: main
Are you sure you want to change the base?
Changes from all commits
0d3260e
bf3d240
aea69ca
0ffdd29
c4a6b27
7be429d
93c01c3
7e0de35
de80681
ec998c9
21e4902
938c2f6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Config\SDK\Configuration\Environment\Adapter; | ||
|
||
use function array_diff_key; | ||
use Composer\InstalledVersions; | ||
use Nevay\SPI\ServiceProviderDependency\PackageDependency; | ||
use OpenTelemetry\Config\SDK\Configuration\Environment\ArrayEnvSource; | ||
use OpenTelemetry\Config\SDK\Configuration\Environment\EnvSource; | ||
use OpenTelemetry\Config\SDK\Configuration\Environment\EnvSourceProvider; | ||
use Symfony\Component\Dotenv\Dotenv; | ||
use Symfony\Component\Dotenv\Exception\PathException; | ||
|
||
#[PackageDependency('symfony/dotenv', '^5.4 || ^6.4 || ^7.0')] | ||
final class SymfonyDotenvProvider implements EnvSourceProvider | ||
{ | ||
/** @psalm-suppress UndefinedClass */ | ||
public function getEnvSource(): EnvSource | ||
{ | ||
$installPath = InstalledVersions::getRootPackage()['install_path']; | ||
|
||
$backup = [$_SERVER, $_ENV]; | ||
$env = []; | ||
|
||
try { | ||
(new Dotenv())->bootEnv($installPath . '/.env'); | ||
$env = $_SERVER; | ||
} catch (PathException) { | ||
} finally { | ||
[$_SERVER, $_ENV] = $backup; | ||
} | ||
|
||
return new ArrayEnvSource(array_diff_key($env, $_SERVER)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Config\SDK\Configuration\Environment\Adapter; | ||
|
||
use Composer\InstalledVersions; | ||
use Dotenv\Dotenv; | ||
use Dotenv\Exception\InvalidPathException; | ||
use Nevay\SPI\ServiceProviderDependency\PackageDependency; | ||
use OpenTelemetry\Config\SDK\Configuration\Environment\ArrayEnvSource; | ||
use OpenTelemetry\Config\SDK\Configuration\Environment\EnvSource; | ||
use OpenTelemetry\Config\SDK\Configuration\Environment\EnvSourceProvider; | ||
|
||
#[PackageDependency('vlucas/phpdotenv', '^4.0 || ^5.0')] | ||
final class VlucasPhpdotenvProvider implements EnvSourceProvider | ||
{ | ||
/** @psalm-suppress UndefinedClass */ | ||
public function getEnvSource(): EnvSource | ||
{ | ||
$backup = [$_SERVER, $_ENV]; | ||
$env = []; | ||
|
||
try { | ||
$env = Dotenv::createImmutable([InstalledVersions::getRootPackage()['install_path']])->load(); | ||
} catch (InvalidPathException) { | ||
} finally { | ||
[$_SERVER, $_ENV] = $backup; | ||
} | ||
|
||
return new ArrayEnvSource(array_diff_key($env, $_SERVER)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Config\SDK\Configuration\Environment; | ||
|
||
interface EnvSourceProvider | ||
{ | ||
public function getEnvSource(): EnvSource; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\Config\SDK\Configuration\Environment; | ||
|
||
use Closure; | ||
|
||
final class LazyEnvSource implements EnvSource | ||
{ | ||
/** | ||
* @param Closure(): EnvSource|EnvSource $env | ||
*/ | ||
public function __construct( | ||
private Closure|EnvSource $env, | ||
) { | ||
} | ||
|
||
public function readRaw(string $name): mixed | ||
{ | ||
if (!$this->env instanceof EnvSource) { | ||
$this->env = ($this->env)(); | ||
} | ||
|
||
return $this->env->readRaw($name); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
namespace OpenTelemetry\SDK\Common\Configuration\Resolver; | ||
|
||
use Nevay\SPI\ServiceLoader; | ||
use OpenTelemetry\SDK\Common\Configuration\Configuration; | ||
|
||
/** | ||
|
@@ -18,6 +19,7 @@ public static function instance(): self | |
{ | ||
static $instance; | ||
$instance ??= new self([ | ||
...ServiceLoader::load(ResolverInterface::class), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This effectively replaces the below resolvers because it will use the -- Should |
||
new EnvironmentResolver(), | ||
new PhpIniResolver(), | ||
]); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace OpenTelemetry\SDK\Common\Configuration\Resolver; | ||
|
||
use Nevay\SPI\ServiceLoader; | ||
use Nevay\SPI\ServiceProviderDependency\PackageDependency; | ||
use OpenTelemetry\Config\SDK\Configuration\Environment\EnvSourceProvider; | ||
use OpenTelemetry\Config\SDK\Configuration\Environment\EnvSourceReader; | ||
use OpenTelemetry\Config\SDK\Configuration\Environment\LazyEnvSource; | ||
use OpenTelemetry\Config\SDK\Configuration\Environment\PhpIniEnvSource; | ||
use OpenTelemetry\Config\SDK\Configuration\Environment\ServerEnvSource; | ||
use OpenTelemetry\SDK\Common\Configuration\Configuration; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
#[PackageDependency('open-telemetry/sdk-configuration', '*')] | ||
class SdkConfigurationResolver implements ResolverInterface | ||
{ | ||
private readonly EnvSourceReader $reader; | ||
|
||
public function __construct() | ||
{ | ||
$envSources = []; | ||
$envSources[] = new ServerEnvSource(); | ||
$envSources[] = new PhpIniEnvSource(); | ||
|
||
/** @var EnvSourceProvider $envSourceProvider */ | ||
foreach (ServiceLoader::load(EnvSourceProvider::class) as $envSourceProvider) { | ||
$envSources[] = new LazyEnvSource($envSourceProvider->getEnvSource(...)); | ||
} | ||
|
||
$this->reader = new EnvSourceReader($envSources); | ||
} | ||
|
||
public function retrieveValue(string $variableName) | ||
{ | ||
return $this->reader->read($variableName); | ||
} | ||
|
||
public function hasVariable(string $variableName): bool | ||
{ | ||
return !Configuration::isEmpty($this->reader->read($variableName)); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -60,6 +60,9 @@ | |
"spi": { | ||
"OpenTelemetry\\API\\Instrumentation\\AutoInstrumentation\\HookManagerInterface": [ | ||
"OpenTelemetry\\API\\Instrumentation\\AutoInstrumentation\\ExtensionHookManager" | ||
], | ||
"OpenTelemetry\\SDK\\Common\\Configuration\\Resolver\\ResolverInterface": [ | ||
"OpenTelemetry\\SDK\\Common\\Configuration\\Resolver\\SdkConfigurationResolver" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only kicks in when |
||
] | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sort package config re-ordered this.