[PATCH] Added debug_random directive

J Carter jordanc.carter at outlook.com
Sat Jul 15 03:48:25 UTC 2023


# 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.

Example usage:

events {
    worker_connections  1024;
    #if uncommented, the percentage applies to connection from lo.
    #debug_connection    127.0.0.1/8;
    debug_random        1%;
}

diff -r 77c1418916f7 -r b1ea0a60417e 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	Sat Jul 15 04:25:59 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
 };
 
@@ -1254,6 +1263,53 @@
 }
 
 
+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 = ngx_strlen(c);
+
+    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)
 {
diff -r 77c1418916f7 -r b1ea0a60417e 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	Sat Jul 15 04:25:59 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 b1ea0a60417e 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	Sat Jul 15 04:25:59 2023 +0100
@@ -523,6 +523,14 @@
     struct sockaddr_in6  *sin6;
     ngx_uint_t            n;
 #endif
+    ngx_uint_t r = ngx_random();
+
+    if (!ecf->debug_connection.nelts) {
+        c->log->log_level = (r < ecf->debug_rnd) ?
+                            NGX_LOG_DEBUG_CONNECTION|NGX_LOG_DEBUG_ALL
:
+                            c->log->log_level;
+        return;
+    }
 
     cidr = ecf->debug_connection.elts;
     for (i = 0; i < ecf->debug_connection.nelts; i++) {
@@ -561,7 +569,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:


More information about the nginx-devel mailing list