uwsgi日志慢请求分析器:uwsgi-sloth

uwsgi-sloth 是由我开发的一个分析uwsgi日志的工具, 主要的功能是通过分析uwsgi日志里面的请求处理时间来生成慢请求报表,为你在优化uwsgi app性能时提供帮助。

目前支持针对uwsgi日志文件生成 单个静态报表文件 或者实时分析日志来生成 实时报表

效果图预览:

https://github.com/piglei/uwsgi-sloth/raw/master/uwsgi-sloth-screenshot.png

如果你的站点跑在uwsgi下面,而且没有成熟的性能监控系统,那uwsgi-sloth简直就是为你量身定制。

项目地址: https://github.com/piglei/uwsgi-sloth/

快速开始

安装

uwsgi-sloth完全使用python编写,你可以直接使用pip来安装它:

System Message: WARNING/2 (<string>, line 26)

Cannot analyze code. Pygments package not found.

.. code-block:: bash

    # 安装pypi的稳定版本
    $ pip install uwsgi-sloth

    # 安装github上的最新版
    $ pip install -e git+https://github.com/piglei/uwsgi-sloth#egg=uwsgi-sloth

生成一份静态报表文件

安装完成后,你可以使用 uwsgi-sloth analyze 命令来分析你的uwsgi日志文件。

System Message: WARNING/2 (<string>, line 39)

Cannot analyze code. Pygments package not found.

.. code-block:: bash

    # Generate a report
    $ uwsgi-sloth analyze -f uwsgi_access.log --output=report.html

    # Specify threshold for request process time
    $ uwsgi-sloth analyze -f uwsgi_access.log --output=report.html --min-msecs=400

查看更多: uwsgi-sloth analyze

实时报表

我们还提供了一个更强大的功能:实时日志报表,这个配置起来比上一个要稍微麻烦一些。

首先,通过 uwsgi-sloth echo_conf 命令创建一个默认的配置文件:

System Message: WARNING/2 (<string>, line 57)

Cannot analyze code. Pygments package not found.

.. code-block:: bash

    uwsgi-sloth echo_conf > /data/uwsgi_sloth/myblog.conf

默认的配置文件看起来是这样的,每一个选项描述写在了注释里:

System Message: WARNING/2 (<string>, line 63)

Cannot analyze code. Pygments package not found.

.. code-block:: ini

    # A sample uwsgi-sloth config file

    # uwsgi log path, only support default log format
    uwsgi_log_path = '/your_uwsgi_logs/web.log'

    # All HTML files and data files will store here, must have read/write permissions
    data_dir = '/you_data/uwsgi-sloth/'

    # Minimal msecs for detect slow requests, default to 200
    # min_msecs = 200

    # Domain for your website, best given
    domain = 'http://www.yourwebsite.com/'

    # Custom url regular expressions file
    # url_file = '/your_custom_url_file_path'

在修改了最重要的 uwsgi_log_pathdata_dir 两个参数之后,你便可以通过 uwsgi-sloth start -c /data/uwsgi_sloth/myblog.conf 命令来启动uwsgi-sloth的worker了,如果 一切都正常的话,你会看到这样的输出:

[2014-06-26 01:32:56,851] uwsgi_sloth INFO: Start from last savepoint, last_log_datetime: 2014-06-26 09:32:04
[2014-06-26 01:32:58,859] uwsgi_sloth INFO: Rendering HTML file /data/uwsgi_sloth/myblog/html/latest_5mins.html...
... ...

如果你的日志文件比较庞大,第一次启动可能会花上比较长的时间。

作为守护进程运行

uwsgi-sloth 并没有内置作为守护进程启动的选项,我建议你使用类似 supervisor 这样的工具来管理uwsgi-sloth进程。

查看你的实时报表

现在,实时的报表已经被建立而且可以自动更新了,我们应该配置我们的服务器来访问它, 下面这个配置是针对Nginx的:

$ cat /etc/nginx/sites-enabled/sloth_myblog.conf
server {

    listen   80;
    server_name  uwsgi-sloth.zlovezl.cn;

    location / {
        root /data/uwsgi_sloth/myblog/html/;
        index "latest_5mins.html";
    }
}

重载了Nginx的配置文件以后,打开你的浏览器你就能看到那些漂亮的报表了。

当前命令

uwsgi-sloth analyze

可用参数

usage: uwsgi-sloth analyze [-h] -f FILEPATH [--output OUTPUT]
                           [--min-msecs MIN_MSECS] [--domain DOMAIN]
                           [--url-file URL_FILE]

optional arguments:
  -h, --help            show this help message and exit
  -f FILEPATH, --filepath FILEPATH
                        Path of uwsgi log file
  --output OUTPUT       HTML report file path
  --min-msecs MIN_MSECS
                        Request serve time lower than this value will not be
                        counted, default: 200
  --domain DOMAIN       Make url in report become a hyper-link by settings a
                        domain
  --url-file URL_FILE   Customized url rules in regular expression

使用一个自定义的URL规则文件

默认情况下,uwsgi-sloth会通过把URL中连续的数字转换为“d+”这样的符号来给URL 分组,如果你需要自定义你的URL规则,你需要建立一个URL规则文件,内容大概是这样:

$ cat url_rules
# A valid url_rules file are seperated regular expressions
^club/(?P<place>\w+)/(?P<year>\d+)/(?P<issue>\d+)/signup/$
^club/signup/success/$
^club/checkin/success/$

然后使用 --url-file 选项来指定它

$ uwsgi-sloth analyze -f uwsgi_access.log --output=report.html --url-file=url_rules

uwsgi-sloth echo_conf

打印一个默认的配置文件

uwsgi-sloth start

启动实时报表的worker

$ uwsgi-sloth start -h
usage: uwsgi-sloth start [-h] -c CONFIG

optional arguments:
  -h, --help            show this help message and exit
  -c CONFIG, --config CONFIG
                        uwsgi-sloth config file, use "uwsgi-sloth echo_conf"
                        for a default one

注意点

  • 目前只支持默认的uwsgi日志格式
  • Python 2.6/2.7下测试通过
  • 默认情况下, uwsgi-sloth 会通过把连续的数字转换为 '(d+)' 来进行URL归类,就像这样: /users/3074/ -> /users/(\d+)

欢迎试用和反馈!