未验证 提交 a497824e 编写于 作者: 何延龙 提交者: GitHub

Added memcache collection and skywalking 8.0 support

Added memcache collection and skywalking 8.0 support
上级 ccb58445
......@@ -9,10 +9,9 @@ SkyAPM PHP
## Documents
* [Documents in English](docs/README.md)
* [中文文档](docs/README_ZH.md)
## Docker image
Go to Docker hub -> [SkyAPM PHP](https://hub.docker.com/r/skyapm/skywalking-php)
Go to Docker hub -> [https://hub.docker.com/r/skyapm/skywalking-php](https://hub.docker.com/r/skyapm/skywalking-php)
```shell script
docker run -d -e SW_AGENT_COLLECTOR_BACKEND_SERVICES=127.0.0.1:11800 skywalking-php
```
......@@ -34,6 +33,7 @@ Host in Beijing. Go to [demo](http://106.75.237.45:8080/).
1. GRPC Client ([GRPC](https://github.com/grpc/grpc-php))
1. Predis Client ([Predis](https://packagist.org/packages/predis/predis))
1. Redis Extension ([Redis Extension](https://github.com/phpredis/phpredis))
1. Memcache Extension
## Contact Us
* Submit an [issue](https://github.com/SkyAPM/SkyAPM-php-sdk/issues)
......
.idea
\ No newline at end of file
# PHP SkyWalking Class: SkyWalking PHP client
---
- [Installation](#installation)
- [Requirements](#requirements)
- [Quick Start and Examples](#quick-start-and-examples)
---
### Installation
To install PHP SkyWalking Class, simply:
### Requirements
PHP Curl Class works with PHP 5.2 - 7.9.99 , and HHVM.
### Quick Start and Examples
// must put these code at the beginning !!!
// that would auto register shutdown function !!!
include_once ("./sdk-php/SkyWalking.php");// or use composer
//LOG_PATH is skywalking's logfile path
SkyWalking::getInstance("api")->setLogPath(LOG_PATH)->setSamplingRate(5);
....
....
$ch = curl_init();
curl_setopt($ch);
....
....
SkyWalking::getInstance()->startSpanOfCurl("www.api.com", $headers);
....
....
curl_setopt($ch);
$rs = curl_exec($ch);
SkyWalking::getInstance()->endSpanOfcurl($ch);
....
....
\ No newline at end of file
# PHP SkyWalking 类: SkyWalking PHP 客户端
---
- [安装](#installation)
- [要求](#requirements)
- [快速开始实例](#quick-start-and-examples)
---
### Installation
使用安装 SkyWalking 类, 操作:
### Requirements
PHP Curl Class works with PHP 5.2 - 7.9.99 , and HHVM.
### Quick Start and Examples
// must put these code at the beginning !!!
// that would auto register shutdown function !!!
include_once ("./sdk-php/SkyWalking.php");// or use composer
//LOG_PATH is skywalking's logfile path
SkyWalking::getInstance("api")->setLogPath(LOG_PATH)->setSamplingRate(5);
....
....
$ch = curl_init();
curl_setopt($ch);
....
....
SkyWalking::getInstance()->startSpanOfCurl("www.api.com", $headers);
....
....
curl_setopt($ch);
$rs = curl_exec($ch);
SkyWalking::getInstance()->endSpanOfcurl($ch);
....
....
\ No newline at end of file
此差异已折叠。
<?php
/**
* 其他项目调用此项目会传递类似 HEADER 信息
* $_SERVER['HTTP_SWTRACECONTEXT'] = 'Segment1504173645811.59a7de4dc618f.14768.0.0.127.0.0.1|59a7de4dc61c5|api|baidu.com|Segment1504173645811.59a7de4dc61b0.14768.0.0.127.0.0.1|1';
*
*/
include_once ("./SkyWalking.php");
SkyWalking::getInstance("mapi");
/**
* 定义日志目录,目前传递数据使用log方式
* 设置log写入路径的方法:
*/
define('LOG_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR . 'log');
SkyWalking::getInstance("api")->setLogPath(LOG_PATH)->setSamplingRate(5);
//发起B请求
sendA();
//发起B请求
sendB();
//发起B请求
sendC();
function sendA(){
/** start 应用本身的业务代码 **/
$headers = array(
"Content-type: text/xml",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Cache-Control: no-cache",
"Pragma: no-cache",
);
/** end 应用本身的业务代码 **/
SkyWalking::getInstance()->startSpanOfCurl("api.com", $headers);
//var_dump($headers);
/** start 应用本身的业务代码 **/
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.api.com/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_HTTPHEADER,$headers);
curl_setopt($curl, CURLOPT_HEADER, 0);
$result = curl_exec($curl);
$errno = curl_errno($curl);
$error = curl_error($curl);
/** end 应用本身的业务代码 **/
SkyWalking::getInstance()->endSpanOfcurl($curl);
/** start 应用本身的业务代码 **/
curl_close($curl);
/** end 应用本身的业务代码 **/
}
function sendB(){
/** start 应用本身的业务代码 **/
$headers = array(
"Content-type: text/xml",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Cache-Control: no-cache",
"Pragma: no-cache",
);
/** end 应用本身的业务代码 **/
SkyWalking::getInstance()->startSpanOfCurl("api.com", $headers);
//var_dump($headers);
/** start 应用本身的业务代码 **/
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.api.com/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_HTTPHEADER,$headers);
curl_setopt($curl, CURLOPT_HEADER, 0);
$result = curl_exec($curl);
$errno = curl_errno($curl);
$error = curl_error($curl);
/** end 应用本身的业务代码 **/
SkyWalking::getInstance()->endSpanOfcurl($curl);
/** start 应用本身的业务代码 **/
curl_close($curl);
/** end 应用本身的业务代码 **/
}
function sendC(){
/** start 应用本身的业务代码 **/
$headers = array(
"Content-type: text/xml",
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Cache-Control: no-cache",
"Pragma: no-cache",
);
/** end 应用本身的业务代码 **/
SkyWalking::getInstance()->startSpanOfCurl("api.com", $headers);
//var_dump($headers);
/** start 应用本身的业务代码 **/
/** start 应用本身的业务代码 **/
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, 'http://www.api.com/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl,CURLOPT_HTTPHEADER,$headers);
curl_setopt($curl, CURLOPT_HEADER, 0);
$result = curl_exec($curl);
$errno = curl_errno($curl);
$error = curl_error($curl);
/** end 应用本身的业务代码 **/
SkyWalking::getInstance()->endSpanOfcurl($curl);
/** start 应用本身的业务代码 **/
curl_close($curl);
/** end 应用本身的业务代码 **/
}
\ No newline at end of file
......@@ -20,7 +20,7 @@ func main() {
app := cli.NewApp()
app.Name = "sky_php_agent"
app.Usage = "the skywalking trace sending agent"
app.Version = "3.2.8"
app.Version = "3.3.0"
app.Flags = []cli.Flag{
&cli.StringSliceFlag{Name: "grpc", Usage: "SkyWalking collector address", Value: cli.NewStringSlice("127.0.0.1:11800")},
&cli.StringFlag{Name: "socket", Usage: "Pipeline for communicating with PHP", Value: "/tmp/sky-agent.sock"},
......
......@@ -2,4 +2,4 @@
* Quick Start
* [Quick start](quick-start.md)
* [Install agent](install-agent.md)
* [Install](install.md)
文档
=====
* [更改记录](./zh/change-log.md)
* [安装php扩展](./zh/install-sdk.md)
* [启动agent](./zh/start-agent.md)
* [常见问题](./zh/qa.md)
\ No newline at end of file
Tips: It is recommended that you use SkyWalking and use nginx as load balancing
Tips: It is recommended to use nginx as the load balancer of SkyWalking-oap-server
# Install SkyWalking PHP Agent
......@@ -12,15 +12,15 @@ When building directly from Git sources or after custom modifications you might
## PHP extension + Agent
The collect data from your instance you need both the PHP extension, and the agent.
No pre built binaries or PHP extension are availble at the moment, so you need to
build them from source.
build them from a source.
You can run the following commands to install the SkyWalking PHP Agent.
## Install PHP Extension
## Install PHP Extension
```shell script
curl -Lo v3.2.8.tar.gz https://github.com/SkyAPM/SkyAPM-php-sdk/archive/v3.2.8.tar.gz
tar zxvf v3.2.8.tar.gz
cd SkyAPM-php-sdk-3.2.8
curl -Lo v3.3.0.tar.gz https://github.com/SkyAPM/SkyAPM-php-sdk/archive/v3.3.0.tar.gz
tar zxvf v3.3.0.tar.gz
cd SkyAPM-php-sdk-3.3.0
phpize && ./configure && make && make install
```
......@@ -29,7 +29,7 @@ phpize && ./configure && make && make install
For installing the sky-php-agent, you first need to build it:
```shell script
cd SkyAPM-php-sdk-3.2.8
cd SkyAPM-php-sdk-3.3.0
go build -o sky-php-agent cmd/main.go
chmod +x sky-php-agent
cp sky-php-agent /usr/local/bin
......@@ -37,7 +37,7 @@ cp sky-php-agent /usr/local/bin
## How to use
### Add skywalking config to php.ini and restart php-fpm
### Add SkyWalking config to php.ini and restart php-fpm
```shell script
; Loading extensions in PHP
......@@ -47,119 +47,31 @@ extension=skywalking.so
skywalking.enable = 1
; Set skyWalking collector version (5 or 6 or 7 or 8)
skywalking.version = 6
skywalking.version = 8
; Set app code e.g. MyProjectName
skywalking.app_code = MyProjectName
; sock file path(default /tmp/sky-agent.sock)
; sock file path default /tmp/sky-agent.sock
; Warning *[systemd] please disable PrivateTmp feature*
; Warning *Make sure PHP has read and write permissions on the socks file*
skywalking.sock_path=/tmp/sky-agent.sock
```
## Select startup script and log output method
## If you use CentOS 7 Use the startup script below to You need to change the corresponding address, version
## sky-php-agent `systemd` example
```shell script
[Unit]
Description=The Sw-Php-Agent Process Manager
Description=The SkyWalking PHP-Agent Process Manager
After=syslog.target network.target
[Service]
Type=simple
#Modify the corresponding directory and address here
ExecStart=/usr/local/bin/sky-php-agent-linux-X64 --grpc=127.0.0.1:11800 --sky-version=7 --socket=/tmp/sky-agent.sock
# Modify the corresponding directory and address here
ExecStart=/usr/local/bin/sky-php-agent --grpc=127.0.0.1:11800 --sky-version=8 --socket=/tmp/sky-agent.sock
ExecStop=/bin/kill -SIGINT $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
```
## If you use CentOS6 Log management with lograted
## This is Start script You need to change the address, version and Copy and run it with root
```shell script
echo "
#/bin/bash
start(){
[ -f /tmp/php-agent.pid ]&&echo "Alredy running"&&exit 1
/usr/local/bin/sky-php-agent-linux-X64 --grpc=127.0.0.1:11800 --sky-version=7 --socket=/tmp/sky-agent.sock 1>>/var/log/phpagent/php-agent.log 2>>/var/log/phpagent/php-agent-error.log &
if [ $? -eq 0 ];then
echo $! >/tmp/php-agent.pid
echo "sky-php-agent startup!!!"
fi
}
stop(){
kill -9 `cat /tmp/php-agent.pid`
rm -f /tmp/php-agent.pid
[[ $? == 0 ]]&&echo "sky-php-agent stop!!!"
}
# add the restart method
case $1 in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "USAGE: $0 {start |stop |restart }"
;;
esac
exit 0 " > /etc/init.d/php-agent
```
## This is Log management script,Copy and run it with root
```shell script
mkdir /var/log/phpagent/
echo "
/var/log/phpagent/*.log {
rotate 10
daily
dateext
nocompress
missingok
notifempty
create 0644 root root
postrotate
/etc/init.d/php-agent restart
endscript
}" >/etc/logrotate.d/phpagent
```
### Agent parameter description
```shell script
# View help information
./sky-php-agent -h
# Specify grpc address
/usr/local/bin/sky-php-agent --grpc 127.0.0.1:11800
# Specify the socket file. The path is the path in the php.ini configuration
/usr/local/bin/sky-php-agent --socket=/tmp/sky-agent.sock
# Specify the version of skywalking
/usr/local/bin/sky-php-agent --sky-version=7
```
### ⚠️⚠️⚠️ Warning *Make sure PHP has read and write permissions on the socks file*
# todo
# Get started quickly with docker
See [Install agent](install-agent.md)
\ No newline at end of file
See [docker-compose.yml](../docker-compose.yml)
See [Dockerfile](../Dockerfile)
# 20191001
## Agent 修改
1. 修改文件权限为0777
1. 执行`sky-php-agent -h` 查看可用参数
# 20190722
## SkyWalking 扩展修改
1. 支持对mysqli扩展的支持(目前暂时仅支持`mysqli::query()`)
# 20190719
## Agent 修改
1. agent创建的文件权限修改为0666, 文件的类型修改为sock类型。修复php与sock文件进行通信时提示"Permission Deny"的问题
2. agent的参数接收方式修改, 执行`sky-php-agent -h` 查看可用参数
3. agent支持在参数中指定sock文件的路径
## SkyWalking 扩展修改
1. 新增 skywalking_get_trace_info():array 方法,获取trace数据,以方便业务日志获取当前请求trace信息。
2. 删除无用的skywalking.log_path及skywalking.grpc配置项
3. 新增 skywalking.sock_path 配置项,支持自定义sock文件的路径
# 环境
PHP7+
# 安装
## 安装PHP的SkyWalking扩展(Ubuntu环境)
1.安装php-dev (具体的PHP版本以你的环境为准,必须php7+)
```shell
sudo apt install php7.2-dev
```
2.安装curl开发包
```shell
sudo apt install curl-dev
```
3.git clone 源码
```shell
git clone https://github.com/SkyAPM/SkyAPM-php-sdk.git /path/to/SkyAPM-php-sdk
```
4.编译安装SkyAPM-php-sdk
```shell
cd /path/to/SkyAPM-php-sdk
phpize
./configure
make
sudo make install
```
5.新建php的skywalking扩展配置文件,写入配置
```shell
; 扩展so
extension=skywalking.so
; 是否启用:0 关闭;1 启用 (默认值为0)
skywalking.enable=1
; skywalking的版本:5或者6(默认值为6)
skywalking.version=6
; app_code代码,不要含特殊字符,请使用数字、字母、下换线。(默认为:hello_skywalking)
skywalking.app_code=hello_skywalking
; sock文件路径(默认值为/var/run/sky-agent.sock)
skywalking.sock_path=/var/run/sky-agent.sock
```
6.重启php-fpm服务
```shell
sudo service php-fpm restart
```
7.查看skywalking扩展是否成功加载
```shell
php -m | grep skywalking
```
查看具体配置,请使用`php -i`或者`phpinfo()`函数
# 常见问题
-----
1. linux下php不能读取/tmp文件夹下文件,即php无法与/tmp目录下的sock文件进行通信。
该问题一般是由`systemd`服务的`PrivateTmp`属性配置导致的。
`systemd`中服务的配置文件都在目录`/lib/systemd/system/`中:
* 检查SELinux配置,可尝试关闭SELinux
* `apache`的配置文件为`apache2.service`
* `php-fpm`的配置文件为`php-fpm.service` (可能会存在版本号,例如`php7.2-fpm.service`)
修改`PrivateTmp`属性的取值为`false`, 再执行`sudo systemctl daemon-reload`, 最后再重启`apache`或者`php-fpm`
1. 对于已经安装并启用了`SkyWalking`扩展的PHP环境的主机,所发送的trace信息在哪里?
* 如果`skywalking.version=5`, 则`http`请求的`header`中会加入一个名为`SW3`的数据。PHP可使用`$_SERVER['HTTP_SW3']`获取。[协议文档](https://github.com/apache/skywalking/blob/master/docs/en/protocols/Skywalking-Cross-Process-Propagation-Headers-Protocol-v1.md)
* 如果`skywalking.version=6`, 则`http`请求的`header`中会加入一个名为`SW6`的数据。PHP可使用`$_SERVER['HTTP_SW6']`获取。[协议文档](https://github.com/apache/skywalking/blob/master/docs/en/protocols/Skywalking-Cross-Process-Propagation-Headers-Protocol-v2.md)
* SW6值格式为:`1-{distributedTraceIdEncode}-{traceSegmentIdEncode}-{span_id}-{application_instance}-{entryApplicationInstance}-{peerHostEncode}-{entryEndpointNameEncode}-{parentEndpointNameEncode}`, Encode算法为base64_encode()。示例取值为:`1-OTQuMjIyMzUuMTU2MjkxMDg1MzAwMDM=-OTQuMjIyMzUuMTU2MjkxMDg1MzAwMDM=-1-94-94-IwE6NDQz-Iy9pbmRleC5waHA/ZGRkZGRkZA==-IwE=`
* 值中`distributedTraceId`即下方的`globalTraceIds[0]`的取值
1. 会传递trace信息的请求类型有哪些?
* php curl扩展发出的请求
* php PDO扩展发出的请求
* php mysqli扩展发出的请求
* php yar扩展(client)发出的请求
* php [grpc/grpc](https://github.com/grpc/grpc-php) 发出的请求
* php redis扩展发出的请求
* php predis库发出的请求
1. skywalking_get_trace_info()函数的返回值格式?
* 返回值为数组。如果扩展加载但是未启用(`skywalking.enable=0`), 则返回空数组
```php
# skywalking_get_trace_info 返回值格式如下:
[
'application_instance' => 94,
'pid' => 22230,
'application_id' => 29,
'version' => 6,
'segment' => [
'traceSegmentId' => '94.22412.15629106130002',
'isSizeLimited' => 0,
'spans' => [
[
'tags' => [
'url' => '/index.php?id=123'
],
'spanId' => 0,
'parentSpanId' => -1,
'startTime' => 1562910613896,
'operationName' => '/index.php',
'peer' => '127.0.0.1:80',
'spanType' => 0,
'spanLayer' => 3,
'componentId' => 2,
'refs' => [
[
'type' => 0,
'parentTraceSegmentId' => '94.22412.15629106130002',
'parentSpanId' => 1,
'parentApplicationInstanceId' => 94,
'networkAddress' => ':443',
'entryApplicationInstanceId' => 94,
'entryServiceName' => 'index.php?to=123',
'parentServiceName' => '',
],
],
],
],
],
'globalTraceIds' => [
'94.22235.15629108530003',
],
]
```
1. mysqli有过程式风格和对象式风格,上报的数据有何不同?
* 目前扩展仅监控mysqli_query()、mysqli::query()方法, 并记录对应执行的sql, 在上报到oapServer的时候,统一按照mysqli->query()的格式进行上报
\ No newline at end of file
# 启动agent
1.选择你的操作系统对应的agent应用程序
darwin指MacOS操作系统, linux指Linux操作系统
x64/x86分别指64位操作系统/32位操作系统
```
sky-php-agent-darwin-x64
sky-php-agent-darwin-x86
sky-php-agent-linux-x64
sky-php-agent-linux-x86
sky-php-agent-linux-arm64
sky-php-agent-linux-arm86
```
2.启动agent(以 Ubuntu x64 平台示例)
```shell
# 添加可执行权限
sudo chmod +x ./sky-php-agent-linux-x64
# 启动
./sky-php-agent-linux-x64 127.0.0.1:11800 /var/run/sky-agent.sock