Skip to content

Commit

Permalink
improve excluded urls code (#1227)
Browse files Browse the repository at this point in the history
improve code and add more tests to the OTEL_PHP_EXCLUDED_URLS feature
  • Loading branch information
brettmc authored Feb 2, 2024
1 parent 36e2b7f commit e522d9d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 28 deletions.
45 changes: 34 additions & 11 deletions src/SDK/SdkAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,9 @@

class SdkAutoloader
{
private static ?bool $enabled = null;

public static function autoload(): bool
{
try {
self::$enabled ??= Configuration::getBoolean(Variables::OTEL_PHP_AUTOLOAD_ENABLED);
} catch (InvalidArgumentException $e) {
//invalid setting, assume false
self::$enabled = false;
}
if (!self::$enabled || self::isIgnoredUrl()) {
if (!self::isEnabled() || self::isExcludedUrl()) {
return false;
}
Globals::registerInitializer(function (Configurator $configurator) {
Expand Down Expand Up @@ -96,8 +88,39 @@ public static function isIgnoredUrl(): bool
/**
* @internal
*/
public static function reset(): void
public static function isEnabled(): bool
{
try {
$enabled = Configuration::getBoolean(Variables::OTEL_PHP_AUTOLOAD_ENABLED);
} catch (InvalidArgumentException $e) {
//invalid setting, assume false
return false;
}

return $enabled;
}

/**
* Test whether a request URI is set, and if it matches the excluded urls configuration option
*
* @internal
*/
public static function isExcludedUrl(): bool
{
self::$enabled = null;
$excludedUrls = Configuration::getList(Variables::OTEL_PHP_EXCLUDED_URLS, []);
if ($excludedUrls === []) {
return false;
}
$url = $_SERVER['REQUEST_URI'] ?? null;
if (!$url) {
return false;
}
foreach ($excludedUrls as $excludedUrl) {
if (preg_match(sprintf('|%s|', $excludedUrl), $url) === 1) {
return true;
}
}

return false;
}
}
57 changes: 40 additions & 17 deletions tests/Unit/SDK/SdkAutoloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public function setUp(): void
{
LoggerHolder::set(new NullLogger());
Globals::reset();
SdkAutoloader::reset();
}

public function tearDown(): void
Expand All @@ -37,17 +36,35 @@ public function tearDown(): void

public function test_disabled_by_default(): void
{
$this->assertFalse(SdkAutoloader::autoload());
$this->assertInstanceOf(NoopMeterProvider::class, Globals::meterProvider());
$this->assertInstanceOf(NoopTracerProvider::class, Globals::tracerProvider());
$this->assertInstanceOf(NoopLoggerProvider::class, Globals::loggerProvider());
$this->assertInstanceOf(NoopTextMapPropagator::class, Globals::propagator(), 'propagator not initialized by disabled autoloader');
$this->assertFalse(SdkAutoloader::isEnabled());
}

public function test_disabled_with_invalid_flag(): void
public function test_enabled(): void
{
$this->setEnvironmentVariable(Variables::OTEL_PHP_AUTOLOAD_ENABLED, 'true');
$this->assertTrue(SdkAutoloader::isEnabled());
}

public function test_disabled(): void
{
$this->setEnvironmentVariable(Variables::OTEL_PHP_AUTOLOAD_ENABLED, 'false');
$this->assertFalse(SdkAutoloader::isEnabled());
}

public function test_disabled_with_invalid_setting(): void
{
$this->setEnvironmentVariable(Variables::OTEL_PHP_AUTOLOAD_ENABLED, 'invalid-value');
$this->assertFalse(SdkAutoloader::isEnabled());
}

public function test_noop_if_disabled(): void
{
$this->setEnvironmentVariable(Variables::OTEL_PHP_AUTOLOAD_ENABLED, 'false');
$this->assertFalse(SdkAutoloader::autoload());
$this->assertInstanceOf(NoopMeterProvider::class, Globals::meterProvider());
$this->assertInstanceOf(NoopTracerProvider::class, Globals::tracerProvider());
$this->assertInstanceOf(NoopLoggerProvider::class, Globals::loggerProvider());
$this->assertInstanceOf(NoopTextMapPropagator::class, Globals::propagator(), 'propagator not initialized by disabled autoloader');
}

public function test_sdk_disabled_does_not_disable_propagator(): void
Expand All @@ -70,26 +87,24 @@ public function test_enabled_by_configuration(): void
$this->assertNotInstanceOf(NoopLoggerProvider::class, Globals::loggerProvider());
}

public function test_ignore_urls_without_request_uri(): void
public function test_exclude_urls_without_request_uri(): void
{
$this->setEnvironmentVariable(Variables::OTEL_PHP_AUTOLOAD_ENABLED, 'true');
$this->setEnvironmentVariable(Variables::OTEL_PHP_EXCLUDED_URLS, '*');
$this->setEnvironmentVariable(Variables::OTEL_PHP_EXCLUDED_URLS, '.*');
unset($_SERVER['REQUEST_URI']);
$this->assertFalse(SdkAutoloader::isIgnoredUrl());
$this->assertFalse(SdkAutoloader::isExcludedUrl());
}

/**
* @dataProvider ignoreUrlsProvider
* @dataProvider excludeUrlsProvider
*/
public function test_ignore_urls(string $ignore, string $uri, bool $expected): void
public function test_exclude_urls(string $exclude, string $uri, bool $expected): void
{
$this->setEnvironmentVariable(Variables::OTEL_PHP_AUTOLOAD_ENABLED, 'true');
$this->setEnvironmentVariable(Variables::OTEL_PHP_EXCLUDED_URLS, $ignore);
$this->setEnvironmentVariable(Variables::OTEL_PHP_EXCLUDED_URLS, $exclude);
$_SERVER['REQUEST_URI'] = $uri;
$this->assertSame($expected, SdkAutoloader::isIgnoredUrl());
$this->assertSame($expected, SdkAutoloader::isExcludedUrl());
}

public static function ignoreUrlsProvider(): array
public static function excludeUrlsProvider(): array
{
return [
[
Expand Down Expand Up @@ -124,4 +139,12 @@ public static function ignoreUrlsProvider(): array
],
];
}

public function test_enabled_with_excluded_url(): void
{
$this->setEnvironmentVariable(Variables::OTEL_PHP_AUTOLOAD_ENABLED, 'true');
$this->setEnvironmentVariable(Variables::OTEL_PHP_EXCLUDED_URLS, '.*');
$_SERVER['REQUEST_URI'] = '/test';
$this->assertFalse(SdkAutoloader::autoload());
}
}

0 comments on commit e522d9d

Please sign in to comment.