
# 使用OpenTelemetry上报PHP应用数据
华为云APM兼容OpenTelemetry协议，支持直接接收通过OpenTelemetry SDK或Agent上报的链路追踪数据。本文介绍如何使用OpenTelemetry方式，将PHP语言的应用接入APM，并上报链路追踪指标数据。
#### 限制条件
非侵入式接入，需使用PHP 8.0以及以上版本。
#### 环境准备
1. 准备插件/工具：PHP 8.0+、PECL、composer。
2. 安装，命令如下：
   ```
   apt update 
   apt install -y php-pear 
   apt install composer
   ```
   
3. 环境验证，命令如下：
   ```
   pecl version 
   composer --version
   ```
   
 
#### 示例demo
1. 初始化demo应用。
   在空白的项目目录中，初始化一个简单的composer.json文件。
   ```
   composer init \   
   --no-interaction \   
   --require slim/slim:"^4" \   
   --require slim/psr7:"^1"  
   composer update
   ```
   
2. 编写应用代码。创建index.php文件，内容如下：
   ```
   <?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. 运行程序，命令如下：
   ```
   php -S localhost:8080 & 
   curl http://localhost:8080/rolldice
   ```
   
 
#### 非侵入式接入
1. 安装必须构建工具，命令如下： 
   ```
   sudo apt-get install gcc make autoconf
   ```
   
   
2. 使用构建安装扩展组件，命令如下： 
   ```
   sudo apt-get install php-dev 
   pecl install opentelemetry  
   pecl install grpc   //安装编译时间较长（20-30分钟），请耐心等待 
   pecl install protobuf-3.21.12  //与 PHP 8兼容
   ```
   根据php需要选择对应的插件版本，比如php8.0，opentelemetry需要选择1.1.3或者之前的版本，protobuf 使用3.21.0相近的版本。
   
   
3. 启用扩展组件。 
   1. 定位当前环境加载的php.ini配置文件路径，命令如下：
      ```
      php --ini | grep "Configuration File"
      ```
      
   
   2. 在php中添加以下内容：
      ```
      [opentelemetry]
      extension=opentelemetry.so[
      [PHP]
      extensionxtension=grpc.so
      extensionextension=protobuf.so
      ```
      
   
   
   
   
4. 验证命令如下： 
   ```
   php --ri opentelemetry
   php --ri grpc
   php --ri protobuf
   ```
   
   
5. 安装服务框架所需的opentelemetry自动埋点的依赖，命令如下： 
   ```
   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. 添加云服务配置参数为环境变量中并启动，命令如下： 
   ```
   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=应用名称.组件名称.环境名称 \
   php -S localhost:8080 &
   ```
   
   
7. 触发请求，命令如下： 
   ```
   curl -kv http://localhost:8080/rolldice
   ```
   ![](https://support.huaweicloud.com/bestpractice-apm2/zh-cn_image_0000002511956224.png "点击放大")
   
   
8. 登录[APM控制台](https://auth.huaweicloud.com/authui/login.html?service=https://console.huaweicloud.com/console/#/login)。
9. 单击左侧![](https://support.huaweicloud.com/bestpractice-apm2/zh-cn_image_0000001542238060.png)，选择"管理与监管\> 应用性能管理 APM"，进入APM服务页面。
10. 在左侧导航栏选择"链路追踪 \> 指标"。
11. 在界面左侧树单击环境，单击"接口调用"，切换至接口调用页签，在接口调用页签可以查看接口调用的数据。 
    ![](https://support.huaweicloud.com/bestpractice-apm2/zh-cn_image_0000002511796232.png "点击放大")
    
    
12. 在左侧导航栏选择"链路追踪 \> 调用链"，查看调用链信息。 
    ![](https://support.huaweicloud.com/bestpractice-apm2/zh-cn_image_0000002543356161.png "点击放大")
    
    
 
