Skip to content

Commit bb44ba9

Browse files
u5surfSuperQ
andauthored
Introduce vhost_traffic_status_stats_by_upstream (#293)
Add new boolean flag `vhost_traffic_status_stats_by_upstream` to allow disabling statistics collection for upstream servers --------- Co-authored-by: Ben Kochie <superq@gmail.com>
1 parent 3407cc2 commit bb44ba9

5 files changed

+86
-22
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Table of Contents
7373
* [vhost_traffic_status_histogram_buckets](#vhost_traffic_status_histogram_buckets)
7474
* [vhost_traffic_status_bypass_limit](#vhost_traffic_status_bypass_limit)
7575
* [vhost_traffic_status_bypass_stats](#vhost_traffic_status_bypass_stats)
76+
* [vhost_traffic_status_stats_by_upstream](#vhost_traffic_status_stats_by_upstream)
7677
* [Releases](#releases)
7778
* [See Also](#see-also)
7879
* [TODO](#todo)
@@ -1845,6 +1846,46 @@ http {
18451846
}
18461847
```
18471848

1849+
### vhost_traffic_status_stats_by_upstream
1850+
1851+
| - | - |
1852+
| --- | --- |
1853+
| **Syntax** | **vhost_traffic_status_stats_by_upstream** \<on\|off\> |
1854+
| **Default** | on |
1855+
| **Context** | http|
1856+
1857+
`Description:` Enables or disables to stats `upstreamZone`.
1858+
The `upstreamZone` in the traffic status stats features is bypassed if this option is disabled.
1859+
In other words, it is excluded from the traffic status stats.
1860+
This is mostly useful if you want to be disable statistics collection for upstream servers to reduce CPU load.
1861+
1862+
```Nginx
1863+
http {
1864+
vhost_traffic_status_zone;
1865+
vhost_traffic_status_stats_by_upstream off;
1866+
1867+
proxy_cache_path /var/cache/nginx keys_zone=zone1:1m max_size=1g inactive=24h;
1868+
upstream backend {
1869+
...
1870+
}
1871+
...
1872+
1873+
server {
1874+
1875+
...
1876+
1877+
location /status {
1878+
vhost_traffic_status_display;
1879+
vhost_traffic_status_display_format html;
1880+
}
1881+
location /backend {
1882+
proxy_cache zone1;
1883+
proxy_pass http://backend;
1884+
}
1885+
}
1886+
}
1887+
```
1888+
18481889
## Releases
18491890

18501891
To cut a release, create a changelog entry PR with [git-chglog](https://github.com/git-chglog/git-chglog)

src/ngx_http_vhost_traffic_status_display_json.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,9 @@ ngx_http_vhost_traffic_status_display_set(ngx_http_request_t *r,
837837

838838
buf--;
839839
buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_E);
840-
buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_NEXT);
840+
if (vtscf->stats_by_upstream) {
841+
buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_NEXT);
842+
}
841843

842844
/* filterZones */
843845
o = buf;
@@ -854,25 +856,29 @@ ngx_http_vhost_traffic_status_display_set(ngx_http_request_t *r,
854856
} else {
855857
buf--;
856858
buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_E);
857-
buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_NEXT);
859+
if (vtscf->stats_by_upstream) {
860+
buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_NEXT);
861+
}
858862
}
859863

860864
/* upstreamZones */
861-
o = buf;
865+
if (vtscf->stats_by_upstream) {
866+
o = buf;
862867

863-
buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_UPSTREAM_S);
868+
buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_UPSTREAM_S);
864869

865-
s = buf;
870+
s = buf;
866871

867-
buf = ngx_http_vhost_traffic_status_display_set_upstream_group(r, buf);
872+
buf = ngx_http_vhost_traffic_status_display_set_upstream_group(r, buf);
868873

869-
if (s == buf) {
870-
buf = o;
871-
buf--;
874+
if (s == buf) {
875+
buf = o;
876+
buf--;
872877

873-
} else {
874-
buf--;
875-
buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_E);
878+
} else {
879+
buf--;
880+
buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_E);
881+
}
876882
}
877883

878884
#if (NGX_HTTP_CACHE)

src/ngx_http_vhost_traffic_status_display_prometheus.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -527,16 +527,18 @@ ngx_http_vhost_traffic_status_display_prometheus_set(ngx_http_request_t *r,
527527
}
528528

529529
/* upstreamZones */
530-
o = buf;
530+
if (vtscf->stats_by_upstream) {
531+
o = buf;
531532

532-
buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_PROMETHEUS_FMT_UPSTREAM_S);
533+
buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_PROMETHEUS_FMT_UPSTREAM_S);
533534

534-
s = buf;
535+
s = buf;
535536

536-
buf = ngx_http_vhost_traffic_status_display_prometheus_set_upstream(r, buf, node);
537+
buf = ngx_http_vhost_traffic_status_display_prometheus_set_upstream(r, buf, node);
537538

538-
if (s == buf) {
539-
buf = o;
539+
if (s == buf) {
540+
buf = o;
541+
}
540542
}
541543

542544
#if (NGX_HTTP_CACHE)

src/ngx_http_vhost_traffic_status_module.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,13 @@ static ngx_command_t ngx_http_vhost_traffic_status_commands[] = {
210210
offsetof(ngx_http_vhost_traffic_status_loc_conf_t, bypass_stats),
211211
NULL },
212212

213+
{ ngx_string("vhost_traffic_status_stats_by_upstream"),
214+
NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG,
215+
ngx_conf_set_flag_slot,
216+
NGX_HTTP_LOC_CONF_OFFSET,
217+
offsetof(ngx_http_vhost_traffic_status_loc_conf_t, stats_by_upstream),
218+
NULL },
219+
213220
ngx_null_command
214221
};
215222

@@ -271,10 +278,12 @@ ngx_http_vhost_traffic_status_handler(ngx_http_request_t *r)
271278
"handler::shm_add_server() failed");
272279
}
273280

274-
rc = ngx_http_vhost_traffic_status_shm_add_upstream(r);
275-
if (rc != NGX_OK) {
276-
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
277-
"handler::shm_add_upstream() failed");
281+
if (vtscf->stats_by_upstream) {
282+
rc = ngx_http_vhost_traffic_status_shm_add_upstream(r);
283+
if (rc != NGX_OK) {
284+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
285+
"handler::shm_add_upstream() failed");
286+
}
278287
}
279288

280289
rc = ngx_http_vhost_traffic_status_shm_add_filter(r);
@@ -889,6 +898,7 @@ ngx_http_vhost_traffic_status_create_loc_conf(ngx_conf_t *cf)
889898
* conf->histogram_buckets = { NULL, ... };
890899
* conf->bypass_limit = 0;
891900
* conf->bypass_stats = 0;
901+
* conf->stats_by_upstream = 0;
892902
*/
893903

894904
conf->shm_zone = NGX_CONF_UNSET_PTR;
@@ -908,6 +918,7 @@ ngx_http_vhost_traffic_status_create_loc_conf(ngx_conf_t *cf)
908918
conf->histogram_buckets = NGX_CONF_UNSET_PTR;
909919
conf->bypass_limit = NGX_CONF_UNSET;
910920
conf->bypass_stats = NGX_CONF_UNSET;
921+
conf->stats_by_upstream = NGX_CONF_UNSET;
911922

912923
conf->node_caches = ngx_pcalloc(cf->pool, sizeof(ngx_rbtree_node_t *)
913924
* (NGX_HTTP_VHOST_TRAFFIC_STATUS_UPSTREAM_FG + 1));
@@ -1019,6 +1030,8 @@ ngx_http_vhost_traffic_status_merge_loc_conf(ngx_conf_t *cf, void *parent, void
10191030
ngx_conf_merge_value(conf->bypass_limit, prev->bypass_limit, 0);
10201031
ngx_conf_merge_value(conf->bypass_stats, prev->bypass_stats, 0);
10211032

1033+
ngx_conf_merge_value(conf->stats_by_upstream, prev->stats_by_upstream, 1);
1034+
10221035
name = ctx->shm_name;
10231036

10241037
shm_zone = ngx_shared_memory_add(cf, &name, 0,

src/ngx_http_vhost_traffic_status_module.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,8 @@ typedef struct {
297297
ngx_flag_t bypass_limit;
298298
ngx_flag_t bypass_stats;
299299

300+
ngx_flag_t stats_by_upstream;
301+
300302
ngx_rbtree_node_t **node_caches;
301303
} ngx_http_vhost_traffic_status_loc_conf_t;
302304

0 commit comments

Comments
 (0)