Skip to content

Commit

Permalink
feat(proxy-wasm) expose Nginx worker_id as a property
Browse files Browse the repository at this point in the history
  • Loading branch information
casimiro committed May 9, 2024
1 parent b19d405 commit 156b51d
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/common/proxy_wasm/ngx_proxy_wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ struct ngx_proxy_wasm_ctx_s {
ngx_str_t connection_id; /* r->connection->number */
ngx_str_t mtls; /* ngx.https && ngx.ssl_client_verify */
ngx_str_t root_id; /* pwexec->root_id */
ngx_str_t worker_id; /* ngx_worker */
ngx_str_t call_status; /* dispatch response status */
ngx_str_t response_status; /* response status */
ngx_uint_t call_code;
Expand Down
34 changes: 33 additions & 1 deletion src/common/proxy_wasm/ngx_proxy_wasm_properties.c
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,32 @@ get_filter_root_id(ngx_proxy_wasm_ctx_t *pwctx, ngx_str_t *path,
}


static ngx_int_t
get_worker_id(ngx_proxy_wasm_ctx_t *pwctx, ngx_str_t *path,
ngx_str_t *value)
{
size_t len;
u_char buf[NGX_OFF_T_LEN];

if (!pwctx->worker_id.len) {
len = ngx_sprintf(buf, "%i", ngx_worker) - buf;

pwctx->worker_id.data = ngx_pnalloc(pwctx->pool, len);
if (pwctx->worker_id.data == NULL) {
return NGX_ERROR;
}

ngx_memcpy(pwctx->worker_id.data, buf, len);
pwctx->worker_id.len = len;
}

value->len = pwctx->worker_id.len;
value->data = pwctx->worker_id.data;

return NGX_OK;
}


static pwm2ngx_mapping_t pw2ngx[] = {
#ifdef NGX_WASM_HTTP

Expand Down Expand Up @@ -599,6 +625,12 @@ static pwm2ngx_mapping_t pw2ngx[] = {
ngx_null_string,
&not_supported, NULL },

/* host properties */

{ ngx_string("worker_id"),
ngx_null_string,
&get_worker_id, NULL },

{ ngx_null_string, ngx_null_string, NULL, NULL }
};

Expand Down Expand Up @@ -654,7 +686,7 @@ ngx_proxy_wasm_properties_init(ngx_conf_t *cf)

pwm2ngx_init.hash = &pwm2ngx_hash.hash;
pwm2ngx_init.key = ngx_hash_key;
pwm2ngx_init.max_size = 256;
pwm2ngx_init.max_size = 512;
pwm2ngx_init.bucket_size = ngx_align(64, ngx_cacheline_size);
pwm2ngx_init.name = "pwm2ngx_properties";
pwm2ngx_init.pool = cf->pool;
Expand Down
48 changes: 45 additions & 3 deletions t/03-proxy_wasm/hfuncs/117-proxy_properties_get.t
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,49 @@ qr/$checks/



=== TEST 13: proxy_wasm - get_property() - uri encoded request.path on: request_headers
=== TEST 13: proxy_wasm - get_property() - host properties on: request_headers, request_body, response_headers, response_body
--- load_nginx_modules: ngx_http_echo_module
--- wasm_modules: hostcalls
--- config eval
my $phases = CORE::join(',', qw(
request_headers
request_body
response_headers
response_body
));

qq {
location /t {
proxy_wasm hostcalls 'on=$phases \
test=/t/log/properties \
name=worker_id';
echo ok;
}
}
--- response_body
ok
--- grep_error_log eval: qr/worker_id+: [\w\.]+ at \w+/
--- grep_error_log_out eval
my $checks;
my @phases = qw(
RequestHeaders
ResponseHeaders
ResponseBody
ResponseBody
);

foreach my $phase (@phases) {
$checks .= "worker_id: 0 at $phase\n";
}

qr/$checks/
--- no_error_log
[error]
[crit]



=== TEST 14: proxy_wasm - get_property() - uri encoded request.path on: request_headers
--- load_nginx_modules: ngx_http_echo_module
--- wasm_modules: hostcalls
--- config
Expand All @@ -620,7 +662,7 @@ qr/request.path: \/t\?foo=std\:\:min\&bar=\[1,2\]/



=== TEST 14: proxy_wasm - get_property() - explicitly unsupported properties on: request_headers
=== TEST 15: proxy_wasm - get_property() - explicitly unsupported properties on: request_headers
--- load_nginx_modules: ngx_http_echo_module
--- wasm_modules: hostcalls
--- config eval
Expand Down Expand Up @@ -667,7 +709,7 @@ qr/"response.code_details" property not supported



=== TEST 15: proxy_wasm - get_property() - unknown property on: request_headers
=== TEST 16: proxy_wasm - get_property() - unknown property on: request_headers
--- wasm_modules: hostcalls
--- load_nginx_modules: ngx_http_echo_module
--- config
Expand Down

0 comments on commit 156b51d

Please sign in to comment.