Skip to content

Conversation

@ChrisLightfootWild
Copy link
Contributor

Proposal to prevent the SDK from auto-loading during composer script execution.

During a composer script, the packaged dependencies in the composer.phar are currently locked to PSR-3 v2 and thus any local application dependencies which expect v3 can trigger a fatal error.

By preventing the SdkAutoloader from being auto-loaded, we can hopefully avoid the fatal error from triggering.

Fixes #1673.


To test, we can perform the following actions:

make update PHP_VERSION=8.3

make bash PHP_VERSION=8.3

OTEL_PHP_AUTOLOAD_ENABLED=true composer run-script test-psr3

Before:

> OpenTelemetry\Tests\Integration\Composer\Psr3CompatabilityTest::run

Fatal error: Declaration of Psr\Log\AbstractLogger::emergency(Stringable|string $message, array $context = []): void must be compatible with Psr\Log\LoggerInterface::emergency($message, array $context = []) in /usr/src/myapp/vendor/psr/log/src/LoggerTrait.php on line 18
echo $?
255

After:

> OpenTelemetry\Tests\Integration\Composer\Psr3CompatabilityTest::run
echo $?
0

@ChrisLightfootWild ChrisLightfootWild requested a review from a team as a code owner October 10, 2025 15:26
@codecov
Copy link

codecov bot commented Oct 10, 2025

Codecov Report

❌ Patch coverage is 0% with 9 lines in your changes missing coverage. Please review.
✅ Project coverage is 68.25%. Comparing base (ddb222d) to head (e3005fb).
⚠️ Report is 12 commits behind head on main.

Files with missing lines Patch % Lines
src/SDK/Common/Util/ComposerHandler.php 0.00% 7 Missing ⚠️
src/SDK/_autoload.php 0.00% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff              @@
##               main    #1727      +/-   ##
============================================
- Coverage     68.37%   68.25%   -0.12%     
- Complexity     2971     2974       +3     
============================================
  Files           448      449       +1     
  Lines          9020     8720     -300     
============================================
- Hits           6167     5952     -215     
+ Misses         2853     2768      -85     
Flag Coverage Δ
8.1 68.03% <0.00%> (-0.03%) ⬇️
8.2 68.22% <0.00%> (-0.03%) ⬇️
8.3 68.22% <0.00%> (+<0.01%) ⬆️
8.4 68.23% <0.00%> (-0.02%) ⬇️
8.5 68.21% <0.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
src/SDK/_autoload.php 0.00% <0.00%> (ø)
src/SDK/Common/Util/ComposerHandler.php 0.00% <0.00%> (ø)

... and 216 files with indirect coverage changes


Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ddb222d...e3005fb. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

* 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.

*
* @see https://github.com/open-telemetry/opentelemetry-php/issues/1673
*/
if (getenv('COMPOSER_DEV_MODE') === false) {
Copy link
Contributor

@Nevay Nevay Oct 17, 2025

Choose a reason for hiding this comment

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

Suggested change
if (getenv('COMPOSER_DEV_MODE') === false) {
if (getenv('COMPOSER_BINARY') === false) {

COMPOSER_DEV_MODE is only set by the run-script command, running scripts directly still fails:

OTEL_PHP_AUTOLOAD_ENABLED=true composer test-psr3

Edit: checking solely for COMPOSER_BINARY will be too strict as this environment variable is also propagated to @php scripts.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Nevay Ah, that is unfortunate - thanks for highlighting. Realistically, there may be no safe way to do this then? 🤔

Copy link
Contributor

Choose a reason for hiding this comment

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

Why not start with COMPOSER_DEV_MODE, since that covers some failure cases? It could be broken out into its own class+function such as ComposerHandler::isRunning() to allow for future expansion of methods to identify whether composer is running?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've made the suggested change to move to a ComposerHandler::isRunning() check.

I have also added an additional rudimentary check to try to infer whether or not the composer executable has been called directly.

This does fix the problem with running this way:

OTEL_PHP_AUTOLOAD_ENABLED=true composer test-psr3

Are there any foreseen problems in doing these additional checks?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I also noted a discussion started here which links to this issue.

@bobstrecansky
Copy link
Contributor

@Nevay / @brettmc - can y'all take a look at this when you get a moment?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Psr\Log\AbstractLogger Fatal Error

4 participants