Retrieve values for env var from $_ENV in addition to $_SERVER.

Created on 19 June 2024, 6 months ago

Use getEnv in addition to $_SERVER var

Steps to reproduce

We package our application with docker and variabilize some spΓ©cific var with .env files.

Proposed resolution

It could be handy to use getEnv with retrieving value for SENTRY_DSN, SENTRY_ENVIRONNEMENT and SENTRY_RELEASE for ENV vars.

Remaining tasks

User interface changes

none

API changes

None

Data model changes

Non

✨ Feature request
Status

Active

Version

6.0

Component

Code

Created by

πŸ‡«πŸ‡·France nguerinet

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @nguerinet
  • Pipeline finished with Failed
    6 months ago
    Total: 148s
    #203008
  • Pipeline finished with Failed
    6 months ago
    Total: 182s
    #203017
  • Status changed to Postponed: needs info 6 months ago
  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    It seems you are conflating $_ENV in the title and getenv() 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 or vlucas/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 and vlucas/phpdotenv don't use putenv() or getenv(). 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.

  • Pipeline finished with Failed
    6 months ago
    Total: 153s
    #203020
  • Pipeline finished with Failed
    6 months ago
    Total: 162s
    #203028
  • Pipeline finished with Failed
    6 months ago
    Total: 153s
    #203032
  • Pipeline finished with Failed
    6 months ago
    #203035
  • Pipeline finished with Failed
    6 months ago
    Total: 157s
    #203050
  • Pipeline finished with Failed
    6 months ago
    Total: 153s
    #203065
  • Pipeline finished with Failed
    6 months ago
    Total: 144s
    #203177
  • πŸ‡«πŸ‡·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.

  • Pipeline finished with Failed
    6 months ago
    Total: 152s
    #203186
  • πŸ‡ΊπŸ‡ΈUnited States mfb San Francisco

    Are you using symfony/dotenv or vlucas/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.

  • Pipeline finished with Failed
    6 months ago
    Total: 154s
    #203188
  • Pipeline finished with Failed
    6 months ago
    Total: 156s
    #203191
  • Pipeline finished with Success
    6 months ago
    Total: 215s
    #203195
  • πŸ‡«πŸ‡·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
  • πŸ‡ΊπŸ‡Έ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.

  • πŸ‡«πŸ‡·France nguerinet

    Sorry here the compose file

  • πŸ‡ΊπŸ‡Έ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."

Production build 0.71.5 2024