-
Notifications
You must be signed in to change notification settings - Fork 214
Disable SDK from autoloading during composer execution. #1727
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
base: main
Are you sure you want to change the base?
Changes from all commits
9ec2a8c
c9f43e4
e3005fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\SDK\Common\Util; | ||
|
|
||
| use function basename; | ||
| use function getenv; | ||
| use function in_array; | ||
|
|
||
| final class ComposerHandler | ||
| { | ||
| public static function isRunning(): bool | ||
| { | ||
| /** | ||
| * This is set when composer is running a script; eg: | ||
| * composer run-script test-psr3 | ||
| * | ||
| * However, it is not set in the following case: | ||
| * composer test-psr3 | ||
| */ | ||
| if (getenv('COMPOSER_DEV_MODE')) { | ||
| return true; | ||
| } | ||
|
|
||
| if ( | ||
| ($entrypoint = $_SERVER['argv'][0] ?? '') === getenv('COMPOSER_BINARY') | ||
| || in_array(basename($entrypoint), ['composer', 'composer.phar'], true) | ||
| ) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,4 +2,17 @@ | |
|
|
||
| declare(strict_types=1); | ||
|
|
||
| \OpenTelemetry\SDK\SdkAutoloader::autoload(); | ||
| /** | ||
| * If OTEL_PHP_AUTOLOAD_ENABLED=true, there may be compatability issues | ||
| * if the version of PSR-3 installed in ./vendor conflicts with that of the | ||
| * packaged composer PSR-3 library. | ||
| * | ||
| * If COMPOSER_DEV_MODE is present, then we can assume that a composer script | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would happen if you were starting your application through a composer script? eg something like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously, if you were using a compatible version of If you were using an incompatible version, it would cause a fatal exception. With the proposed changes, the SDK is essentially disabled.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was really wondering whether we're breaking any legitimate use-cases...eg do folks use composer scripts to start and run an application? And if so, will they be surprised when instrumentation stops working? I'm happy to deal with that separately if it comes up. |
||
| * is running, and we can prevent the PSR-3 compatability issues by disabling | ||
| * the SDK from activating. | ||
| * | ||
| * @see https://github.com/open-telemetry/opentelemetry-php/issues/1673 | ||
| */ | ||
| if (\OpenTelemetry\SDK\Common\Util\ComposerHandler::isRunning() === false) { | ||
| \OpenTelemetry\SDK\SdkAutoloader::autoload(); | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace OpenTelemetry\Tests\Integration\Composer; | ||
|
|
||
| use Composer\Script\Event; | ||
|
|
||
| final class Psr3Compatability | ||
| { | ||
| public static function run(Event $event): void | ||
| { | ||
| require_once $event->getComposer()->getConfig()->get('vendor-dir') . '/autoload.php'; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Checking
class_exists(\Composer\Console\Application::class, false)is likely the most reliable way to detect whether we are running as composer command.