- Issue created by @nguerinet
- Status changed to Postponed: needs info
6 months ago 3:50pm 19 June 2024 - πΊπΈUnited States mfb San Francisco
It seems you are conflating
$_ENV
in the title andgetenv()
in the merge request, but these are actually two distinct mechanisms.In any case, I'm hesitant to make such a change for a few reasons.
For one, it's a good idea to use one of the PHP packages dedicated to managing environment variables - either
symfony/dotenv
orvlucas/phpdotenv
. These packages can read your environment variables from a file and make them available via$_SERVER
, where they would also appear if you were using "real" environment variables (the "recommended" PHP configs actually leave$_ENV
empty as a performance micro-optimization).In certain environments where multiple PHP requests (perhaps from different apps entirely) share the same process, e.g. because of usage of threads, it's not safe to call
putenv()
/getenv()
because the environment variable will be set/retrieved for the entire process, not just the request in question. By default,symfony/dotenv
andvlucas/phpdotenv
don't useputenv()
orgetenv()
. So, it's generally not a good idea to encourage use of these functions.The logic to look for environment variables only in
$_SERVER
actually comes from upstream, in the Sentry PHP SDK. So, it seems I'm not alone in my opinions on this.So my preference would be that if you're running in to this issue, rather than make a change in this module, you should either use one of the dotenv packages or, if using custom code, set these environment variables via
$_SERVER
so it's compatible with this module.But if there's any reason that one of these alternatives would be difficult, I'd like to hear about it.
- π«π·France nguerinet
Thanks fr for the clarification with $_ENV and getenv().
My purpose is not to replace the logic with $_SERVER but to also use $_ENV if needed.
The default configuration for PHP for variable order is EGPCS.
You could have the same issue if you run an application with an empty string for that configuration.Have a value in $_SERVER or $_ENV is the same if you have multiple application running on a same process.
In this case I'm not sure the issue is where the env var are set but that you have different app that runs on the same process and could access to those values.Sure I could use another module in order to add all my env variable to $_SERVER but I think it's dumb to add a complete module for 3 variables.
My need is that I run my apps with docker. All my per environment configuration are managed thru .env passed to docker.
Unfortunately, when I use this configuration env vars are not included in $_SERVER but in $_ENV.I change my code to use $_ENV instead of getenv. I also mutualise those settings in one static class.
- πΊπΈUnited States mfb San Francisco
Are you using
symfony/dotenv
orvlucas/phpdotenv
to setup your environment variables? My understanding of both of those packages is that they should populate$_SERVER
.If you're using some other code to deal with environment variables, what is that code? Can you configure/change it to populate
$_SERVER
, at least for these variables?This module is basically just a wrapper around the Sentry PHP SDK for easier integration with Drupal. If you weren't using this module, you'd have the same issue with the SDK only checking
$_SERVER
for environment variables, so I'd prefer not to have a lot of extra code for an edge case where environment variables can't be found there. - π«π·France nguerinet
I agree, I could add some code to provide value from $_ENV to $_SERVER.
For all other env var I need I use them in custom module and use $_ENV.
I complete the PR with test. Don't want to keep a non working branch even if you trash it.
- πΊπΈUnited States mfb San Francisco
@nguerinet not trying to "trash" your merge request, but trying to figure out if there's a need for this extra logic in the module, or if you could resolve by using one of the dotenv libraries and/or tweaking your custom code. If it *is* needed, then arguably it would also be needed upstream in the Sentry SDK.
- Status changed to Closed: cannot reproduce
5 months ago 7:44pm 30 July 2024 - πΊπΈUnited States mfb San Francisco
Closing since I don't have a clear explanation for why we also need to look for environment variables in
$_ENV
But there may very well be a good reason (common use case, etc.), so feel free to re-open this issue if providing more info.
- π«π·France nguerinet
Hi
I usualy work with sentry and symfony on docker environment.
As using docker I manage some of my secret with .env file and sentry used it without custom code.Globaly when using docker with environnement variable with php official image for example, all variables mentions in .env files are not available in your $_SERVER but in $_ENV.
For sure I could use a dotenv module or put some code inside my settings file, but I think that's it would be nice to use raven without adding another module or code to handle configuration.
- πΊπΈUnited States mfb San Francisco
Globaly when using docker with environnement variable with php official image for example, all variables mentions in .env files are not available in your $_SERVER but in $_ENV.
Thanks @nguerinet that's good to know. I'm still fuzzy on when/how variables are missing from $_SERVER, e.g. if I make an .env file and run
docker run --env-file ./env php:8.3-cli -r "echo \$_SERVER['SENTRY_ENVIRONMENT'];"
the SENTRY_ENVIRONMENT variable is printed. - π«π·France nguerinet
Hi,
Yes it works with php cli but when using it with apache it does not.
In order to reproduce it try this.
Put the two files in a folder and run docker compose up -d and go to localhost:85.
You could see that $_SERVER['SENTRY_ENVIRONMENT'] does not exist.
- πΊπΈUnited States mfb San Francisco
You will have to explicity tell the php:8.3-apache docker container to pass an environment variable into $_SERVER
Create or mount a file in the docker container: /etc/apache2/conf-enabled/env.conf
This file should read
PassEnv SENTRY_ENVIRONMENT
Restart and the SENTRY_ENVIRONMENT environment variable should now be available in $_SERVER
- π«π·France nguerinet
I never said that you can't change what is in the $_SERVER; my intention is to use the $_ENV values in addition to the $_SERVER.
There are plenty of workarounds; I just want to propose a simpler one that does not involve extra configuration when using Raven."