[PATCH 1 of 2] Events: debug_random directive

J Carter jordanc.carter at outlook.com
Sat Dec 9 07:46:09 UTC 2023


# HG changeset patch
# User J Carter <jordanc.carter at outlook.com>
# Date 1702098547 0
#      Sat Dec 09 05:09:07 2023 +0000
# Node ID d9275e982a7188a1ea7855295ffa93362ea9830f
# Parent  f366007dd23a6ce8e8427c1b3042781b618a2ade
Events: debug_random directive

This directive enables debug logging for only a percentage of connections that
match debug_connection/s.

This directive is useful for sample style debugging of nginx when under high
load scenarios, where logging all connections would incur excessive overhead.

This directive takes a value between 0.01%-99.99% inclusive.

Example usage:

events {
     worker_connections  1024;
     debug_connection    0.0.0.0/0;
     debug_connection    ::0;
     debug_random        1%;
}

diff -r f366007dd23a -r d9275e982a71 src/event/ngx_event.c
--- a/src/event/ngx_event.c	Tue Nov 14 15:26:02 2023 +0400
+++ b/src/event/ngx_event.c	Sat Dec 09 05:09:07 2023 +0000
@@ -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,51 @@
 }
 
 
+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;
+
+    ngx_int_t             n;
+    ngx_str_t            *value;
+
+    if (ecf->debug_percent != NGX_CONF_UNSET_UINT) {
+        return "is duplicate";
+    }
+
+    value = cf->args->elts;
+
+    if (value[1].len == 0 || value[1].data[value[1].len - 1] != '%') {
+        goto invalid;
+    }
+
+    n = ngx_atofp(value[1].data, value[1].len - 1, 2);
+    if (n == NGX_ERROR || n == 0 || n > 9999) {
+        goto invalid;
+    }
+
+    ecf->debug_percent = n;
+
+    return NGX_CONF_OK;
+
+invalid:
+
+    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                       "invalid percent value \"%V\"", &value[1]);
+
+#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_ERROR;
+}
+
+
 static void *
 ngx_event_core_create_conf(ngx_cycle_t *cycle)
 {
@@ -1279,6 +1333,8 @@
         return NULL;
     }
 
+    ecf->debug_percent = NGX_CONF_UNSET_UINT;
+
 #endif
 
     return ecf;
@@ -1369,5 +1425,9 @@
     ngx_conf_init_value(ecf->accept_mutex, 0);
     ngx_conf_init_msec_value(ecf->accept_mutex_delay, 500);
 
+#if (NGX_DEBUG)
+    ngx_conf_init_uint_value(ecf->debug_percent, 0);
+#endif
+
     return NGX_CONF_OK;
 }
diff -r f366007dd23a -r d9275e982a71 src/event/ngx_event.h
--- a/src/event/ngx_event.h	Tue Nov 14 15:26:02 2023 +0400
+++ b/src/event/ngx_event.h	Sat Dec 09 05:09:07 2023 +0000
@@ -438,6 +438,7 @@
     u_char       *name;
 
 #if (NGX_DEBUG)
+    ngx_uint_t    debug_percent;
     ngx_array_t   debug_connection;
 #endif
 } ngx_event_conf_t;
diff -r f366007dd23a -r d9275e982a71 src/event/ngx_event_accept.c
--- a/src/event/ngx_event_accept.c	Tue Nov 14 15:26:02 2023 +0400
+++ b/src/event/ngx_event_accept.c	Sat Dec 09 05:09:07 2023 +0000
@@ -561,6 +561,12 @@
             break;
         }
 
+        if  (ecf->debug_percent
+             && ecf->debug_percent <= (ngx_uint_t) ngx_random() % 10000)
+        {
+            break;
+        }
+
         c->log->log_level = NGX_LOG_DEBUG_CONNECTION|NGX_LOG_DEBUG_ALL;
         break;
 


More information about the nginx-devel mailing list