open-telemetry / opentelemetry-php

The OpenTelemetry PHP Library
https://opentelemetry.io/docs/instrumentation/php/
Apache License 2.0
756 stars 187 forks source link

Exporters not loaded when using auto root span #1410

Open xvilo opened 1 month ago

xvilo commented 1 month ago

Describe your environment

Steps to reproduce

What is the expected behavior? What did you expect to see? It either:

What is the actual behavior? It can't load the OLTP exporter as a whole

$ php -S localhost:8080 test_otel.php
[Thu Oct 17 08:56:52 2024] PHP 8.3.12 Development Server (http://localhost:8080) started
[Thu Oct 17 08:56:55 2024] [::1]:44470 Accepted
[Thu Oct 17 08:56:55 2024] OpenTelemetry: [warning] Error during opentelemetry initialization: Span exporter factory not defined for: otlp
#0 /my_project_location/vendor/open-telemetry/sdk/Trace/ExporterFactory.php(28): OpenTelemetry\SDK\Registry::spanExporterFactory()
#1 /my_project_location/vendor/open-telemetry/sdk/SdkAutoloader.php(87): OpenTelemetry\SDK\Trace\ExporterFactory->create()
#2 /my_project_location/vendor/open-telemetry/sdk/SdkAutoloader.php(62): OpenTelemetry\SDK\SdkAutoloader::environmentBasedInitializer()
#3 /my_project_location/vendor/open-telemetry/api/Globals.php(93): OpenTelemetry\SDK\SdkAutoloader::OpenTelemetry\SDK\{closure}()
#4 /my_project_location/vendor/open-telemetry/api/Globals.php(43): OpenTelemetry\API\Globals::globals()
#5 /my_project_location/vendor/open-telemetry/sdk/Trace/AutoRootSpan.php(39): OpenTelemetry\API\Globals::tracerProvider()
#6 /my_project_location/vendor/open-telemetry/sdk/SdkAutoloader.php(69): OpenTelemetry\SDK\Trace\AutoRootSpan::create()
#7 /my_project_location/vendor/open-telemetry/sdk/_autoload.php(5): OpenTelemetry\SDK\SdkAutoloader::autoload()
#8 /my_project_location/vendor/composer/autoload_real.php(43): require('...')
#9 /my_project_location/vendor/composer/autoload_real.php(47): {closure}()
#10 /my_project_location/vendor/autoload.php(25): ComposerAutoloaderInit075534cb7854c559e471b4339a4e7714::getLoader()
#11 /my_project_location/public/test_otel.php(26): require('...')
#12 {main} in /my_project_location/vendor/open-telemetry/api/Globals.php(95)
[Thu Oct 17 08:56:55 2024] [::1]:44470 Closing

Additional notes?

From my debugging this happens because: The auto root span _register.php is loader earlier in the chain then the _register.php of the oltp exporter, not sure if it's alphabetical or something else in composer? The auto root span kicks of everything in the sdk package, and then also tries to set up the tracer The tracer has an in memory list of registered exporters, which is usually filled through the _register.php scripts. As that didn't happen yet, the exporter does not exist (yet)

If it were to used as a class, e.g. a list of predefined FQCN's then it could work since it would kick in the class loader in the background.

So you either need to find a way to chain these dependencies (don't think there is a composer way), or, from the tracer factory stuffs, directly depend on a list of predefined classes as a fallback. That could work, but will give some overhead if no authoritative classmap is used

IAmAndre commented 4 weeks ago

Hi @xvilo . Has there been any update/workaround regarding this issue?

xvilo commented 4 weeks ago

Uhm, work is being done on this so it isn’t an issue anymore. (https://github.com/open-telemetry/opentelemetry-php/pull/1412) Another work around for now would be to change the order of the dependencies in your composer.json, and then to dump the autoloader again

IAmAndre commented 2 weeks ago

@xvilo Sorry for the delayed reply. What order would you recommend having, or which package(s) should be moved in the order? Thanks.