adding OTEL_PHP_EXCLUDED_URLS (#1226)
Modeled on python's feature to turn off all instrumentation for specific URLs, https://opentelemetry.io/docs/languages/python/automatic/agent-config/#excluded-urls this feature will effectively disable SDK autoloading if there is a request URI that matches one of the provided excluded URLs, and no-op implementations will be used for tracer/logger/meter providers.
This commit is contained in:
parent
91f5e20ffc
commit
36e2b7f613
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace OpenTelemetry\Example;
|
||||
|
||||
use OpenTelemetry\API\Globals;
|
||||
|
||||
/**
|
||||
* Define OTEL_PHP_EXCLUDED_URLS to include a pattern that matches
|
||||
* REQUEST_URI, which effectively disables the SDK autoloader for some URLs,
|
||||
* meaning no telemetry signals will be emitted.
|
||||
*/
|
||||
putenv('OTEL_PHP_AUTOLOAD_ENABLED=true');
|
||||
putenv('OTEL_TRACES_EXPORTER=console');
|
||||
putenv('OTEL_PHP_EXCLUDED_URLS=client/.*/info,healthcheck');
|
||||
$_SERVER['REQUEST_URI'] = 'https://example.com/v1/healthcheck';
|
||||
|
||||
require __DIR__ . '/../../../vendor/autoload.php';
|
||||
|
||||
$tracer = Globals::tracerProvider()->getTracer('demo');
|
||||
$tracer->spanBuilder('healthcheck')->startSpan()->end();
|
||||
|
|
@ -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';
|
||||
}
|
||||
|
|
|
|||
|
|
@ -31,7 +31,7 @@ class SdkAutoloader
|
|||
//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 @@ class SdkAutoloader
|
|||
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
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -69,4 +69,59 @@ class SdkAutoloaderTest extends TestCase
|
|||
$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,
|
||||
],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue