[PATCH] Added $http2_stream_id

J Carter jordanc.carter at outlook.com
Fri May 12 02:37:52 UTC 2023


# HG changeset patch
# User jordanc.carter at outlook.com
# Date 1683858766 -3600
#      Fri May 12 03:32:46 2023 +0100
# Node ID de1a1b4141e827984cbd0d2feb97f870c32ff289
# Parent  b71e69247483631bd8fc79a47cc32b762625b1fb
Added $http2_stream_id

Useful for tracing multiplexed requests from client logs or pcaps
captured between client and nginx, to nginx's own access logs.

Also useful for matching multiplexed request's access log entries to
debug level error logs - which is particularly difficult to do.

diff --git a/src/http/v2/ngx_http_v2_module.c
b/src/http/v2/ngx_http_v2_module.c ---
a/src/http/v2/ngx_http_v2_module.c +++
b/src/http/v2/ngx_http_v2_module.c @@ -15,6 +15,8 @@
 
 static ngx_int_t ngx_http_v2_variable(ngx_http_request_t *r,
     ngx_http_variable_value_t *v, uintptr_t data);
+static ngx_int_t ngx_http_v2_variable_stream_id(ngx_http_request_t *r,
+    ngx_http_variable_value_t *v, uintptr_t data);
 
 static ngx_int_t ngx_http_v2_module_init(ngx_cycle_t *cycle);
 
@@ -213,6 +215,9 @@
     { ngx_string("http2"), NULL,
       ngx_http_v2_variable, 0, 0, 0 },
 
+    { ngx_string("http2_stream_id"), NULL,
+      ngx_http_v2_variable_stream_id, 0, 0, 0 },
+
       ngx_http_null_variable
 };
 
@@ -271,6 +276,32 @@
 
 
 static ngx_int_t
+ngx_http_v2_variable_stream_id(ngx_http_request_t *r,
+    ngx_http_variable_value_t *v, uintptr_t data)
+{
+    u_char *p;
+
+    if (!r->stream) {
+        v->not_found = 1;
+        return NGX_OK;
+    }
+
+    p = ngx_pnalloc(r->pool, NGX_INT32_LEN);
+    if (p == NULL) {
+        return NGX_ERROR;
+    }
+
+    v->len = ngx_sprintf(p, "%i", r->stream->node->id) - p;
+    v->valid = 1;
+    v->no_cacheable = 0;
+    v->not_found = 0;
+    v->data = p;
+
+    return NGX_OK;
+}
+
+
+static ngx_int_t
 ngx_http_v2_module_init(ngx_cycle_t *cycle)
 {
     return NGX_OK;


More information about the nginx-devel mailing list