Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -207,5 +207,10 @@
"OpenTelemetry\\Tests\\Integration\\SDK\\Common\\Configuration\\TestEnvSourceProvider"
]
}
},
"scripts": {
"test-psr3": [
"OpenTelemetry\\Tests\\Integration\\Composer\\Psr3Compatability::run"
]
}
}
35 changes: 35 additions & 0 deletions src/SDK/Common/Util/ComposerHandler.php
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)
Copy link
Contributor

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.

) {
return true;
}

return false;
}
}
15 changes: 14 additions & 1 deletion src/SDK/_autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The 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 composer run-my-application ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previously, if you were using a compatible version of psr/log then the SDK would bootstrap.

If you were using an incompatible version, it would cause a fatal exception.


With the proposed changes, the SDK is essentially disabled.

Copy link
Contributor

Choose a reason for hiding this comment

The 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();
}
15 changes: 15 additions & 0 deletions tests/Integration/Composer/Psr3Compatability.php
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';
}
}
Loading