Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow OTEL_PHP_DEBUG_SCOPES_DISABLED as en environment variable #1237

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/Context/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
use function assert;
use const FILTER_VALIDATE_BOOLEAN;
use function filter_var;
use function ini_get;
use function spl_object_id;

/**
* @see https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/context/README.md#context
*/
final class Context implements ContextInterface
{
private const OTEL_PHP_DEBUG_SCOPES_DISABLED = 'OTEL_PHP_DEBUG_SCOPES_DISABLED';

/** @var ContextStorageInterface&ExecutionContextAwareInterface */
private static ContextStorageInterface $storage;

Expand Down Expand Up @@ -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
Expand Down
85 changes: 85 additions & 0 deletions tests/Unit/Context/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
brettmc marked this conversation as resolved.
Show resolved Hide resolved

$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');
}
}
}
Loading