I downloaded the OpenTelemetry Windows DLL, and referenced it in the php.ini file.
php -mi
shows the opentelemetry has been loaded.
php -mi | grep telemetry
opentelemetry
php -r
shows the function exists:
php -r "print_r(get_extension_funcs('opentelemetry'));"
Array
(
[0] => OpenTelemetryInstrumentationhook
)
In PhpStorm’s PHP Interpreter also configured the exact PHP, as when I execute phpinfo
from PhpStorm’s run button, it shows the opentelemetry in loaded. I can also get the tracing data when run the code in PhpStorm which imply the extension works.
But why PhpStorm does not recognize the function in the extension?
2
Answers
Thanks to LazyOne's answer detailed explanation.
I tried config the stub path in PhpStorm, but it not works, even when I reopen the PhpStorm or reload the interpreter.
Finally I make a
.phpstorm.stubs
at the root directory of my project, and add the opentelemetry stub file as refered in answer. Then it works.PhpStorm uses stub files for providing code completion (and static analysis) for functionality provided by custom PHP extensions/modules (that come from
.dll
/.so
files).Stub file is a limited PHP version of those classes/methods/functions/etc with (optional) full PHPDoc block but empty bodies. Overall it’s something similar to what
.h
files are in C. You can Ctrl + Click on any standard PHP class/function (e.g.MySQL
orstr_replace()
) and see yourself how it’s done (the IDE will open corresponding stub file in another editor tab). A bit more: https://stackoverflow.com/a/35923482/783119PhpStorm keeps all known-to-them/made-by-them stubs in their main repository: https://github.com/JetBrains/phpstorm-stubs. It is bundled with every PhpStorm version.
It already contains the stub for OpenTelemetry: https://github.com/JetBrains/phpstorm-stubs/blob/master/opentelemetry/opentelemetry.php
I do not see the
opentelemetry
entry in the GUI yet in my current stable 2023.2.4 version (Settings/Preferences | PHP | PHP Runtime
) — https://www.jetbrains.com/help/phpstorm/2023.2/php.html#php-runtime-tab. This suggests that it will be bundled in the next major 2023.3 version only (to be release soon: next week or two). Unless I cannot just locate it there…What you can do right now:
Try the latest 2023.3 Beta build and locate and enable that extension stubs there (it should be there): https://www.jetbrains.com/phpstorm/nextversion/
Using the current PhpStorm version: Get the own copy of the latest stubs repository and specify the path at
Settings/Preferences | PHP | PHP Runtime | Advanced settings | Default stubs path
.Be careful with this one though: do not forget to clear it and switch back to the bundled version when you upgrade to 2023.3 version (outdated stubs can cause other issues — providing wrong signature of the method/not recognizing newly added classes/methods etc). Or ensure to maintain the latest version of the stubs there yourself.
Using any PhpStorm version: Just get that single opentelemetry.php stub file and place it anywhere in your project so it gets indexed: the IDE will use it for code completion.
The same: when you upgrade to 2023.3 or newer version then it’s better to remove such a custom stub file and switch to the bundled stubs.