Updated on 2026-01-30 GMT+08:00

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

  1. Prepare PHP 8.0+, PECL, and Composer.
  2. Run the installation command:
    apt update 
    apt install -y php-pear 
    apt install composer
  3. Verify the environment:
    pecl version 
    composer --version

Demo

  1. Initialize the demo application.
    In the empty project directory, initialize the composer.json file.
    composer init \   
    --no-interaction \   
    --require slim/slim:"^4" \   
    --require slim/psr7:"^1"  
    
    composer update
  2. 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();
  3. Run the demo application:
    php -S localhost:8080 & 
    curl http://localhost:8080/rolldice

Non-Intrusive Access

  1. Install the required build tool:

    sudo apt-get install gcc make autoconf

  2. 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.

  3. Enable the extended component.

    1. Locate the php.ini configuration file loaded in the current environment:
      php --ini | grep "Configuration File"
    2. Add the following content to the PHP file:
      [opentelemetry]
      extension=opentelemetry.so[
      [PHP]
      extensionxtension=grpc.so
      extensionextension=protobuf.so

  4. Perform verification:

    php --ri opentelemetry
    php --ri grpc
    php --ri protobuf

  5. 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

  6. 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 &

  7. Trigger a request:

    curl -kv http://localhost:8080/rolldice

  8. Log in to the APM console.
  9. Click on the left and choose Management & Governance > Application Performance Management.
  10. In the navigation pane, choose Link Trace > Metrics.
  11. In the tree on the left, locate an environment and then go to the URL tab page to view the URL calling data.
  12. In the navigation pane, choose Link Trace > Tracing.