Skip to content

Commit

Permalink
Feature: added vhost_traffic_status_histogram_buckets directive to se…
Browse files Browse the repository at this point in the history
…t the histogram type of request processing time in format/prometheus
  • Loading branch information
vozlt committed Jun 10, 2018
1 parent 0aa14b2 commit 2fd3ae4
Show file tree
Hide file tree
Showing 8 changed files with 428 additions and 91 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Table of Contents
* [vhost_traffic_status_limit_check_duplicate](#vhost_traffic_status_limit_check_duplicate)
* [vhost_traffic_status_set_by_filter](#vhost_traffic_status_set_by_filter)
* [vhost_traffic_status_average_method](#vhost_traffic_status_average_method)
* [vhost_traffic_status_histogram_buckets](#vhost_traffic_status_histogram_buckets)
* [vhost_traffic_status_bypass_limit](#vhost_traffic_status_bypass_limit)
* [vhost_traffic_status_bypass_stats](#vhost_traffic_status_bypass_stats)
* [See Also](#see-also)
Expand All @@ -82,6 +83,7 @@ This document describes nginx-module-vts `v0.1.16` released on 21 May 2018.

## Compatibility
* Nginx
* 1.15.x (last tested: 1.15.0)
* 1.14.x (last tested: 1.14.0)
* 1.13.x (last tested: 1.13.12)
* 1.12.x (last tested: 1.12.2)
Expand Down Expand Up @@ -1613,6 +1615,33 @@ The corresponding values are `requestMsec` and `responseMsec` in JSON.
* **WMA**
* THE WMA is the [weighted moving average](https://en.wikipedia.org/wiki/Moving_average#Weighted_moving_average).

### vhost_traffic_status_histogram_buckets

| - | - |
| --- | --- |
| **Syntax** | **vhost_traffic_status_histogram_buckets** *second* ... |
| **Default** | - |
| **Context** | http, server, location |

`Description:` Sets the observe buckets to be used in the histograms.
By default, if you do not set this directive, it will not work.
The *second* can be expressed in decimal places with a minimum value of 0.001(1ms).
The maximum size of the buckets is 32. If this value is insufficient for you,
change the `NGX_HTTP_VHOST_TRAFFIC_STATUS_DEFAULT_BUCKET_LEN` in the `src/ngx_http_vhost_traffic_status_node.h`

For examples:
* **vhost_traffic_status_histogram_buckets** `0.005` `0.01` `0.05` `0.1` `0.5` `1` `5` `10`
* The observe buckets are [5ms 10ms 50ms 1s 5s 10s].
* **vhost_traffic_status_histogram_buckets** `0.005` `0.01` `0.05` `0.1`
* The observe buckets are [5ms 10ms 50ms 1s].

`Caveats:` By default, if you do not set this directive, the histogram statistics does not work.
The restored histograms by `vhost_traffic_status_dump` directive have no affected by changes to the buckets
by `vhost_traffic_status_histogram_buckets` directive.
So you must first delete the zone or the dump file before changing the buckets
by `vhost_traffic_status_histogram_buckets` directive.
Similar to the above, delete the dump file when using the histogram for the first time.

### vhost_traffic_status_bypass_limit

| - | - |
Expand Down
118 changes: 112 additions & 6 deletions src/ngx_http_vhost_traffic_status_display_prometheus.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ ngx_http_vhost_traffic_status_display_prometheus_set_server_node(
u_char *buf, ngx_str_t *key,
ngx_http_vhost_traffic_status_node_t *vtsn)
{
ngx_str_t server;
ngx_http_vhost_traffic_status_loc_conf_t *vtscf;
ngx_str_t server;
ngx_uint_t i, n;
ngx_http_vhost_traffic_status_loc_conf_t *vtscf;
ngx_http_vhost_traffic_status_node_histogram_bucket_t *b;

vtscf = ngx_http_get_module_loc_conf(r, ngx_http_vhost_traffic_status_module);

Expand All @@ -74,6 +76,35 @@ ngx_http_vhost_traffic_status_display_prometheus_set_server_node(
&vtsn->stat_request_times, vtscf->average_method,
vtscf->average_period) / 1000);

/* histogram */
b = &vtsn->stat_request_buckets;

n = b->len;

if (n > 0) {

/* histogram:bucket */
for (i = 0; i < n; i++) {
buf = ngx_sprintf(buf,
NGX_HTTP_VHOST_TRAFFIC_STATUS_PROMETHEUS_FMT_SERVER_HISTOGRAM_BUCKET,
&server, (double) b->buckets[i].msec / 1000, b->buckets[i].counter);
}

buf = ngx_sprintf(buf,
NGX_HTTP_VHOST_TRAFFIC_STATUS_PROMETHEUS_FMT_SERVER_HISTOGRAM_BUCKET_E,
&server, vtsn->stat_request_counter);

/* histogram:sum */
buf = ngx_sprintf(buf,
NGX_HTTP_VHOST_TRAFFIC_STATUS_PROMETHEUS_FMT_SERVER_HISTOGRAM_SUM,
&server, (double) vtsn->stat_request_time_counter / 1000);

/* histogram:count */
buf = ngx_sprintf(buf,
NGX_HTTP_VHOST_TRAFFIC_STATUS_PROMETHEUS_FMT_SERVER_HISTOGRAM_COUNT,
&server, vtsn->stat_request_counter);
}

#if (NGX_HTTP_CACHE)
buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_PROMETHEUS_FMT_SERVER_CACHE,
&server, vtsn->stat_cache_miss_counter,
Expand Down Expand Up @@ -160,8 +191,10 @@ ngx_http_vhost_traffic_status_display_prometheus_set_filter_node(
u_char *buf, ngx_str_t *key,
ngx_http_vhost_traffic_status_node_t *vtsn)
{
ngx_str_t filter, filter_name;
ngx_http_vhost_traffic_status_loc_conf_t *vtscf;
ngx_str_t filter, filter_name;
ngx_uint_t i, n;
ngx_http_vhost_traffic_status_loc_conf_t *vtscf;
ngx_http_vhost_traffic_status_node_histogram_bucket_t *b;

vtscf = ngx_http_get_module_loc_conf(r, ngx_http_vhost_traffic_status_module);

Expand All @@ -185,6 +218,36 @@ ngx_http_vhost_traffic_status_display_prometheus_set_filter_node(
&vtsn->stat_request_times, vtscf->average_method,
vtscf->average_period) / 1000);

/* histogram */
b = &vtsn->stat_request_buckets;

n = b->len;

if (n > 0) {

/* histogram:bucket */
for (i = 0; i < n; i++) {
buf = ngx_sprintf(buf,
NGX_HTTP_VHOST_TRAFFIC_STATUS_PROMETHEUS_FMT_FILTER_HISTOGRAM_BUCKET,
&filter, &filter_name, (double) b->buckets[i].msec / 1000,
b->buckets[i].counter);
}

buf = ngx_sprintf(buf,
NGX_HTTP_VHOST_TRAFFIC_STATUS_PROMETHEUS_FMT_FILTER_HISTOGRAM_BUCKET_E,
&filter, &filter_name, vtsn->stat_request_counter);

/* histogram:sum */
buf = ngx_sprintf(buf,
NGX_HTTP_VHOST_TRAFFIC_STATUS_PROMETHEUS_FMT_FILTER_HISTOGRAM_SUM,
&filter, &filter_name, (double) vtsn->stat_request_time_counter / 1000);

/* histogram:count */
buf = ngx_sprintf(buf,
NGX_HTTP_VHOST_TRAFFIC_STATUS_PROMETHEUS_FMT_FILTER_HISTOGRAM_COUNT,
&filter, &filter_name, vtsn->stat_request_counter);
}

#if (NGX_HTTP_CACHE)
buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_PROMETHEUS_FMT_FILTER_CACHE,
&filter, &filter_name, vtsn->stat_cache_miss_counter,
Expand Down Expand Up @@ -235,8 +298,10 @@ ngx_http_vhost_traffic_status_display_prometheus_set_upstream_node(
u_char *buf, ngx_str_t *key,
ngx_http_vhost_traffic_status_node_t *vtsn)
{
ngx_str_t upstream, upstream_server;
ngx_http_vhost_traffic_status_loc_conf_t *vtscf;
ngx_str_t target, upstream, upstream_server;
ngx_uint_t i, n, len;
ngx_http_vhost_traffic_status_loc_conf_t *vtscf;
ngx_http_vhost_traffic_status_node_histogram_bucket_t *b;

vtscf = ngx_http_get_module_loc_conf(r, ngx_http_vhost_traffic_status_module);

Expand Down Expand Up @@ -271,6 +336,47 @@ ngx_http_vhost_traffic_status_display_prometheus_set_upstream_node(
&vtsn->stat_upstream.response_times, vtscf->average_method,
vtscf->average_period) / 1000);

/* histogram */
len = 2;

while (len--) {
if (len > 0) {
b = &vtsn->stat_request_buckets;
ngx_str_set(&target, "request");

} else {
b = &vtsn->stat_upstream.response_buckets;
ngx_str_set(&target, "response");
}

n = b->len;

if (n > 0) {
/* histogram:bucket */
for (i = 0; i < n; i++) {
buf = ngx_sprintf(buf,
NGX_HTTP_VHOST_TRAFFIC_STATUS_PROMETHEUS_FMT_UPSTREAM_HISTOGRAM_BUCKET,
&target, &upstream, &upstream_server, (double) b->buckets[i].msec / 1000,
b->buckets[i].counter);
}

buf = ngx_sprintf(buf,
NGX_HTTP_VHOST_TRAFFIC_STATUS_PROMETHEUS_FMT_UPSTREAM_HISTOGRAM_BUCKET_E,
&target, &upstream, &upstream_server, vtsn->stat_request_counter);

/* histogram:sum */
buf = ngx_sprintf(buf,
NGX_HTTP_VHOST_TRAFFIC_STATUS_PROMETHEUS_FMT_UPSTREAM_HISTOGRAM_SUM,
&target, &upstream, &upstream_server, (double) vtsn->stat_request_time_counter / 1000);

/* histogram:count */
buf = ngx_sprintf(buf,
NGX_HTTP_VHOST_TRAFFIC_STATUS_PROMETHEUS_FMT_UPSTREAM_HISTOGRAM_COUNT,
&target, &upstream, &upstream_server, vtsn->stat_request_counter);
}

}

return buf;
}

Expand Down

0 comments on commit 2fd3ae4

Please sign in to comment.