From bb44ba9d1003189be0590b8a575a961f66a2518b Mon Sep 17 00:00:00 2001 From: "Y.Horie" Date: Thu, 2 Jan 2025 17:58:07 +0900 Subject: [PATCH] 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 --- README.md | 41 +++++++++++++++++++ ...x_http_vhost_traffic_status_display_json.c | 30 ++++++++------ ..._vhost_traffic_status_display_prometheus.c | 14 ++++--- src/ngx_http_vhost_traffic_status_module.c | 21 ++++++++-- src/ngx_http_vhost_traffic_status_module.h | 2 + 5 files changed, 86 insertions(+), 22 deletions(-) diff --git a/README.md b/README.md index 6592233..3b1deaa 100644 --- a/README.md +++ b/README.md @@ -73,6 +73,7 @@ Table of Contents * [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) + * [vhost_traffic_status_stats_by_upstream](#vhost_traffic_status_stats_by_upstream) * [Releases](#releases) * [See Also](#see-also) * [TODO](#todo) @@ -1845,6 +1846,46 @@ http { } ``` +### vhost_traffic_status_stats_by_upstream + +| - | - | +| --- | --- | +| **Syntax** | **vhost_traffic_status_stats_by_upstream** \ | +| **Default** | on | +| **Context** | http| + +`Description:` Enables or disables to stats `upstreamZone`. +The `upstreamZone` in the traffic status stats features is bypassed if this option is disabled. +In other words, it is excluded from the traffic status stats. +This is mostly useful if you want to be disable statistics collection for upstream servers to reduce CPU load. + +```Nginx +http { + vhost_traffic_status_zone; + vhost_traffic_status_stats_by_upstream off; + + proxy_cache_path /var/cache/nginx keys_zone=zone1:1m max_size=1g inactive=24h; + upstream backend { + ... + } + ... + + server { + + ... + + location /status { + vhost_traffic_status_display; + vhost_traffic_status_display_format html; + } + location /backend { + proxy_cache zone1; + proxy_pass http://backend; + } + } +} +``` + ## Releases To cut a release, create a changelog entry PR with [git-chglog](https://github.com/git-chglog/git-chglog) diff --git a/src/ngx_http_vhost_traffic_status_display_json.c b/src/ngx_http_vhost_traffic_status_display_json.c index 909cc68..5a3ba90 100644 --- a/src/ngx_http_vhost_traffic_status_display_json.c +++ b/src/ngx_http_vhost_traffic_status_display_json.c @@ -837,7 +837,9 @@ ngx_http_vhost_traffic_status_display_set(ngx_http_request_t *r, buf--; buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_E); - buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_NEXT); + if (vtscf->stats_by_upstream) { + buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_NEXT); + } /* filterZones */ o = buf; @@ -854,25 +856,29 @@ ngx_http_vhost_traffic_status_display_set(ngx_http_request_t *r, } else { buf--; buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_E); - buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_NEXT); + if (vtscf->stats_by_upstream) { + buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_NEXT); + } } /* upstreamZones */ - o = buf; + if (vtscf->stats_by_upstream) { + o = buf; - buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_UPSTREAM_S); + buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_UPSTREAM_S); - s = buf; + s = buf; - buf = ngx_http_vhost_traffic_status_display_set_upstream_group(r, buf); + buf = ngx_http_vhost_traffic_status_display_set_upstream_group(r, buf); - if (s == buf) { - buf = o; - buf--; + if (s == buf) { + buf = o; + buf--; - } else { - buf--; - buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_E); + } else { + buf--; + buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_JSON_FMT_E); + } } #if (NGX_HTTP_CACHE) diff --git a/src/ngx_http_vhost_traffic_status_display_prometheus.c b/src/ngx_http_vhost_traffic_status_display_prometheus.c index cd5e70d..858c2fb 100644 --- a/src/ngx_http_vhost_traffic_status_display_prometheus.c +++ b/src/ngx_http_vhost_traffic_status_display_prometheus.c @@ -527,16 +527,18 @@ ngx_http_vhost_traffic_status_display_prometheus_set(ngx_http_request_t *r, } /* upstreamZones */ - o = buf; + if (vtscf->stats_by_upstream) { + o = buf; - buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_PROMETHEUS_FMT_UPSTREAM_S); + buf = ngx_sprintf(buf, NGX_HTTP_VHOST_TRAFFIC_STATUS_PROMETHEUS_FMT_UPSTREAM_S); - s = buf; + s = buf; - buf = ngx_http_vhost_traffic_status_display_prometheus_set_upstream(r, buf, node); + buf = ngx_http_vhost_traffic_status_display_prometheus_set_upstream(r, buf, node); - if (s == buf) { - buf = o; + if (s == buf) { + buf = o; + } } #if (NGX_HTTP_CACHE) diff --git a/src/ngx_http_vhost_traffic_status_module.c b/src/ngx_http_vhost_traffic_status_module.c index bbb41eb..5625232 100644 --- a/src/ngx_http_vhost_traffic_status_module.c +++ b/src/ngx_http_vhost_traffic_status_module.c @@ -210,6 +210,13 @@ static ngx_command_t ngx_http_vhost_traffic_status_commands[] = { offsetof(ngx_http_vhost_traffic_status_loc_conf_t, bypass_stats), NULL }, + { ngx_string("vhost_traffic_status_stats_by_upstream"), + NGX_HTTP_MAIN_CONF|NGX_CONF_FLAG, + ngx_conf_set_flag_slot, + NGX_HTTP_LOC_CONF_OFFSET, + offsetof(ngx_http_vhost_traffic_status_loc_conf_t, stats_by_upstream), + NULL }, + ngx_null_command }; @@ -271,10 +278,12 @@ ngx_http_vhost_traffic_status_handler(ngx_http_request_t *r) "handler::shm_add_server() failed"); } - rc = ngx_http_vhost_traffic_status_shm_add_upstream(r); - if (rc != NGX_OK) { - ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, - "handler::shm_add_upstream() failed"); + if (vtscf->stats_by_upstream) { + rc = ngx_http_vhost_traffic_status_shm_add_upstream(r); + if (rc != NGX_OK) { + ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, + "handler::shm_add_upstream() failed"); + } } 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) * conf->histogram_buckets = { NULL, ... }; * conf->bypass_limit = 0; * conf->bypass_stats = 0; + * conf->stats_by_upstream = 0; */ conf->shm_zone = NGX_CONF_UNSET_PTR; @@ -908,6 +918,7 @@ ngx_http_vhost_traffic_status_create_loc_conf(ngx_conf_t *cf) conf->histogram_buckets = NGX_CONF_UNSET_PTR; conf->bypass_limit = NGX_CONF_UNSET; conf->bypass_stats = NGX_CONF_UNSET; + conf->stats_by_upstream = NGX_CONF_UNSET; conf->node_caches = ngx_pcalloc(cf->pool, sizeof(ngx_rbtree_node_t *) * (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 ngx_conf_merge_value(conf->bypass_limit, prev->bypass_limit, 0); ngx_conf_merge_value(conf->bypass_stats, prev->bypass_stats, 0); + ngx_conf_merge_value(conf->stats_by_upstream, prev->stats_by_upstream, 1); + name = ctx->shm_name; shm_zone = ngx_shared_memory_add(cf, &name, 0, diff --git a/src/ngx_http_vhost_traffic_status_module.h b/src/ngx_http_vhost_traffic_status_module.h index 5f7b47b..4557b8b 100644 --- a/src/ngx_http_vhost_traffic_status_module.h +++ b/src/ngx_http_vhost_traffic_status_module.h @@ -297,6 +297,8 @@ typedef struct { ngx_flag_t bypass_limit; ngx_flag_t bypass_stats; + ngx_flag_t stats_by_upstream; + ngx_rbtree_node_t **node_caches; } ngx_http_vhost_traffic_status_loc_conf_t;