Skip to content

Commit

Permalink
Implement vhost_traffic_status_upstream_no_cache filter
Browse files Browse the repository at this point in the history
This enables monitoring upstream backend events that are configured not to
use cache.

Add the following line to nginx config to create a group:

        vhost_traffic_status_filter_upstream_no_cache "NO_CACHE";
  • Loading branch information
heikkiorsila authored and ypcs committed Oct 3, 2022
1 parent d0d3134 commit e4f366c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 13 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ Table of Contents
* [vhost_traffic_status_filter_by_set_key](#vhost_traffic_status_filter_by_set_key)
* [vhost_traffic_status_filter_check_duplicate](#vhost_traffic_status_filter_check_duplicate)
* [vhost_traffic_status_filter_max_node](#vhost_traffic_status_filter_max_node)
* [vhost_traffic_status_filter_upstream_no_cache](#vhost_traffic_status_filter_upstream_no_cache)
* [vhost_traffic_status_limit](#vhost_traffic_status_limit)
* [vhost_traffic_status_limit_traffic](#vhost_traffic_status_limit_traffic)
* [vhost_traffic_status_limit_traffic_by_set_key](#vhost_traffic_status_limit_traffic_by_set_key)
Expand Down Expand Up @@ -1479,6 +1480,32 @@ http {
In the above example, the `/^uris.*/` and `/^client::ports.*/` group string patterns are limited to a total of 16 nodes.
The other filters like `country::.*` are not limited.

### vhost_traffic_status_filter_upstream_no_cache

| - | - |
| --- | --- |
| **Syntax** | **vhost_traffic_status_filter_upstream_no_cache** *name* |
| **Default** | - |
| **Context** | http, server, location |

`Description:` Create a group name that counts requests that have an upstream
but should not be cached. The group is visible under serverZones.
Note: This is event is not "cache miss" or "bypass cache". Example:
`$ edit nginx.conf`

```Nginx
http {
...
vhost_traffic_status_zone;
...
server {
server_name example.org;
...
vhost_traffic_status_filter_upstream_no_cache NO_CACHE;
}
}
```

### vhost_traffic_status_limit

| - | - |
Expand Down
23 changes: 22 additions & 1 deletion 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_filter_upstream_no_cache"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
ngx_conf_set_str_slot,
NGX_HTTP_LOC_CONF_OFFSET,
offsetof(ngx_http_vhost_traffic_status_loc_conf_t, no_cache_server_zone),
NULL },

ngx_null_command
};

Expand Down Expand Up @@ -265,12 +272,21 @@ ngx_http_vhost_traffic_status_handler(ngx_http_request_t *r)
return NGX_DECLINED;
}

rc = ngx_http_vhost_traffic_status_shm_add_server(r);
rc = ngx_http_vhost_traffic_status_shm_add_server(r, NULL);
if (rc != NGX_OK) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"handler::shm_add_server() failed");
}

if (ngx_strlen(vtscf->no_cache_server_zone.data) > 0) {
rc = ngx_http_vhost_traffic_status_shm_add_server(
r, &vtscf->no_cache_server_zone);
if (rc != NGX_OK) {
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"handler::shm_add_server(NO_CACHE) 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,
Expand Down Expand Up @@ -889,6 +905,8 @@ 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->no_cache_server_zone = { 0, NULL };
*/

conf->shm_zone = NGX_CONF_UNSET_PTR;
Expand Down Expand Up @@ -1019,6 +1037,9 @@ 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_str_value(conf->no_cache_server_zone,
prev->no_cache_server_zone, "");

name = ctx->shm_name;

shm_zone = ngx_shared_memory_add(cf, &name, 0,
Expand Down
3 changes: 3 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,9 @@ typedef struct {
ngx_flag_t bypass_limit;
ngx_flag_t bypass_stats;

/* Controls name of "no cache" server zone */
ngx_str_t no_cache_server_zone;

ngx_rbtree_node_t **node_caches;
} ngx_http_vhost_traffic_status_loc_conf_t;

Expand Down
31 changes: 20 additions & 11 deletions src/ngx_http_vhost_traffic_status_shm.c
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,8 @@ ngx_http_vhost_traffic_status_shm_add_filter_node(ngx_http_request_t *r,


ngx_int_t
ngx_http_vhost_traffic_status_shm_add_server(ngx_http_request_t *r)
ngx_http_vhost_traffic_status_shm_add_server(ngx_http_request_t *r,
const ngx_str_t *no_cache_server_zone)
{
unsigned type;
ngx_int_t rc;
Expand All @@ -366,17 +367,25 @@ ngx_http_vhost_traffic_status_shm_add_server(ngx_http_request_t *r)

cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);

if (vtscf->filter && vtscf->filter_host && r->headers_in.server.len) {
/* set the key by host header */
dst = r->headers_in.server;

if (no_cache_server_zone != NULL &&
ngx_strlen(no_cache_server_zone->data) > 0) {
if (r->upstream == NULL || r->upstream->cache_status != 0) {
return NGX_OK;
}
dst.len = no_cache_server_zone->len;
dst.data = no_cache_server_zone->data;
} else {
/* set the key by server_name variable */
dst = cscf->server_name;
if (dst.len == 0) {
dst.len = 1;
dst.data = (u_char *) "_";
}
if (vtscf->filter && vtscf->filter_host && r->headers_in.server.len) {
/* set the key by host header */
dst = r->headers_in.server;
} else {
/* set the key by server_name variable */
dst = cscf->server_name;
if (dst.len == 0) {
dst.len = 1;
dst.data = (u_char *) "_";
}
}
}

type = NGX_HTTP_VHOST_TRAFFIC_STATUS_UPSTREAM_NO;
Expand Down
3 changes: 2 additions & 1 deletion src/ngx_http_vhost_traffic_status_shm.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ typedef struct {
} ngx_http_vhost_traffic_status_shm_info_t;


ngx_int_t ngx_http_vhost_traffic_status_shm_add_server(ngx_http_request_t *r);
ngx_int_t ngx_http_vhost_traffic_status_shm_add_server(
ngx_http_request_t *r, const ngx_str_t *no_cache_server_zone);
ngx_int_t ngx_http_vhost_traffic_status_shm_add_filter(ngx_http_request_t *r);
ngx_int_t ngx_http_vhost_traffic_status_shm_add_upstream(ngx_http_request_t *r);

Expand Down

0 comments on commit e4f366c

Please sign in to comment.