iralance / myblog

notes
0 stars 0 forks source link

tideways监控平台 #25

Open iralance opened 7 years ago

iralance commented 7 years ago

线上接口分析,手动打log非常不便。tideways扩展能把每条请求生成详细的执行日志,我们通过对日志做简单的分析就能看到程序哪部分耗时最长,建议下载xhgui,一款基于Bootstrap的xhprof UI程序。

安装

tideways

环境:mac
brew tap tideways/homebrew-profiler
brew install php71-tideways

配置ext-tideways.ini

[tideways]
extension="/usr/local/Cellar/php71-tideways/v4.1.2/tideways.so"
tideways.connection=unix:///usr/local/var/run/tidewaysd.sock
tideways.auto_prepend_library=0

重启php-fpm,查看phpinfo可以看到 t2

  1. mongodb

    brew install mongodb
    brew services start mongodb
    brew install php71-mongodb
  2. xhgui(bootstrap的ui)

    git clone https://github.com/laynefyc/xhgui-branch.git
    cd xhgui-branch
    php install.php
  3. 修改配置

如果你的MongoDB安装在当前机器,可以不用修改xhgui的配置文件,如果不是你需要在配置文件中修改MongoDB的连接ip和域名,xhgui-branch/config/config.default.php。当然你也可以选择直接存为文件。

// 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',
  1. 测试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 } )
  2. 配置nginx
    • 要监控的页面
      location ~ .*\.(php|php5)?$ {
      fastcgi_param PHP_VALUE "auto_prepend_file=/Users/qianlei/code/github/xhgui-branch/external/header.php";
      ...
      }
    • xhgui的webroot目录,如下配置为单独申请了一个域名 vim xhgui.conf
      
      log_format  xhgui  '$remote_addr - $remote_user [$time_local] "$request" '
           '$status $body_bytes_sent "$http_referer" '
           '"$http_user_agent" $http_x_forwarded_for';

server { listen 80; server_name xhgui.cc; root /Users/qianlei/code/github/xhgui-branch/webroot; location / { index index.php; if (!-e $request_filename) { rewrite . /index.php last; } } location ~ .*.(php|php5)?$ {

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

    #include fastcgi_params;
    try_files $uri =404;
    fastcgi_pass  127.0.0.1:9000;
    #fastcgi_pass  unix:/tmp/php-cgi.sock;
    fastcgi_index index.php;
include /usr/local/etc/nginx/fastcgi.conf;
}

#location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ {
#       expires      30d;
#}
#location ~ .*\.(js|css)?$ {
#       expires      12h;
#}
access_log  /Users/qianlei/code/wwwlogs/xhgui.log  xhgui;

}

nginx -s reload后访问xhgui.cc即可以看到页面
![t3](https://user-images.githubusercontent.com/4393443/28827184-529e5672-76ff-11e7-8c64-c81cacf34c1a.png)
![t4](https://user-images.githubusercontent.com/4393443/28827207-60ad851c-76ff-11e7-9f59-75e18634bbf3.png)
![t5](https://user-images.githubusercontent.com/4393443/28827210-640f035c-76ff-11e7-9bc6-4896f347ac6e.png)

频率配置,还是在xhgui的config/config.default.php文件中

profiler.enable => function() { // 如果域名为我们新建的域名则不捕获 if($_SERVER[SERVER_NAME] == jcc.cc){ return False; }else{ // 100%采样,默认为1% return True;//rand(1, 100) === 42; } }



## 参考
* [tideways](https://tideways.io/profiler/docs/setup/installation)