diff --git a/src/Context/Context.php b/src/Context/Context.php index a12c2de60..1a0b26226 100644 --- a/src/Context/Context.php +++ b/src/Context/Context.php @@ -7,7 +7,6 @@ use function assert; use const FILTER_VALIDATE_BOOLEAN; use function filter_var; -use function ini_get; use function spl_object_id; /** @@ -15,6 +14,8 @@ */ final class Context implements ContextInterface { + private const OTEL_PHP_DEBUG_SCOPES_DISABLED = 'OTEL_PHP_DEBUG_SCOPES_DISABLED'; + /** @var ContextStorageInterface&ExecutionContextAwareInterface */ private static ContextStorageInterface $storage; @@ -91,9 +92,10 @@ public function activate(): ScopeInterface private static function debugScopesDisabled(): bool { - $disabled = $_SERVER['OTEL_PHP_DEBUG_SCOPES_DISABLED'] ?? ini_get('OTEL_PHP_DEBUG_SCOPES_DISABLED'); - - return filter_var($disabled, FILTER_VALIDATE_BOOLEAN); + 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 + ); } public function withContextValue(ImplicitContextKeyedInterface $value): ContextInterface diff --git a/tests/Unit/Context/ContextTest.php b/tests/Unit/Context/ContextTest.php index 213ff66c9..b023300a4 100644 --- a/tests/Unit/Context/ContextTest.php +++ b/tests/Unit/Context/ContextTest.php @@ -6,6 +6,7 @@ use OpenTelemetry\Context\Context; use OpenTelemetry\Context\ContextKeys; +use OpenTelemetry\Context\DebugScope; use OpenTelemetry\Context\ImplicitContextKeyedInterface; use PHPUnit\Framework\TestCase; use stdClass; @@ -177,4 +178,88 @@ public function test_attach_and_detach_set_current_ctx(): void $scope->detach(); } } + + public function test_debug_scopes_disabled_env_var(): void + { + \putenv('OTEL_PHP_DEBUG_SCOPES_DISABLED=1'); + + $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 + { + $_SERVER['OTEL_PHP_DEBUG_SCOPES_DISABLED'] = '1'; + + $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'); + } + } }