Skip to content

Commit

Permalink
adding OTEL_PHP_EXCLUDED_URLS (#1226)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
brettmc authored Feb 1, 2024
1 parent 91f5e20 commit 36e2b7f
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
22 changes: 22 additions & 0 deletions examples/traces/features/ignore_request_urls.php
Original file line number Diff line number Diff line change
@@ -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();
1 change: 1 addition & 0 deletions src/SDK/Common/Configuration/Variables.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
26 changes: 25 additions & 1 deletion src/SDK/SdkAutoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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
*/
Expand Down
55 changes: 55 additions & 0 deletions tests/Unit/SDK/SdkAutoloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
],
];
}
}

0 comments on commit 36e2b7f

Please sign in to comment.