diff --git a/examples/traces/features/ignore_request_urls.php b/examples/traces/features/ignore_request_urls.php new file mode 100644 index 000000000..ff2c33d3c --- /dev/null +++ b/examples/traces/features/ignore_request_urls.php @@ -0,0 +1,22 @@ +getTracer('demo'); +$tracer->spanBuilder('healthcheck')->startSpan()->end(); diff --git a/src/SDK/Common/Configuration/Variables.php b/src/SDK/Common/Configuration/Variables.php index d0bb3c8ab..8ccb28ac1 100644 --- a/src/SDK/Common/Configuration/Variables.php +++ b/src/SDK/Common/Configuration/Variables.php @@ -139,4 +139,5 @@ interface Variables public const OTEL_PHP_AUTOLOAD_ENABLED = 'OTEL_PHP_AUTOLOAD_ENABLED'; public const OTEL_PHP_INTERNAL_METRICS_ENABLED = 'OTEL_PHP_INTERNAL_METRICS_ENABLED'; //whether the SDK should emit its own metrics public const OTEL_PHP_DISABLED_INSTRUMENTATIONS = 'OTEL_PHP_DISABLED_INSTRUMENTATIONS'; + public const OTEL_PHP_EXCLUDED_URLS = 'OTEL_PHP_EXCLUDED_URLS'; } diff --git a/src/SDK/SdkAutoloader.php b/src/SDK/SdkAutoloader.php index 246f578cd..d901cf8b7 100644 --- a/src/SDK/SdkAutoloader.php +++ b/src/SDK/SdkAutoloader.php @@ -31,7 +31,7 @@ public static function autoload(): bool //invalid setting, assume false self::$enabled = false; } - if (!self::$enabled) { + if (!self::$enabled || self::isIgnoredUrl()) { return false; } Globals::registerInitializer(function (Configurator $configurator) { @@ -69,6 +69,30 @@ public static function autoload(): bool return true; } + /** + * Test whether a request URI is set, and if it matches the excluded urls configuration option + * + * @internal + */ + public static function isIgnoredUrl(): bool + { + $ignoreUrls = Configuration::getList(Variables::OTEL_PHP_EXCLUDED_URLS, []); + if ($ignoreUrls === []) { + return false; + } + $url = $_SERVER['REQUEST_URI'] ?? null; + if (!$url) { + return false; + } + foreach ($ignoreUrls as $ignore) { + if (preg_match(sprintf('|%s|', $ignore), $url) === 1) { + return true; + } + } + + return false; + } + /** * @internal */ diff --git a/tests/Unit/SDK/SdkAutoloaderTest.php b/tests/Unit/SDK/SdkAutoloaderTest.php index 51cb66df4..598a41770 100644 --- a/tests/Unit/SDK/SdkAutoloaderTest.php +++ b/tests/Unit/SDK/SdkAutoloaderTest.php @@ -69,4 +69,59 @@ public function test_enabled_by_configuration(): void $this->assertNotInstanceOf(NoopTracerProvider::class, Globals::tracerProvider()); $this->assertNotInstanceOf(NoopLoggerProvider::class, Globals::loggerProvider()); } + + public function test_ignore_urls_without_request_uri(): void + { + $this->setEnvironmentVariable(Variables::OTEL_PHP_AUTOLOAD_ENABLED, 'true'); + $this->setEnvironmentVariable(Variables::OTEL_PHP_EXCLUDED_URLS, '*'); + unset($_SERVER['REQUEST_URI']); + $this->assertFalse(SdkAutoloader::isIgnoredUrl()); + } + + /** + * @dataProvider ignoreUrlsProvider + */ + public function test_ignore_urls(string $ignore, string $uri, bool $expected): void + { + $this->setEnvironmentVariable(Variables::OTEL_PHP_AUTOLOAD_ENABLED, 'true'); + $this->setEnvironmentVariable(Variables::OTEL_PHP_EXCLUDED_URLS, $ignore); + $_SERVER['REQUEST_URI'] = $uri; + $this->assertSame($expected, SdkAutoloader::isIgnoredUrl()); + } + + public static function ignoreUrlsProvider(): array + { + return [ + [ + 'foo', + '/foo?bar=baz', + true, + ], + [ + 'foo', + '/bar', + false, + ], + [ + 'foo,bar', + 'https://example.com/bar?p1=2', + true, + ], + [ + 'foo,bar', + 'https://example.com/baz?p1=2', + false, + ], + [ + 'client/.*/info,healthcheck', + 'https://site/client/123/info', + true, + ], + [ + 'client/.*/info,healthcheck', + 'https://site/xyz/healthcheck', + true, + ], + ]; + } }