Skip to content

Commit 40a1f89

Browse files
authored
Add option to disable debug scopes (#1205)
1 parent 99f3d54 commit 40a1f89

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

Context.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
namespace OpenTelemetry\Context;
66

77
use function assert;
8+
use const FILTER_VALIDATE_BOOLEAN;
9+
use function filter_var;
10+
use function ini_get;
811
use function spl_object_id;
912

1013
/**
@@ -80,12 +83,19 @@ public static function getCurrent(): ContextInterface
8083
public function activate(): ScopeInterface
8184
{
8285
$scope = self::storage()->attach($this);
83-
/** @psalm-suppress RedundantCondition */
84-
assert((bool) $scope = new DebugScope($scope));
86+
/** @psalm-suppress RedundantCondition @phpstan-ignore-next-line */
87+
assert(self::debugScopesDisabled() || $scope = new DebugScope($scope));
8588

8689
return $scope;
8790
}
8891

92+
private static function debugScopesDisabled(): bool
93+
{
94+
$disabled = $_SERVER['OTEL_PHP_DEBUG_SCOPES_DISABLED'] ?? ini_get('OTEL_PHP_DEBUG_SCOPES_DISABLED');
95+
96+
return filter_var($disabled, FILTER_VALIDATE_BOOLEAN);
97+
}
98+
8999
public function withContextValue(ImplicitContextKeyedInterface $value): ContextInterface
90100
{
91101
return $value->storeInContext($this);

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ try {
3131

3232
It is recommended to use a `try-finally` statement after `::activate()` to ensure that the created scope is properly `::detach()`ed.
3333

34+
### Debug scopes
35+
36+
By default, scopes created by `::activate()` warn on invalid and missing calls to `::detach()` in non-production
37+
environments. This feature can be disabled by setting the environment variable `OTEL_PHP_DEBUG_SCOPES_DISABLED` to a
38+
truthy value. Disabling is only recommended for applications using `exit` / `die` to prevent unavoidable notices.
39+
3440
## Async applications
3541

3642
### Fiber support

0 commit comments

Comments
 (0)