Skip to content

Commit 3a7aabf

Browse files
heikkiorsilaypcs
authored andcommitted
Implement vhost_traffic_status_upstream_no_cache filter
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";
1 parent 0009b3b commit 3a7aabf

File tree

5 files changed

+74
-13
lines changed

5 files changed

+74
-13
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ Table of Contents
6464
* [vhost_traffic_status_filter_by_set_key](#vhost_traffic_status_filter_by_set_key)
6565
* [vhost_traffic_status_filter_check_duplicate](#vhost_traffic_status_filter_check_duplicate)
6666
* [vhost_traffic_status_filter_max_node](#vhost_traffic_status_filter_max_node)
67+
* [vhost_traffic_status_filter_upstream_no_cache](#vhost_traffic_status_filter_upstream_no_cache)
6768
* [vhost_traffic_status_limit](#vhost_traffic_status_limit)
6869
* [vhost_traffic_status_limit_traffic](#vhost_traffic_status_limit_traffic)
6970
* [vhost_traffic_status_limit_traffic_by_set_key](#vhost_traffic_status_limit_traffic_by_set_key)
@@ -1479,6 +1480,32 @@ http {
14791480
In the above example, the `/^uris.*/` and `/^client::ports.*/` group string patterns are limited to a total of 16 nodes.
14801481
The other filters like `country::.*` are not limited.
14811482

1483+
### vhost_traffic_status_filter_upstream_no_cache
1484+
1485+
| - | - |
1486+
| --- | --- |
1487+
| **Syntax** | **vhost_traffic_status_filter_upstream_no_cache** *name* |
1488+
| **Default** | - |
1489+
| **Context** | http, server, location |
1490+
1491+
`Description:` Create a group name that counts requests that have an upstream
1492+
but should not be cached. The group is visible under serverZones.
1493+
Note: This is event is not "cache miss" or "bypass cache". Example:
1494+
`$ edit nginx.conf`
1495+
1496+
```Nginx
1497+
http {
1498+
...
1499+
vhost_traffic_status_zone;
1500+
...
1501+
server {
1502+
server_name example.org;
1503+
...
1504+
vhost_traffic_status_filter_upstream_no_cache NO_CACHE;
1505+
}
1506+
}
1507+
```
1508+
14821509
### vhost_traffic_status_limit
14831510

14841511
| - | - |

src/ngx_http_vhost_traffic_status_module.c

Lines changed: 22 additions & 1 deletion
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_filter_upstream_no_cache"),
214+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
215+
ngx_conf_set_str_slot,
216+
NGX_HTTP_LOC_CONF_OFFSET,
217+
offsetof(ngx_http_vhost_traffic_status_loc_conf_t, no_cache_server_zone),
218+
NULL },
219+
213220
ngx_null_command
214221
};
215222

@@ -265,12 +272,21 @@ ngx_http_vhost_traffic_status_handler(ngx_http_request_t *r)
265272
return NGX_DECLINED;
266273
}
267274

268-
rc = ngx_http_vhost_traffic_status_shm_add_server(r);
275+
rc = ngx_http_vhost_traffic_status_shm_add_server(r, NULL);
269276
if (rc != NGX_OK) {
270277
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
271278
"handler::shm_add_server() failed");
272279
}
273280

281+
if (ngx_strlen(vtscf->no_cache_server_zone.data) > 0) {
282+
rc = ngx_http_vhost_traffic_status_shm_add_server(
283+
r, &vtscf->no_cache_server_zone);
284+
if (rc != NGX_OK) {
285+
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
286+
"handler::shm_add_server(NO_CACHE) failed");
287+
}
288+
}
289+
274290
rc = ngx_http_vhost_traffic_status_shm_add_upstream(r);
275291
if (rc != NGX_OK) {
276292
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
@@ -889,6 +905,8 @@ ngx_http_vhost_traffic_status_create_loc_conf(ngx_conf_t *cf)
889905
* conf->histogram_buckets = { NULL, ... };
890906
* conf->bypass_limit = 0;
891907
* conf->bypass_stats = 0;
908+
*
909+
* conf->no_cache_server_zone = { 0, NULL };
892910
*/
893911

894912
conf->shm_zone = NGX_CONF_UNSET_PTR;
@@ -1019,6 +1037,9 @@ ngx_http_vhost_traffic_status_merge_loc_conf(ngx_conf_t *cf, void *parent, void
10191037
ngx_conf_merge_value(conf->bypass_limit, prev->bypass_limit, 0);
10201038
ngx_conf_merge_value(conf->bypass_stats, prev->bypass_stats, 0);
10211039

1040+
ngx_conf_merge_str_value(conf->no_cache_server_zone,
1041+
prev->no_cache_server_zone, "");
1042+
10221043
name = ctx->shm_name;
10231044

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

src/ngx_http_vhost_traffic_status_module.h

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

300+
/* Controls name of "no cache" server zone */
301+
ngx_str_t no_cache_server_zone;
302+
300303
ngx_rbtree_node_t **node_caches;
301304
} ngx_http_vhost_traffic_status_loc_conf_t;
302305

src/ngx_http_vhost_traffic_status_shm.c

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,8 @@ ngx_http_vhost_traffic_status_shm_add_filter_node(ngx_http_request_t *r,
353353

354354

355355
ngx_int_t
356-
ngx_http_vhost_traffic_status_shm_add_server(ngx_http_request_t *r)
356+
ngx_http_vhost_traffic_status_shm_add_server(ngx_http_request_t *r,
357+
const ngx_str_t *no_cache_server_zone)
357358
{
358359
unsigned type;
359360
ngx_int_t rc;
@@ -365,17 +366,25 @@ ngx_http_vhost_traffic_status_shm_add_server(ngx_http_request_t *r)
365366

366367
cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
367368

368-
if (vtscf->filter && vtscf->filter_host && r->headers_in.server.len) {
369-
/* set the key by host header */
370-
dst = r->headers_in.server;
371-
369+
if (no_cache_server_zone != NULL &&
370+
ngx_strlen(no_cache_server_zone->data) > 0) {
371+
if (r->upstream == NULL || r->upstream->cache_status != 0) {
372+
return NGX_OK;
373+
}
374+
dst.len = no_cache_server_zone->len;
375+
dst.data = no_cache_server_zone->data;
372376
} else {
373-
/* set the key by server_name variable */
374-
dst = cscf->server_name;
375-
if (dst.len == 0) {
376-
dst.len = 1;
377-
dst.data = (u_char *) "_";
378-
}
377+
if (vtscf->filter && vtscf->filter_host && r->headers_in.server.len) {
378+
/* set the key by host header */
379+
dst = r->headers_in.server;
380+
} else {
381+
/* set the key by server_name variable */
382+
dst = cscf->server_name;
383+
if (dst.len == 0) {
384+
dst.len = 1;
385+
dst.data = (u_char *) "_";
386+
}
387+
}
379388
}
380389

381390
type = NGX_HTTP_VHOST_TRAFFIC_STATUS_UPSTREAM_NO;

src/ngx_http_vhost_traffic_status_shm.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ typedef struct {
1919
} ngx_http_vhost_traffic_status_shm_info_t;
2020

2121

22-
ngx_int_t ngx_http_vhost_traffic_status_shm_add_server(ngx_http_request_t *r);
22+
ngx_int_t ngx_http_vhost_traffic_status_shm_add_server(
23+
ngx_http_request_t *r, const ngx_str_t *no_cache_server_zone);
2324
ngx_int_t ngx_http_vhost_traffic_status_shm_add_filter(ngx_http_request_t *r);
2425
ngx_int_t ngx_http_vhost_traffic_status_shm_add_upstream(ngx_http_request_t *r);
2526

0 commit comments

Comments
 (0)