Skip to content

Commit

Permalink
updated ngx_http_reqstat_module
Browse files Browse the repository at this point in the history
  • Loading branch information
weiyue committed Nov 24, 2015
1 parent f40f0b7 commit 44c8507
Show file tree
Hide file tree
Showing 8 changed files with 723 additions and 197 deletions.
2 changes: 1 addition & 1 deletion auto/modules
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ if [ $HTTP_UPSTREAM_SESSION_STICKY_SHARED = YES ]; then
fi

if [ $HTTP_REQ_STATUS = YES ]; then
HTTP_MODULES="$HTTP_MODULES $HTTP_REQ_STATUS_MODULE"
HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES $HTTP_REQ_STATUS_MODULE"
HTTP_SRCS="$HTTP_SRCS $HTTP_REQ_STATUS_SRCS"
HTTP_DEPS="$HTTP_DEPS $HTTP_REQ_STATUS_DEPS"
fi
Expand Down
1 change: 0 additions & 1 deletion auto/options
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ NGX_ALL_MODULES="
ngx_http_map_module
ngx_http_split_clients_module
ngx_http_user_agent_module
ngx_http_reqstat_module
ngx_http_referer_module
ngx_http_rewrite_module
ngx_http_ssl_module
Expand Down
109 changes: 71 additions & 38 deletions docs/modules/ngx_http_reqstat_module.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,43 @@ This module will help monitor running status of Tengine.

* It shows all the results by default, and can be set to show part of them by specifying zones.

* It support for user-defined status by using nginx variables. The maximum of all the status is 50.
* It supports for user-defined status by using nginx variables. The maximum of all the status is 50.

* It recycles out-of-date running status information.

* It supports for defining output format.

* It follows the request processing flow, so internal redirect will not affect monitoring.

* Do not use variables of response as a condition, eg., $status.

Compilation
===========

The module is compiled into Tengine by default. It can be disabled with '--without-http_reqstat_module'
configuration parameter, or it can be compiled as a '.so' with '--with-http_reqstat_module=shared'.

If you use this module as a '.so', please make sure it is after 'ngx_http_lua_module'. Please refer to
'nginx -m'.


Example
===========

http {
req_status_zone server "$host,$server_addr:$server_port" 10M;
req_status_zone server "$host,$server_addr:$server_port" 10M;
req_status_zone_add_indicator server $limit;

http {
server {
location /us {
req_status_show;
req_status_show_field req_total $limit;
}

set $limit 0;

if ($arg_limit = '1') {
set $limit 1;
}

req_status server;
Expand All @@ -45,46 +62,48 @@ Example

* when you call '/us', you will get the results like this:

www.taobao.com,127.0.0.1:80,162,6242,1,1,1,0,0,0,0,10,1,10,1
www.example.com,127.0.0.1:80,162,6242,1,1,1,0,0,0,0,10,1,10,1....

* Each line shows the status infomation of a "$host,$server_addr:$server_port".

* Line format:
* Default line format:

kv,bytes_in_total,bytes_out_total,conn_total,req_total,2xx,3xx,4xx,5xx,other,rt_total,upstream_req,upstream_rt,upstream_tries,200,206,302,304,403,404,416,499,500,502,503,504,508,detail_other,ups_4xx,ups_5xx
kv,bytes_in,bytes_out,conn_total,req_total,http_2xx,http_3xx,http_4xx,http_5xx,http_other_status,rt,ups_req,ups_rt,ups_tries,http_200,http_206,http_302,http_304,http_403,http_404,http_416,http_499,http_500,http_502,http_503,http_504,http_508,http_other_detail_status,http_ups_4xx,http_ups_5xx

* **kv** value of the variable defined by the directive 'req_status_zone'. The maximun key length is configurable, 104B by default, and overlength will be cut off
* **bytes_in_total** total number of bytes received from client
* **bytes_out_total** total number of bytes sent to client
* **kv** value of the variable defined by the directive 'req_status_zone'. The maximun key length is configurable, 152B by default, and overlength will be cut off
* **bytes_in** total number of bytes received from client
* **bytes_out** total number of bytes sent to client
* **conn_total** total number of accepted connections
* **req_total** total number of processed requests
* **2xx** total number of 2xx requests
* **3xx** total number of 3xx requests
* **4xx** total number of 4xx requests
* **5xx** total number of 5xx requests
* **other** total number of other requests
* **rt_total** accumulation or rt
* **upstream_req** total number of requests calling for upstream
* **upstream_rt** accumulation or upstream rt
* **upstream_tries** total number of times calling for upstream
* **200** total number of 200 requests
* **206** total number of 206 requests
* **302** total number of 302 requests
* **304** total number of 304 requests
* **403** total number of 403 requests
* **404** total number of 404 requests
* **416** total number of 416 requests
* **499** total number of 499 requests
* **500** total number of 500 requests
* **502** total number of 502 requests
* **503** total number of 503 requests
* **504** total number of 504 requests
* **508** total number of 508 requests
* **detail_other** total number of requests of other status codes
* **ups_4xx** total number of requests of upstream 4xx
* **ups_5xx** total number of requests of upstream 5xx

* some fields will be removed in future, because user-defined status has been supported.
* **http_2xx** total number of 2xx requests
* **http_3xx** total number of 3xx requests
* **http_4xx** total number of 4xx requests
* **http_5xx** total number of 5xx requests
* **http_other_status** total number of other requests
* **rt** accumulation or rt
* **ups_req** total number of requests calling for upstream
* **ups_rt** accumulation or upstream rt
* **ups_tries** total number of times calling for upstream
* **http_200** total number of 200 requests
* **http_206** total number of 206 requests
* **http_302** total number of 302 requests
* **http_304** total number of 304 requests
* **http_403** total number of 403 requests
* **http_404** total number of 404 requests
* **http_416** total number of 416 requests
* **http_499** total number of 499 requests
* **http_500** total number of 500 requests
* **http_502** total number of 502 requests
* **http_503** total number of 503 requests
* **http_504** total number of 504 requests
* **http_508** total number of 508 requests
* **http_other_detail_status** total number of requests of other status codes
* **http_ups_4xx** total number of requests of upstream 4xx
* **http_ups_5xx** total number of requests of upstream 5xx

* You can use names in the left column to define output format, with directive 'req_status_show_field'

* Some fields will be removed in future, because user-defined status has been supported.

* tsar can parse the result and monitor, see also https://github.com/alibaba/tsar

Expand Down Expand Up @@ -116,7 +135,7 @@ Example:
req_status
-------------------------

**Syntax**: *req_status zone_name1 [zone_name2 [zone_name3]]*
**Syntax**: *req_status zone_name1 [zone_name2 [zone_name3 [...]]]*

**Default**: *none*

Expand All @@ -136,6 +155,19 @@ req_status_show
Display the status information. You can specify zones to display.


req_status_show_field
-------------------------------
**Syntax**: *req_status_show_field field_name1 [field_name2 [field_name3 [...]]]*

**Default**: *all the fields, including user defined fields*

**Context**: *loc*

Define output format, used with the directive 'req_status_show'. You can use names
to define internal supported fields, see it above. And also you can use variables
to define user defined fields. 'kv' is always the first field in a line.


req_status_zone_add_indicator
--------------------------------

Expand Down Expand Up @@ -169,6 +201,7 @@ req_status_zone_recycle

**Context**: *main*

Define the recycle threshold for a zone. Recycle will be switched on when the shared memory is exhausted, and will only take effect on imformation whose visit frequency is lower than the setting.
Define the recycle threshold for a zone. Recycle will be switched on when the shared memory is exhausted,
and will only take effect on imformation whose visit frequency is lower than the setting.
The setting frequency is defined by 'times' and 'seconds', and it is 10r/min by default.
req_status_zone_recycle demo_zone 10 60;
94 changes: 61 additions & 33 deletions docs/modules/ngx_http_reqstat_module_cn.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,35 @@

* 回收过期的监控数据。

* 设置输出格式

* 跟踪请求,不受内部跳转的影响

* 不要使用与响应相关的变量作为条件,比如"$status"

编译
===========

默认编入Tengine,可通过--without-http_reqstat_module不编译此模块,或通过--with-http_reqstat_module=shared编译为so模块。

使用so模块加载的话,请确保其顺序在"ngx_http_lua_module"之后。可以借助"nginx -m"来确认。

例子
===========

http {
req_status_zone server "$host,$server_addr:$server_port" 10M;
req_status_zone server "$host,$server_addr:$server_port" 10M;
req_status_zone_add_indicator server $limit;

http {
server {
location /us {
req_status_show;
req_status_show_field req_total $limit;
}

set $limit 0;

if ($arg_limit = '1') {
set $limit 1;
}

req_status server;
Expand All @@ -41,40 +55,42 @@

* 每行对应一个server

* 每行的格式
* 每行的默认格式

kv,bytes_in_total,bytes_out_total,conn_total,req_total,2xx,3xx,4xx,5xx,other,rt_total,upstream_req,upstream_rt,upstream_tries,200,206,302,304,403,404,416,499,500,502,503,504,508,detail_other,ups_4xx,ups_5xx
kv,bytes_in,bytes_out,conn_total,req_total,http_2xx,http_3xx,http_4xx,http_5xx,http_other_status,rt,ups_req,ups_rt,ups_tries,http_200,http_206,http_302,http_304,http_403,http_404,http_416,http_499,http_500,http_502,http_503,http_504,http_508,http_other_detail_status,http_ups_4xx,http_ups_5xx

* kv 计算得到的req_status_zone指令定义变量的值,最大长度可配置,默认104B,超长的部分截断
* bytes_in_total 从客户端接收流量总和
* bytes_out_total 发送到客户端流量总和
* bytes_in 从客户端接收流量总和
* bytes_out 发送到客户端流量总和
* conn_total 处理过的连接总数
* req_total 处理过的总请求数
* 2xx 2xx请求的总数
* 3xx 3xx请求的总数
* 4xx 4xx请求的总数
* 5xx 5xx请求的总数
* other 其他请求的总数
* rt_total rt的总数
* upstream_req 需要访问upstream的请求总数
* upstream_rt 访问upstream的总rt
* upstream_tries upstram总访问次数
* 200 200请求的总数
* 206 206请求的总数
* 302 302请求的总数
* 304 304请求的总数
* 403 403请求的总数
* 404 404请求的总数
* 416 416请求的总数
* 499 499请求的总数
* 500 500请求的总数
* 502 502请求的总数
* 503 503请求的总数
* 504 504请求的总数
* 508 508请求的总数
* detail_other 非以上13种status code的请求总数
* ups_4xx upstream返回4xx响应的请求总数
* ups_5xx upstream返回5xx响应的请求总数
* http_2xx 2xx请求的总数
* http_3xx 3xx请求的总数
* http_4xx 4xx请求的总数
* http_5xx 5xx请求的总数
* http_other_status 其他请求的总数
* rt rt的总数
* ups_req 需要访问upstream的请求总数
* ups_rt 访问upstream的总rt
* ups_tries upstram总访问次数
* http_200 200请求的总数
* http_206 206请求的总数
* http_302 302请求的总数
* http_304 304请求的总数
* http_403 403请求的总数
* http_404 404请求的总数
* http_416 416请求的总数
* http_499 499请求的总数
* http_500 500请求的总数
* http_502 502请求的总数
* http_503 503请求的总数
* http_504 504请求的总数
* http_508 508请求的总数
* http_other_detail_status 非以上13种status code的请求总数
* http_ups_4xx upstream返回4xx响应的请求总数
* http_ups_5xx upstream返回5xx响应的请求总数

* 可以用"req_status_show_field"指令定义输出格式。左侧栏是字段的名字。

* 注,后续会清理这些状态,因为已经支持了自定义状态。

Expand Down Expand Up @@ -106,7 +122,7 @@ req_status_zone
req_status
-------------------------

**Syntax**: *req_status zone_name1 [zone_name2 [zone_name3]]*
**Syntax**: *req_status zone_name1 [zone_name2 [zone_name3 [...]]]*

**Default**: *none*

Expand All @@ -127,6 +143,18 @@ req_status_show
按格式返回统计结果。可指定返回部分目标的统计结果。


req_status_show_field
-------------------------------
**Syntax**: *req_status_show_field field_name1 [field_name2 [field_name3 [...]]]*

**Default**: *all the fields, including user defined fields*

**Context**: *loc*

定义输出格式。可以使用的字段:内置字段,以上面的名字来表示;自定义字段,用变量表示。
'kv'总是每行的第一个字段。


req_status_zone_add_indicator
--------------------------------

Expand Down
27 changes: 21 additions & 6 deletions src/http/modules/ngx_http_reqstat.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@
#include <ngx_http.h>


#define NGX_HTTP_REQSTAT_USI 29
#define NGX_HTTP_REQSTAT_UMAX 50
#define NGX_HTTP_REQSTAT_SLOT NGX_HTTP_REQSTAT_UMAX - NGX_HTTP_REQSTAT_USI
#define NGX_HTTP_REQSTAT_RSRV 29
#define NGX_HTTP_REQSTAT_MAX 50
#define NGX_HTTP_REQSTAT_USER NGX_HTTP_REQSTAT_MAX - NGX_HTTP_REQSTAT_RSRV


#define variable_index(str, index) { ngx_string(str), index }

typedef struct ngx_http_reqstat_rbnode_s ngx_http_reqstat_rbnode_t;

typedef struct variable_index_s variable_index_t;

struct variable_index_s {
ngx_str_t name;
ngx_int_t index;
};

struct ngx_http_reqstat_rbnode_s {
u_char color;
u_char padding[3];
uint32_t len;

ngx_queue_t queue;
ngx_queue_t visit;

Expand Down Expand Up @@ -48,7 +57,7 @@ struct ngx_http_reqstat_rbnode_s {
ngx_atomic_t ureq;
ngx_atomic_t urt;
ngx_atomic_t utries;
ngx_atomic_t extra[NGX_HTTP_REQSTAT_SLOT];
ngx_atomic_t extra[NGX_HTTP_REQSTAT_USER];

ngx_atomic_int_t excess;

Expand All @@ -62,6 +71,9 @@ typedef struct {
ngx_array_t *monitor;
ngx_array_t *display;
ngx_array_t *bypass;
ngx_int_t index;
ngx_array_t *user_select;
ngx_array_t *user_defined_str;
} ngx_http_reqstat_conf_t;


Expand All @@ -81,6 +93,7 @@ typedef struct {
ngx_array_t *user_defined;
ngx_int_t key_len;
ngx_uint_t recycle_rate;
ngx_int_t alloc_already_fail;
} ngx_http_reqstat_ctx_t;


Expand All @@ -89,6 +102,7 @@ typedef struct {
ngx_uint_t sent;
ngx_array_t monitor_index;
ngx_flag_t bypass;
ngx_http_reqstat_conf_t *conf;
} ngx_http_reqstat_store_t;


Expand Down Expand Up @@ -186,5 +200,6 @@ typedef struct {
#define NGX_HTTP_REQSTAT_REQ_FIELD(node, offset) \
((ngx_atomic_t *) ((char *) node + offset))

ngx_http_reqstat_rbnode_t *ngx_http_reqstat_rbtree_lookup(
ngx_shm_zone_t *shm_zone, ngx_str_t *val);

ngx_http_reqstat_rbnode_t *
ngx_http_reqstat_rbtree_lookup(ngx_shm_zone_t *shm_zone, ngx_str_t *val);
Loading

0 comments on commit 44c8507

Please sign in to comment.