Skip to content

Commit

Permalink
Introduce vhost_traffic_status_stats_by_upstream (#293)
Browse files Browse the repository at this point in the history
Add new boolean flag `vhost_traffic_status_stats_by_upstream` to allow disabling statistics collection for upstream servers


---------

Co-authored-by: Ben Kochie <[email protected]>
  • Loading branch information
u5surf and SuperQ authored Jan 2, 2025
1 parent 3407cc2 commit bb44ba9
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 22 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -1845,6 +1846,46 @@ http {
}
```

### vhost_traffic_status_stats_by_upstream

| - | - |
| --- | --- |
| **Syntax** | **vhost_traffic_status_stats_by_upstream** \<on\|off\> |
| **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)
Expand Down
30 changes: 18 additions & 12 deletions src/ngx_http_vhost_traffic_status_display_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
14 changes: 8 additions & 6 deletions src/ngx_http_vhost_traffic_status_display_prometheus.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 17 additions & 4 deletions src/ngx_http_vhost_traffic_status_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -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));
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions src/ngx_http_vhost_traffic_status_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit bb44ba9

Please sign in to comment.