Reporting PHP Application Data Using OpenTelemetry
Huawei Cloud APM is compatible with OpenTelemetry and can directly receive the trace data reported using the OpenTelemetry SDK or Agent. This section describes how to use OpenTelemetry to connect a PHP application to APM and report trace data.
Constraint
For non-intrusive access, PHP 8.0 or later must be used.
Environment Preparation
- Prepare PHP 8.0+, PECL, and Composer.
- Run the installation command:
apt update apt install -y php-pear apt install composer
- Verify the environment:
pecl version composer --version
Demo
- Initialize the demo application.
- Compile the application code. Create an index.php file:
<?php use Psr\Http\Message\ResponseInterface as Response; use Psr\Http\Message\ServerRequestInterface as Request; use Slim\Factory\AppFactory; require __DIR__ . '/vendor/autoload.php'; $app = AppFactory::create(); $app->get('/rolldice', function (Request $request, Response $response) { $result = random_int(1,6); $response->getBody()->write(strval($result)); return $response; }); $app->run(); - Run the demo application:
php -S localhost:8080 & curl http://localhost:8080/rolldice
Non-Intrusive Access
- Install the required build tool:
sudo apt-get install gcc make autoconf
- Install the extended component using the build tool:
sudo apt-get install php-dev pecl install opentelemetry pecl install grpc //It takes about 20 to 30 minutes to install the component. pecl install protobuf-3.21.12 // Compatible with PHP 8.
Select the required plug-in version based on PHP version. For example, for PHP 8.0, use OpenTelemetry 1.1.3 or earlier and Protobuf 3.21.0 or a similar version.
- Enable the extended component.
- Locate the php.ini configuration file loaded in the current environment:
php --ini | grep "Configuration File"
- Add the following content to the PHP file:
[opentelemetry] extension=opentelemetry.so[ [PHP] extensionxtension=grpc.so extensionextension=protobuf.so
- Locate the php.ini configuration file loaded in the current environment:
- Perform verification:
php --ri opentelemetry php --ri grpc php --ri protobuf
- Install the required dependencies for OpenTelemetry auto tracking:
composer config allow-plugins.php-http/discovery false composer require open-telemetry/sdk composer require open-telemetry/exporter-otlp composer require open-telemetry/transport-grpc composer require open-telemetry/opentelemetry-auto-slim composer update
- Add the cloud service configuration parameters to environment variables and run the startup command:
env OTEL_EXPORTER_OTLP_ENDPOINT=http://xxx.xxx.xx.xxx:4317 \ OTEL_EXPORTER_OTLP_HEADERS=Authentication=LV***9l \ OTEL_EXPORTER_OTLP_PROTOCOL=grpc \ OTEL_PHP_AUTOLOAD_ENABLED=true \ OTEL_SERVICE_NAME=Application name.Component name.Environment name \ php -S localhost:8080 &
- Trigger a request:
curl -kv http://localhost:8080/rolldice

- Log in to the APM console.
- Click
on the left and choose Management & Governance > Application Performance Management. - In the navigation pane, choose Link Trace > Metrics.
- In the tree on the left, locate an environment and then go to the URL tab page to view the URL calling data.
- In the navigation pane, choose Link Trace > Tracing.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot