Skip to content

Commit

Permalink
Add option to disable debug scopes (#1205)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nevay authored Jan 9, 2024
1 parent 99f3d54 commit 40a1f89
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 40a1f89

Please sign in to comment.