Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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"
]
}
}
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 (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.

\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