Tideways和xhgui打造PHP非侵入式监控平台

1.简介

当我们发现生产环境的某个接口执行时间特别长时应该怎么做?直接登录线上机器单步调试?打大量的log然后分析? 一般我们可以把分析流程拆分为如下几步操作:

1.分析开发环境下执行是否会慢;如果是代码问题,在开发环境下就能检测出来;
2.分析预发环境执行是否会慢;如果是数据库或者第三方扩展问题,在预发环境就能检查出来。
3.从生产环境摘一台机器,分析代码执行慢的原因;如果是机器的问题,在生产环境就能检查出来。

如果按照以上步骤排查问题、耗时、耗体、耗心情。

2.环境搭建

1.docker+mongodb:用来存储产生的分析数据
2.tideways扩展
3.安装xhgui:用来展示相应数据
4.利用PHP内部服务器测试
5.配置nginx
6.插入到php文件中

1.docker+mongodb

docker环境的搭建自行百度。

下载镜像

docker pull registry.docker-cn.com/library/mongo

执行sudo docker images 显示,说明镜像已经安装成功

registry.docker-cn.com/library/mongo                           latest              eeeeee        3 weeks ago         393.3 MB

创建本地数据文件夹

/data1/mongodb

创建并启动mongodb容器

docker run --name mymongodb =v /data1/mongodb:/data1/mongodb --net=host -d 镜像id --auth

执行命令 sudo docker ps

ddsdfdsfsd        e21e0711c3d2:latest                                                  "docker-entrypoint.s   2 days ago          Up 2 days                               mymongodb

测试一下

sudo docker exec -it mymongodb bash
mongo

MongoDB shell version v4.0.5
connecting to: mongodb:
Implicit session: session { ) }
MongoDB server version: 4.0.5
>

将mongodb加入php.ini

到这里说明mongodb已经安装成功,端口默认为27017

1.安装tideways扩展

下载安装包:

wget -Otideways-php.tar.gz "https://s3-eu-west-1.amazonaws.com/tideways/extension/4.1.5/tideways-php-4.1.5-x86_64.tar.gz"

安装扩展:

tar xzvf tideways-php.tar.gz

cd tideways-php

bash install.sh

将tideways扩展 加入php.ini

2.xhgui

安装过程

git clone https://github.com/laynefyc/xhgui-branch.git
cd xhgui-branch
composer update
composer install

修改MongoDB的连接ip和域名,路径如下:xhgui-branch/config/config.default.php

// Can be either mongodb or file.
   /*
   'save.handler' => 'file',
   'save.handler.filename' => dirname(__DIR__) . '/cache/' . 'xhgui.data.' . microtime(true) . '_' . substr(md5($url), 0, 6),
   */
   'save.handler' => 'mongodb',

   // Needed for file save handler. Beware of file locking. You can adujst this file path
   // to reduce locking problems (eg uniqid, time ...)
   //'save.handler.filename' => __DIR__.'/../data/xhgui_'.date('Ymd').'.dat',
   'db.host' => 'mongodb://127.0.0.1:27017', // 地址
   'db.db' => 'xhprof', // 数据库名称

创建mongodb数据库、并优化索引

$ mongo
> use xhprof
> db.results.ensureIndex( { 'meta.SERVER.REQUEST_TIME' : -1 } )
> db.results.ensureIndex( { 'profile.main().wt' : -1 } )
> db.results.ensureIndex( { 'profile.main().mu' : -1 } )
> db.results.ensureIndex( { 'profile.main().cpu' : -1 } )
> db.results.ensureIndex( { 'meta.url' : 1 } )
3. 利用PHP内置服务器测试

移动到xhgui-branch目录下的webroot目录

php -S 0.0.0.0:8888
10306662-985798880eb1ebc3.png
image.png

说明执行成功

4. 配置nginx
server {
    listen       80 ;
    root /xhgui-branch/webroot;
    server_name  www.tuzisir.com;
    fastcgi_param PHP_VALUE auto_prepend_file= /xhgui-branch/external/header.php; # 所有执行此域名都会被收集
     location / {
        index  index.php;
        if (!-e $request_filename) {
            rewrite . /index.php last;
        }
    }
    location   ~ \.php$  {
        set $script_uri "";
        if ( $request_uri ~* "([^?]*)?" ) {
            set $script_uri $1;
        }
        fastcgi_pass 127.0.0.1:9768; # 配置php-fpm
        fastcgi_param  SCRIPT_URL         $script_uri;
        include fastcgi/comm_fastcgi_params;
    }
}

配置php-fpm,主要参数

[www.tuzisir.com]
user = www
group = www
listen = 127.0.0.1:9768

测试

输入域名

4. 单独php使用

<?php
include_once "/xhgui-branch/external/header.php";

3.其它问题

1.如何不收集某域名下的执行数据

修改xhgui-branch/config/config.default.php


'profiler.enable' => function() {
    if($_SERVER['SERVER_NAME'] == 'www.tuzisir.com'){
        return false;
    }else{
        // 100%采样,默认为1%
        return true;//rand(1, 100) === 42;
    }
},

4.学习地址

http://blog.it2048.cn/article-tideways-xhgui/
https://www.jianshu.com/p/3a8c1904e807
https://www.jianshu.com/p/e1eab7d15b17

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值