On Sat, 15 Jul 2023 03:48:25 +0000 J Carter <jordanc.car...@outlook.com> wrote:
> # HG changeset patch > # User J Carter <jordanc.car...@outlook.com> > # Date 1689391559 -3600 > # Sat Jul 15 04:25:59 2023 +0100 > # Node ID b1ea0a60417e547513654bf9d6bb79714865c780 > # Parent 77c1418916f7817a0d020f28d8a08f278a12fe43 > Added debug_random directive > > This directive enforces for EITHER a percentage of total connections > OR a percentage of connections matched by debug_connection CIDRs > to have debug logging enabled. > > This is useful for debugging when nginx is under high load > (production) - where debugging all connections is not possible without > disrupting traffic. > > This directive takes a value between 0.00%-100.00% exclusive. > # HG changeset patch # User J Carter <jordanc.car...@outlook.com> # Date 1689649226 -3600 # Tue Jul 18 04:00:26 2023 +0100 # Node ID 87f6f95e0385e6cd37354979ea61cc2435deb430 # Parent 77c1418916f7817a0d020f28d8a08f278a12fe43 Added debug_random directive Rework of previous patch. Fixed several bugs. Example usage: events { worker_connections 1024; #if uncommented, the percentage applies to connection from lo. #debug_connection 127.0.0.0/8; debug_random 1%; } diff -r 77c1418916f7 -r 87f6f95e0385 src/event/ngx_event.c --- a/src/event/ngx_event.c Thu Jun 08 14:58:01 2023 +0400 +++ b/src/event/ngx_event.c Tue Jul 18 04:00:26 2023 +0100 @@ -30,6 +30,8 @@ static char *ngx_event_use(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); static char *ngx_event_debug_connection(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); +static char *ngx_event_debug_random(ngx_conf_t *cf, ngx_command_t *cmd, + void *conf); static void *ngx_event_core_create_conf(ngx_cycle_t *cycle); static char *ngx_event_core_init_conf(ngx_cycle_t *cycle, void *conf); @@ -162,6 +164,13 @@ 0, NULL }, + { ngx_string("debug_random"), + NGX_EVENT_CONF|NGX_CONF_TAKE1, + ngx_event_debug_random, + 0, + 0, + NULL }, + ngx_null_command }; @@ -496,6 +505,7 @@ size_t size, cl; ngx_shm_t shm; ngx_time_t *tp; + ngx_cidr_t *cidr; ngx_core_conf_t *ccf; ngx_event_conf_t *ecf; @@ -507,6 +517,29 @@ "using the \"%s\" event method", ecf->name); } + if (ecf->debug_connection.nelts == 0 && ecf->debug_rnd > 0) { + cidr = ngx_array_push(&ecf->debug_connection); + if (cidr == NULL) { + return NGX_ERROR; + } + /*0.0.0.0/0*/ + ngx_memzero(cidr, sizeof(ngx_cidr_t)); + cidr->family = AF_INET; + +#ifdef NGX_HAVE_INET6 + cidr = ngx_array_push(&ecf->debug_connection); + if (cidr == NULL) { + return NGX_ERROR; + } + /*::/0*/ + ngx_memzero(cidr, sizeof(ngx_cidr_t)); + cidr->family = AF_INET6; +#endif + + } else if (ecf->debug_connection.nelts > 0 && ecf->debug_rnd == 0) { + ecf->debug_rnd = NGX_MAX_INT32_VALUE; + } + ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); ngx_timer_resolution = ccf->timer_resolution; @@ -1254,6 +1287,54 @@ } +static char * +ngx_event_debug_random(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) +{ +#if (NGX_DEBUG) + ngx_event_conf_t *ecf = conf; + + u_char *c; + ngx_int_t pct; + ngx_uint_t len; + ngx_str_t *value; + + value = cf->args->elts; + c = value[1].data; + len = value[1].len; + + if (c[len-1] != '%') { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "%V missing '%'", + &value[1]); + return NGX_CONF_ERROR; + } + + pct = ngx_atofp(c, len-1, 2); + + if (pct == NGX_ERROR + || pct == 0 + || pct > 9999) + { + ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, + "%V is an invalid value", + &value[1]); + return NGX_CONF_ERROR; + } + + ecf->debug_rnd = NGX_MAX_INT32_VALUE / 10000 * pct; + +#else + + ngx_conf_log_error(NGX_LOG_WARN, cf, 0, + "\"debug_random\" is ignored, you need to rebuild " + "nginx using --with-debug option to enable it"); + +#endif + + return NGX_CONF_OK; +} + + static void * ngx_event_core_create_conf(ngx_cycle_t *cycle) { @@ -1279,6 +1360,8 @@ return NULL; } + ecf->debug_rnd = NGX_CONF_UNSET_UINT; + #endif return ecf; @@ -1369,5 +1452,7 @@ ngx_conf_init_value(ecf->accept_mutex, 0); ngx_conf_init_msec_value(ecf->accept_mutex_delay, 500); + ngx_conf_init_uint_value(ecf->debug_rnd, 0); + return NGX_CONF_OK; } diff -r 77c1418916f7 -r 87f6f95e0385 src/event/ngx_event.h --- a/src/event/ngx_event.h Thu Jun 08 14:58:01 2023 +0400 +++ b/src/event/ngx_event.h Tue Jul 18 04:00:26 2023 +0100 @@ -438,6 +438,7 @@ u_char *name; #if (NGX_DEBUG) + ngx_uint_t debug_rnd; ngx_array_t debug_connection; #endif } ngx_event_conf_t; diff -r 77c1418916f7 -r 87f6f95e0385 src/event/ngx_event_accept.c --- a/src/event/ngx_event_accept.c Thu Jun 08 14:58:01 2023 +0400 +++ b/src/event/ngx_event_accept.c Tue Jul 18 04:00:26 2023 +0100 @@ -523,6 +523,7 @@ struct sockaddr_in6 *sin6; ngx_uint_t n; #endif + ngx_uint_t r = ngx_random(); cidr = ecf->debug_connection.elts; for (i = 0; i < ecf->debug_connection.nelts; i++) { @@ -561,7 +562,9 @@ break; } - c->log->log_level = NGX_LOG_DEBUG_CONNECTION|NGX_LOG_DEBUG_ALL; + c->log->log_level = (r < ecf->debug_rnd) ? + NGX_LOG_DEBUG_CONNECTION|NGX_LOG_DEBUG_ALL: + c->log->log_level; break; next: _______________________________________________ nginx-devel mailing list nginx-devel@nginx.org https://mailman.nginx.org/mailman/listinfo/nginx-devel