From 684d650c9fde2b450ba972d460335e75da0b0a36 Mon Sep 17 00:00:00 2001 From: Brett McBride Date: Tue, 5 Mar 2024 12:27:17 +1100 Subject: [PATCH] use configuration for context debug scopes disable --- deptrac.yaml | 1 + src/API/Behavior/Internal/Logging.php | 12 +--- src/Config/Resolver/CompositeResolver.php | 2 +- src/Config/ValueTypes.php | 2 + src/Config/Variables.php | 1 + src/Context/Context.php | 11 +--- src/Context/composer.json | 1 + tests/Unit/Context/ContextTest.php | 75 +++++------------------ 8 files changed, 27 insertions(+), 78 deletions(-) diff --git a/deptrac.yaml b/deptrac.yaml index 74d2be469..d4ec0b65a 100644 --- a/deptrac.yaml +++ b/deptrac.yaml @@ -85,6 +85,7 @@ deptrac: ruleset: Context: - FFI + - Config SemConv: ~ API: - Config diff --git a/src/API/Behavior/Internal/Logging.php b/src/API/Behavior/Internal/Logging.php index e5bec7ab5..8369b7642 100644 --- a/src/API/Behavior/Internal/Logging.php +++ b/src/API/Behavior/Internal/Logging.php @@ -5,6 +5,8 @@ namespace OpenTelemetry\API\Behavior\Internal; use OpenTelemetry\API\Behavior\Internal\LogWriter\LogWriterInterface; +use OpenTelemetry\Config\Configuration; +use OpenTelemetry\Config\Variables; use Psr\Log\LogLevel; /** @@ -69,15 +71,7 @@ public static function logLevel(): int private static function getLogLevel(): int { - $level = array_key_exists(self::OTEL_LOG_LEVEL, $_SERVER) - ? $_SERVER[self::OTEL_LOG_LEVEL] - : getenv(self::OTEL_LOG_LEVEL); - if (!$level) { - $level = ini_get(self::OTEL_LOG_LEVEL); - } - if (!$level) { - $level = self::DEFAULT_LEVEL; - } + $level = Configuration::getEnum(Variables::OTEL_LOG_LEVEL, self::DEFAULT_LEVEL); return self::level($level); } diff --git a/src/Config/Resolver/CompositeResolver.php b/src/Config/Resolver/CompositeResolver.php index 39d162bbf..61cebcad6 100644 --- a/src/Config/Resolver/CompositeResolver.php +++ b/src/Config/Resolver/CompositeResolver.php @@ -11,7 +11,7 @@ */ class CompositeResolver { - // @var array + // @var list private array $resolvers = []; public static function instance(): self diff --git a/src/Config/ValueTypes.php b/src/Config/ValueTypes.php index 59308e0b5..5e7cefca9 100644 --- a/src/Config/ValueTypes.php +++ b/src/Config/ValueTypes.php @@ -130,4 +130,6 @@ interface ValueTypes public const OTEL_PHP_LOG_DESTINATION = VariableTypes::ENUM; public const OTEL_PHP_INTERNAL_METRICS_ENABLED = VariableTypes::BOOL; public const OTEL_PHP_DISABLED_INSTRUMENTATIONS = VariableTypes::LIST; + public const OTEL_PHP_DEBUG_SCOPES_DISABLED = VariableTypes::BOOL; + public const OTEL_PHP_EXCLUDED_URLS = VariableTypes::LIST; } diff --git a/src/Config/Variables.php b/src/Config/Variables.php index cdf565884..41129e455 100644 --- a/src/Config/Variables.php +++ b/src/Config/Variables.php @@ -138,6 +138,7 @@ interface Variables public const OTEL_PHP_DETECTORS = 'OTEL_PHP_DETECTORS'; public const OTEL_PHP_AUTOLOAD_ENABLED = 'OTEL_PHP_AUTOLOAD_ENABLED'; public const OTEL_PHP_INTERNAL_METRICS_ENABLED = 'OTEL_PHP_INTERNAL_METRICS_ENABLED'; //whether the SDK should emit its own metrics + public const OTEL_PHP_DEBUG_SCOPES_DISABLED = 'OTEL_PHP_DEBUG_SCOPES_DISABLED'; public const OTEL_PHP_DISABLED_INSTRUMENTATIONS = 'OTEL_PHP_DISABLED_INSTRUMENTATIONS'; public const OTEL_PHP_EXCLUDED_URLS = 'OTEL_PHP_EXCLUDED_URLS'; } diff --git a/src/Context/Context.php b/src/Context/Context.php index 278c27d3e..2ee017581 100644 --- a/src/Context/Context.php +++ b/src/Context/Context.php @@ -5,8 +5,8 @@ namespace OpenTelemetry\Context; use function assert; -use const FILTER_VALIDATE_BOOLEAN; -use function filter_var; +use OpenTelemetry\Config\Configuration; +use OpenTelemetry\Config\Variables; use function spl_object_id; /** @@ -14,8 +14,6 @@ */ final class Context implements ContextInterface { - private const OTEL_PHP_DEBUG_SCOPES_DISABLED = 'OTEL_PHP_DEBUG_SCOPES_DISABLED'; - /** @var ContextStorageInterface&ExecutionContextAwareInterface */ private static ContextStorageInterface $storage; @@ -92,10 +90,7 @@ public function activate(): ScopeInterface private static function debugScopesDisabled(): bool { - return filter_var( - $_SERVER[self::OTEL_PHP_DEBUG_SCOPES_DISABLED] ?? \getenv(self::OTEL_PHP_DEBUG_SCOPES_DISABLED) ?: \ini_get(self::OTEL_PHP_DEBUG_SCOPES_DISABLED), - FILTER_VALIDATE_BOOLEAN - ); + return Configuration::getBoolean(Variables::OTEL_PHP_DEBUG_SCOPES_DISABLED, false); } public function withContextValue(ImplicitContextKeyedInterface $value): ContextInterface diff --git a/src/Context/composer.json b/src/Context/composer.json index ee191de9b..d82cc7b4a 100644 --- a/src/Context/composer.json +++ b/src/Context/composer.json @@ -18,6 +18,7 @@ ], "require": { "php": "^8.0", + "open-telemetry/config": "*", "symfony/polyfill-php81": "^1.26", "symfony/polyfill-php82": "^1.26" }, diff --git a/tests/Unit/Context/ContextTest.php b/tests/Unit/Context/ContextTest.php index b023300a4..27b1d7822 100644 --- a/tests/Unit/Context/ContextTest.php +++ b/tests/Unit/Context/ContextTest.php @@ -4,6 +4,8 @@ namespace OpenTelemetry\Tests\Unit\Context; +use AssertWell\PHPUnitGlobalState\EnvironmentVariables; +use OpenTelemetry\Config\Variables; use OpenTelemetry\Context\Context; use OpenTelemetry\Context\ContextKeys; use OpenTelemetry\Context\DebugScope; @@ -16,6 +18,15 @@ */ class ContextTest extends TestCase { + use EnvironmentVariables; + + public function tearDown(): void + { + $this->restoreEnvironmentVariables(); + unset($_SERVER[Variables::OTEL_PHP_DEBUG_SCOPES_DISABLED]); + \putenv('OTEL_PHP_DEBUG_SCOPES_DISABLED'); + } + public function test_activate(): void { $context = Context::getRoot(); @@ -179,87 +190,31 @@ public function test_attach_and_detach_set_current_ctx(): void } } - public function test_debug_scopes_disabled_env_var(): void + public function test_debug_scopes_disabled(): void { - \putenv('OTEL_PHP_DEBUG_SCOPES_DISABLED=1'); + $this->setEnvironmentVariable(Variables::OTEL_PHP_DEBUG_SCOPES_DISABLED, 'true'); $context = Context::getRoot(); - $scope = $context->activate(); try { $this->assertNotInstanceOf(DebugScope::class, $scope); } finally { $scope->detach(); - \putenv('OTEL_PHP_DEBUG_SCOPES_DISABLED'); } } - public function test_debug_scopes_disabled_server_var(): void + public function test_debug_scopes_disabled_false(): void { - $_SERVER['OTEL_PHP_DEBUG_SCOPES_DISABLED'] = '1'; + $this->setEnvironmentVariable(Variables::OTEL_PHP_DEBUG_SCOPES_DISABLED, 'false'); $context = Context::getRoot(); - - $scope = $context->activate(); - - try { - $this->assertNotInstanceOf(DebugScope::class, $scope); - } finally { - $scope->detach(); - unset($_SERVER['OTEL_PHP_DEBUG_SCOPES_DISABLED']); - } - } - - public function test_debug_scopes_disabled_order_server(): void - { - $_SERVER['OTEL_PHP_DEBUG_SCOPES_DISABLED'] = '1'; - \putenv('OTEL_PHP_DEBUG_SCOPES_DISABLED=0'); - - $context = Context::getRoot(); - - $scope = $context->activate(); - - try { - $this->assertNotInstanceOf(DebugScope::class, $scope); - } finally { - $scope->detach(); - unset($_SERVER['OTEL_PHP_DEBUG_SCOPES_DISABLED']); - \putenv('OTEL_PHP_DEBUG_SCOPES_DISABLED'); - } - } - - public function test_debug_scopes_disabled_falsey_server(): void - { - $_SERVER['OTEL_PHP_DEBUG_SCOPES_DISABLED'] = '0'; - \putenv('OTEL_PHP_DEBUG_SCOPES_DISABLED=1'); - - $context = Context::getRoot(); - - $scope = $context->activate(); - - try { - $this->assertInstanceOf(DebugScope::class, $scope); - } finally { - $scope->detach(); - unset($_SERVER['OTEL_PHP_DEBUG_SCOPES_DISABLED']); - \putenv('OTEL_PHP_DEBUG_SCOPES_DISABLED'); - } - } - - public function test_debug_scopes_disabled_falsey_env(): void - { - \putenv('OTEL_PHP_DEBUG_SCOPES_DISABLED=0'); - - $context = Context::getRoot(); - $scope = $context->activate(); try { $this->assertInstanceOf(DebugScope::class, $scope); } finally { $scope->detach(); - \putenv('OTEL_PHP_DEBUG_SCOPES_DISABLED'); } } }