From 40a1f8941b7e8578bf3780d61e445bca5e9fbe5a Mon Sep 17 00:00:00 2001 From: Tobias Bachert Date: Tue, 9 Jan 2024 01:33:41 +0100 Subject: [PATCH] Add option to disable debug scopes (#1205) --- Context.php | 14 ++++++++++++-- README.md | 6 ++++++ 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Context.php b/Context.php index 32b0162..a12c2de 100644 --- a/Context.php +++ b/Context.php @@ -5,6 +5,9 @@ namespace OpenTelemetry\Context; use function assert; +use const FILTER_VALIDATE_BOOLEAN; +use function filter_var; +use function ini_get; use function spl_object_id; /** @@ -80,12 +83,19 @@ public static function getCurrent(): ContextInterface public function activate(): ScopeInterface { $scope = self::storage()->attach($this); - /** @psalm-suppress RedundantCondition */ - assert((bool) $scope = new DebugScope($scope)); + /** @psalm-suppress RedundantCondition @phpstan-ignore-next-line */ + assert(self::debugScopesDisabled() || $scope = new DebugScope($scope)); return $scope; } + 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); + } + public function withContextValue(ImplicitContextKeyedInterface $value): ContextInterface { return $value->storeInContext($this); diff --git a/README.md b/README.md index 4dfe0e2..8a1ea25 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,12 @@ try { It is recommended to use a `try-finally` statement after `::activate()` to ensure that the created scope is properly `::detach()`ed. +### Debug scopes + +By default, scopes created by `::activate()` warn on invalid and missing calls to `::detach()` in non-production +environments. This feature can be disabled by setting the environment variable `OTEL_PHP_DEBUG_SCOPES_DISABLED` to a +truthy value. Disabling is only recommended for applications using `exit` / `die` to prevent unavoidable notices. + ## Async applications ### Fiber support