Skip to content
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

Reading boolean configuration values via php.ini is impossible due to architecture #1442

Open
gdaszuta opened this issue Nov 28, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@gdaszuta
Copy link

So because my application works with spawning some sub-processes in a manner that doesn't pass full environment verbatim, I've tried to move configuration to php.ini, and it does seem that it's impossible to do so in a consistent way.

The issue is, that some values are boolean:

    OTEL_SDK_DISABLED
    OTEL_PHP_INTERNAL_METRICS_ENABLED
    OTEL_PHP_AUTOLOAD_ENABLED
    OTEL_PHP_EXPERIMENTAL_AUTO_ROOT_SPAN

and php function https://www.php.net/manual/en/function.get-cfg-var.php used in OpenTelemetry\Config\SDK\Configuration\Environment\PhpIniEnvSource to read configuration from ini file has some undocumented, but consistent behaviour where any value considered by php developers as a 'truthy' is returned as string of value '1'.

The behaviour is apparently consistent between all php versions since at least 2013, probably since forever.

$ for PHP in 5.6 8.0 8.1 8.2 8.3 8.4 ; do 
    docker run -it --rm php:$PHP \
        php -d 'TEST_VALUE=true' -r 'echo phpversion() . " => " . json_encode(get_cfg_var("TEST_VALUE")) . "\n";' ;
done
5.6.40 => "1"
8.0.30 => "1"
8.1.31 => "1"
8.2.26 => "1"
8.3.14 => "1"
8.4.1 => "1"

This makes OpenTelemetry\SDK\Common\Configuration\Parser\BooleanParser always throw InvalidArgumentException, as only acceptable values are 'true' and 'false', log warning Invalid boolean value "%s" interpreted as "false" for %s', and not enable instrumentation.

This, unfortunately makes the php.ini way of configuration mentioned in documentation impossible, and as I can see in git history it was impossible since #870, so for good two years.

I see three ways to fix the issue – either revert the change to allow string "1" as a valid boolean value, reimplement php configuration parser to return verbatim configuration variable values, or drop this path of configuration altogether from documentation.

FYI @brettmc

@gdaszuta
Copy link
Author

So I did some poking around php internals and available solutions, and it does seem that reimplementation of get_cfg_var() is suprisingly the most sane solution:

function get_raw_cfg_var(string $option) {
    $ini_files = array_map("trim", explode(",", php_ini_scanned_files()));

    foreach ($ini_files as $file) {
        $values = parse_ini_file($file, false, INI_SCANNER_RAW );

        if(isset($values[$option])) {
            return $values[$option];
        }
    }

    return false;
}

It could be fractionally slower than the native function (wasn't in my tests, but in theory it could), but it's specification-friendly, working, and drop-in. I can prepare PR later, please just someone let me know if you like this approach.

@brettmc
Copy link
Collaborator

brettmc commented Nov 28, 2024

if it's not possible to quote the values in php.ini (eg TEST_VALUE="true" in the example above), then your proposed solution looks ok to me. I'm not concerned about it being slightly slower, it's not heavily used.

@gdaszuta
Copy link
Author

Ok, you got me here :) I was so focused on debugging the issue, I hadn't even consider quoting of this string could be mandatory.

I see one drawback of my solution – it won't work with parameters defined by php -d ..., and as far as I understand there is no way to gather runtime execution parameters from inside the script, so it also wouldn't be consistent. Maybe updating documentation would be the best way.

@brettmc
Copy link
Collaborator

brettmc commented Nov 29, 2024

Maybe updating documentation would be the best way.

Great! We could probably add some phpdoc to the ini processing class, as folks might look there. It's been some time since I went over our documentation over in opentelemetry.io, but it's very briefly mentioned in https://opentelemetry.io/docs/languages/php/sdk/#autoloading and maybe that section could be improved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants