-
Notifications
You must be signed in to change notification settings - Fork 187
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
[opentelemetry-php-contrib] laravel: When loading configuration files, SDK ignores laravel's .env #1436
Comments
The instrumentation is bootstrapped before Laravel, so the .env configuration will not work. |
Got it. I asked around and https://github.com/vlucas/phpdotenv package has been suggested. It is already been used by laravel too. I added these lines before EnvironmentResolver() so values $dotenv = Dotenv::createImmutable($_ENV['HOME']);
$dotenv->load(); Once the values are loaded, EnvironmentResolver() works as usual. One thing I am unsure is how universal is the key of |
Configuration must be provided via You need to move your |
EDIT: I was wrong, it's possible to use boolean variables in php.ini, they just need to be quoted, so Just mentioning, that because #1442 it's partially impossible to instrument laravel now in a meaningful way without some tinkering. OpenTelemetry instrumentation is loaded and configured in composer autoloader, so it's either env or php.ini, and php.ini doesn't work right now – there is a possibility to create a separate autoloader just for vlucas/phpdotenv to configure opentelemetry in a way that's consistent with Laravel way, but this isn't trivial as the Dotenv class itself has it's fair share of dependencies, so it would be reinventing a wheel. The issue with environment variables is, that they aren't passed as they are in some cases (cronjobs aren't passing env at all while spawning child process https://github.com/laravel/framework/blob/11.x/src/Illuminate/Console/Scheduling/Event.php#L198), I cannot be 100% sure about Horizon (although it should work), and some component in tests (AFAIR testbench or phpunit) while passing variables is casting boolean to string of value Only 100% working solution for all Laravel components right now is to prepare a separate initialization script with contents similar to https://github.com/open-telemetry/opentelemetry-php/blob/main/examples/load_config_env.php#L11 and include it at the begining of |
This doesn't sound like an issue with instrumentation. If you are calling
|
@gmhafiz - is this resolved? |
For production, our option is to hardcode the required variables into nginx virtual host file in order to not to contaminate other applications. for dev environment, this is less of an issue because many of use use docker compose to set the required variables. |
Describe your environment
OS: linux debian 12
php: 8.2
laravel: v11
open-telemetry/opentelemetry-auto-laravel: 0.0.28
open-telemetry/sdk: 1.1.2
Steps to reproduce
This is after a normal laravel installation instrumented with
open-telemetry/opentelemetry-auto-laravel
package. There are two ways of running laravel. Method 1 works withphp artisan serve
. Method 2 using php-fpm does not read environment variable.Get it and make a copy of
.env
file.Method 1:
Run with
Monitor log for traceID
Perform a request
Method 2
Run laravel using nginx and php-fpm
Perform a request
What is the expected behavior?
In both methods, traceID is present. For example.
What is the actual behavior?
Method 1 has traceID, but in method 2, otel is not enabled (uses noop) and traceID becomes 0000000000000000000000000000000
Additional context
Even though both methods have
OTEL_PHP_AUTOLOAD_ENABLED=true
set in.env
file, only in the first method otel is enabled.I am unsure the mechanism of
php artisan serve
is picking up values from.env
In method 2, what I think happened was the SDK tries to load values using both
$_SERVER
(from EnvironmentResolver.php) andgetenv()
. AndOTEL_PHP_AUTOLOAD_ENABLED
variable is false at this point for some reason.What I have done, or try to attempt:
server/vhost.conf
like below and it works. but I am unsure about hard coding values like these.server/php.ini
will work too because ofPhpIniResolver.php
, as advised by https://opentelemetry.io/docs/zero-code/php/#phpini-configuration. I want to avoid this method as it will affect all php applications.While both 1) and 2) works, ideally I want this package to load values using environment variables. But no otel variables are loaded because values set in
.env
file are not read by the SDK.Something could be done to
open-telemetry/opentelemetry-auto-laravel
package to load values from laravel's.env
using helper function such asenv()
.Looking around, many laravel packages publish a config file under
config
directory. For example inconfig/database.php
, DB_CONNECTION is read usingenv()
helper function.Laravel reads this config using a
config()
magic function and uses {{filename.key}} magic string as parameter. For example callingconfig('database.default')
returns the default value set in.env
which is 'sqlite'.Suggestion
Perhaps
open-telemetry/opentelemetry-auto-laravel
could also publish a config file and then allow it to load those values on top of bothEnvironmentResolver.php
andPhpIniResolver.php
loaded byopen-telemetry/sdk
package.This new config file should be limited only for
open-telemetry/opentelemetry-auto-laravel
package. What do you think?The text was updated successfully, but these errors were encountered: