[PATCH] Added debug_random directive
J Carter
jordanc.carter at outlook.com
Mon Jul 24 04:24:04 UTC 2023
On Tue, 18 Jul 2023 03:36:02 +0000
J Carter <jordanc.carter at outlook.com> wrote:
> On Sat, 15 Jul 2023 03:48:25 +0000
> J Carter <jordanc.carter at outlook.com> wrote:
>
> > # HG changeset patch
> > # User J Carter <jordanc.carter at 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.carter at 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%;
> }
>
# HG changeset patch
# User J Carter <jordanc.carter at outlook.com>
# Date 1690171706 -3600
# Mon Jul 24 05:08:26 2023 +0100
# Node ID ea91b9aa69d8ce9dc9878209a83b7d538e6bc7e1
# Parent 77c1418916f7817a0d020f28d8a08f278a12fe43
Added debug_random directive
More bug fixes and style changes.
diff -r 77c1418916f7 -r ea91b9aa69d8 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 Mon Jul 24 05:08: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,33 @@
"using the \"%s\" event method", ecf->name);
}
+ if (ecf->debug_connection.nelts == 0
+ && ecf->debug_scaled_pct > 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_scaled_pct == 0)
+ {
+ ecf->debug_scaled_pct = 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 +1291,55 @@
}
+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;
+
+ if (ecf->debug_scaled_pct != NGX_CONF_UNSET_UINT) {
+ return "is duplicate";
+ }
+
+ 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_scaled_pct = 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 +1365,8 @@
return NULL;
}
+ ecf->debug_scaled_pct = NGX_CONF_UNSET_UINT;
+
#endif
return ecf;
@@ -1369,5 +1457,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_scaled_pct, 0);
+
return NGX_CONF_OK;
}
diff -r 77c1418916f7 -r ea91b9aa69d8 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 Mon Jul 24 05:08:26 2023 +0100
@@ -438,6 +438,7 @@
u_char *name;
#if (NGX_DEBUG)
+ ngx_uint_t debug_scaled_pct;
ngx_array_t debug_connection;
#endif
} ngx_event_conf_t;
diff -r 77c1418916f7 -r ea91b9aa69d8 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 Mon Jul 24 05:08: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++) {
@@ -548,6 +549,7 @@
#if (NGX_HAVE_UNIX_DOMAIN)
case AF_UNIX:
+ r = 0;
break;
#endif
@@ -561,7 +563,10 @@
break;
}
- c->log->log_level = NGX_LOG_DEBUG_CONNECTION|NGX_LOG_DEBUG_ALL;
+ if (r <= ecf->debug_scaled_pct) {
+ c->log->log_level = NGX_LOG_DEBUG_CONNECTION|NGX_LOG_DEBUG_ALL;
+ }
+
break;
next:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: hgexport
Type: application/octet-stream
Size: 5190 bytes
Desc: not available
URL: <http://mailman.nginx.org/pipermail/nginx-devel/attachments/20230724/359540fb/attachment.obj>
More information about the nginx-devel
mailing list