diff --git a/src/common/metrics/ngx_wa_metrics.c b/src/common/metrics/ngx_wa_metrics.c index 3cc619a4d..95f6cadd1 100644 --- a/src/common/metrics/ngx_wa_metrics.c +++ b/src/common/metrics/ngx_wa_metrics.c @@ -291,11 +291,11 @@ ngx_wa_metrics_add(ngx_wa_metrics_t *metrics, ngx_str_t *name, rc = ngx_wasm_shm_kv_get_locked(metrics->shm, NULL, &mid, &p, &cas); if (rc == NGX_OK) { + ngx_log_debug1(NGX_LOG_DEBUG_WASM, metrics->shm->log, 0, + "wasm returning existing metric \"%z\"", mid); goto done; } - dd("adding new metric"); - ngx_memzero(buf, size); m = (ngx_wa_metric_t *) buf; m->type = type; @@ -363,9 +363,6 @@ ngx_wa_metrics_get(ngx_wa_metrics_t *metrics, uint32_t mid, ngx_uint_t *out) return NGX_ERROR; } - ngx_log_debug2(NGX_LOG_DEBUG_WASM, metrics->shm->log, 0, - "wasm retrieving metric \"%z\" as %d", mid, *out); - return NGX_OK; } diff --git a/t/03-proxy_wasm/hfuncs/contexts/150-proxy_define_metric.t b/t/03-proxy_wasm/hfuncs/contexts/150-proxy_define_metric.t index 01d5424a2..fe69d804c 100644 --- a/t/03-proxy_wasm/hfuncs/contexts/150-proxy_define_metric.t +++ b/t/03-proxy_wasm/hfuncs/contexts/150-proxy_define_metric.t @@ -4,167 +4,184 @@ use strict; use lib '.'; use t::TestWasmX; -our $workers = 2; +skip_hup(); -workers($workers); -if ($workers > 1) { - master_on(); -} - -plan_tests(6); +plan_tests(4); run_tests(); __DATA__ -=== TEST 1: proxy_wasm contexts - proxy_define_metric - on_vm_start -Hostcalls filter prefixes the name of a metric with the phase in which it's -defined. A metric c1 defined within on_configure ends up named c1_Configure. +=== TEST 1: proxy_wasm contexts - define_metric on_vm_start +--- main_config + env WASMTIME_BACKTRACE_DETAILS=1; + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm 'define_metric'; + } +--- config + location /t { + proxy_wasm context_checks; + return 200; + } +--- ignore_response_body +--- error_log +define_metric status: 0 +--- no_error_log +[error] +[crit] + ---- skip_no_debug ---- valgrind + +=== TEST 2: proxy_wasm contexts - define_metric - on_configure --- main_config wasm { - module hostcalls $TEST_NGINX_CRATES_DIR/hostcalls.wasm 'define_metric'; + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; } --- config location /t { - proxy_wasm hostcalls; + proxy_wasm context_checks 'on_configure=define_metric'; return 200; } ---- error_log eval -qr/defined counter ".+c1_OnVMStart" with id \d+/ +--- ignore_response_body +--- error_log +define_metric status: 0 --- no_error_log [error] [crit] -[emerg] -[alert] -=== TEST 2: proxy_wasm contexts - proxy_define_metric - on_configure ---- valgrind ---- wasm_modules: hostcalls ---- load_nginx_modules: ngx_http_echo_module +=== TEST 3: proxy_wasm contexts - define_metric - on_tick +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } --- config location /t { - proxy_wasm hostcalls 'on_configure=define_metrics \ - metrics=c1,g1'; - echo ok; + proxy_wasm context_checks 'on_tick=define_metric'; + return 200; } ---- grep_error_log eval: qr/defined metric \w+ as \d+ at \w+/ ---- grep_error_log_out eval -qr/defined metric c1_Configure as \d+ at Configure -defined metric g1_Configure as \d+ at Configure\n/ +--- ignore_response_body +--- error_log +define_metric status: 0 --- no_error_log [error] [crit] -[emerg] -[alert] -=== TEST 3: proxy_wasm contexts - proxy_define_metric - on_tick ---- wasm_modules: hostcalls ---- load_nginx_modules: ngx_http_echo_module +=== TEST 4: proxy_wasm contexts - define_metric on_http_dispatch_response +--- wasm_modules: context_checks --- config location /t { - proxy_wasm hostcalls 'on_tick=define_metrics \ - tick_period=500 \ - n_sync_calls=1 \ - metrics=c1,g1'; - echo ok; + proxy_wasm context_checks 'on_http_dispatch_response=define_metric \ + host=127.0.0.1:$TEST_NGINX_SERVER_PORT'; + return 200; + } + + location /dispatch { + return 200; + } +--- ignore_response_body +--- error_log +define_metric status: 0 +--- no_error_log +[error] +[crit] + + + +=== TEST 5: proxy_wasm contexts - define_metric on_request_headers +--- wasm_modules: context_checks +--- metrics: 16k +--- config + location /t { + proxy_wasm context_checks 'on_request_headers=define_metric'; + return 200; } ---- grep_error_log eval: qr/defined metric \w+ as \d+ at \w+/ ---- grep_error_log_out eval -qr/defined metric c1_Tick as \d+ at Tick -defined metric g1_Tick as \d+ at Tick\n/ +--- ignore_response_body +--- error_log +define_metric status: 0 --- no_error_log [error] [crit] -[emerg] -[alert] -=== TEST 4: proxy_wasm contexts - proxy_define_metric - on: request_headers, request_body, response_headers, response_body, log +=== TEST 6: proxy_wasm contexts - define_metric on_request_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 - log -)); - -qq{ +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } +--- config location /t { - proxy_wasm hostcalls 'on=$phases \ - test=/t/metrics/define \ - metrics=c1,g1'; + proxy_wasm context_checks 'on_request_body=define_metric'; echo ok; } -} --- request POST /t -hello ---- grep_error_log eval: qr/defined metric \w+ as \d+ at \w+/ ---- grep_error_log_out eval -my $checks; -my @phases = qw( - RequestHeaders - RequestBody - ResponseHeaders - ResponseBody - ResponseBody - Log -); - -foreach my $p (@phases) { - my $suffixed_c1 = "c1_" . $p; - my $suffixed_g1 = "g1_" . $p; - $checks .= " -?defined metric $suffixed_c1 as [0-9]+ at $p -defined metric $suffixed_g1 as [0-9]+ at $p\n"; -} - -qr/$checks/ +payload +--- ignore_response_body +--- error_log +define_metric status: 0 --- no_error_log [error] [crit] -[emerg] -[alert] -=== TEST 5: proxy_wasm contexts - proxy_define_metric - on_http_call_response ---- wasm_modules: hostcalls ---- load_nginx_modules: ngx_http_echo_module ---- http_config eval ---- config eval -qq{ - listen unix:$ENV{TEST_NGINX_UNIX_SOCKET}; - - location /dispatched { - return 200 "Hello back"; +=== TEST 7: proxy_wasm contexts - define_metric on_response_headers +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } +--- config + location /t { + proxy_wasm context_checks 'on_response_headers=define_metric'; + return 200; } +--- ignore_response_body +--- error_log +define_metric status: 0 +--- no_error_log +[error] +[crit] + + +=== TEST 8: proxy_wasm contexts - define_metric on_response_body +--- load_nginx_modules: ngx_http_echo_module +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } +--- config location /t { - proxy_wasm hostcalls 'test=/t/dispatch_http_call \ - host=unix:$ENV{TEST_NGINX_UNIX_SOCKET} \ - path=/dispatched \ - on_http_call_response=define_metrics \ - metrics=c1,g1'; + proxy_wasm context_checks 'on_response_body=define_metric'; echo ok; } -} ---- grep_error_log eval: qr/defined metric \w+ as \d+ at \w+/ ---- grep_error_log_out eval -qr/defined metric c1_HTTPCallResponse as \d+ at HTTPCallResponse -defined metric g1_HTTPCallResponse as \d+ at HTTPCallResponse\n/ +--- ignore_response_body +--- error_log +define_metric status: 0 +--- no_error_log +[error] +[crit] + + + +=== TEST 9: proxy_wasm contexts - define_metric on_log +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } +--- config + location /t { + proxy_wasm context_checks 'on_log=define_metric'; + return 200; + } +--- ignore_response_body +--- error_log +define_metric status: 0 --- no_error_log [error] [crit] -[emerg] -[alert] diff --git a/t/03-proxy_wasm/hfuncs/contexts/151-proxy_increment_metric.t b/t/03-proxy_wasm/hfuncs/contexts/151-proxy_increment_metric.t index 199079de3..ecd2b3b5b 100644 --- a/t/03-proxy_wasm/hfuncs/contexts/151-proxy_increment_metric.t +++ b/t/03-proxy_wasm/hfuncs/contexts/151-proxy_increment_metric.t @@ -6,167 +6,180 @@ use t::TestWasmX; skip_hup(); -our $workers = 2; - -workers($workers); -if ($workers > 1) { - master_on(); -} - -plan_tests(6); +plan_tests(4); run_tests(); __DATA__ -=== TEST 1: proxy_wasm contexts - proxy_increment_metric - on_vm_start ---- skip_no_debug ---- valgrind +=== TEST 1: proxy_wasm contexts - increment_metric on_vm_start --- main_config wasm { - module hostcalls $TEST_NGINX_CRATES_DIR/hostcalls.wasm 'increment_metric'; + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm 'increment_metric'; } --- config location /t { - proxy_wasm hostcalls; + proxy_wasm context_checks; return 200; } ---- error_log eval -qr/updating metric "\d+" with 1/ +--- ignore_response_body +--- error_log +increment_metric status: 0 --- no_error_log [error] [crit] -[emerg] -[alert] -=== TEST 2: proxy_wasm metrics shm - proxy_increment_metric - on_configure ---- valgrind ---- load_nginx_modules: ngx_http_echo_module ---- wasm_modules: hostcalls +=== TEST 2: proxy_wasm contexts - increment_metric - on_configure +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } --- config location /t { - proxy_wasm hostcalls 'on_configure=define_and_increment_counters \ - metrics=c2'; - echo ok; + proxy_wasm context_checks 'on_configure=increment_metric'; + return 200; } ---- error_log eval -qr/c1_Configure: $::workers at Configure/ +--- ignore_response_body +--- error_log +increment_metric status: 0 --- no_error_log [error] [crit] -[emerg] -[alert] -=== TEST 3: proxy_wasm metrics - proxy_increment_metric() - on_tick ---- wasm_modules: hostcalls ---- load_nginx_modules: ngx_http_echo_module +=== TEST 3: proxy_wasm contexts - increment_metric - on_tick +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } --- config location /t { - proxy_wasm hostcalls 'on_configure=define_metrics \ - on_tick=increment_counters \ - tick_period=500 \ - n_sync_calls=1 \ - metrics=c2'; - echo ok; + proxy_wasm context_checks 'on_tick=increment_metric'; + return 200; + } +--- ignore_response_body +--- error_log +increment_metric status: 0 +--- no_error_log +[error] +[crit] + + + +=== TEST 4: proxy_wasm contexts - increment_metric on_http_dispatch_response +--- wasm_modules: context_checks +--- config + location /t { + proxy_wasm context_checks 'on_http_dispatch_response=increment_metric \ + host=127.0.0.1:$TEST_NGINX_SERVER_PORT'; + return 200; + } + + location /dispatch { + return 200; + } +--- ignore_response_body +--- error_log +increment_metric status: 0 +--- no_error_log +[error] +[crit] + + + +=== TEST 5: proxy_wasm contexts - increment_metric on_request_headers +--- wasm_modules: context_checks +--- config + location /t { + proxy_wasm context_checks 'on_request_headers=increment_metric'; + return 200; } ---- error_log eval -qr/c1_Configure: $::workers at Tick/ +--- ignore_response_body +--- error_log +increment_metric status: 0 --- no_error_log [error] [crit] -[emerg] -[alert] -=== TEST 4: proxy_wasm metrics - proxy_increment_metric() - on: request_headers, request_body, response_headers, response_body, log +=== TEST 6: proxy_wasm contexts - increment_metric on_request_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 - log -)); - -qq{ +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } +--- config location /t { - proxy_wasm hostcalls 'on_configure=define_metrics \ - on=$phases \ - test=/t/metrics/increment_counters \ - metrics=c2'; + proxy_wasm context_checks 'on_request_body=increment_metric'; echo ok; } -} --- request POST /t -hello ---- grep_error_log eval: qr/(incremented \w+|\w+: \d+) at \w+/ ---- grep_error_log_out eval -my $checks; -my $i = 0; -my @phases = qw( - RequestHeaders - RequestBody - ResponseHeaders - ResponseBody - ResponseBody - Log -); - -foreach my $p (@phases) { - $i++; - $checks .= " -?incremented c1_Configure at $p -incremented c2_Configure at $p -c1_Configure: $i at $p -c2_Configure: $i at $p\n"; -} - -qr/$checks/ +payload +--- ignore_response_body +--- error_log +increment_metric status: 0 --- no_error_log [error] [crit] -[emerg] -[alert] -=== TEST 5: proxy_wasm metrics - proxy_increment_metric() - on_http_call_response ---- wasm_modules: hostcalls ---- load_nginx_modules: ngx_http_echo_module ---- http_config eval ---- config eval -qq{ - listen unix:$ENV{TEST_NGINX_UNIX_SOCKET}; - - location /dispatched { - return 200 "Hello back"; +=== TEST 7: proxy_wasm contexts - increment_metric on_response_headers +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; } +--- config + location /t { + proxy_wasm context_checks 'on_response_headers=increment_metric'; + return 200; + } +--- ignore_response_body +--- error_log +increment_metric status: 0 +--- no_error_log +[error] +[crit] + + +=== TEST 8: proxy_wasm contexts - increment_metric on_response_body +--- load_nginx_modules: ngx_http_echo_module +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } +--- config location /t { - proxy_wasm hostcalls 'on_configure=define_metrics \ - test=/t/dispatch_http_call \ - host=unix:$ENV{TEST_NGINX_UNIX_SOCKET} \ - path=/dispatched \ - on_http_call_response=increment_counters \ - metrics=c2'; + proxy_wasm context_checks 'on_response_body=increment_metric'; echo ok; } -} ---- grep_error_log eval: qr/(incremented \w+|\w+: \d+) at \w+/ ---- grep_error_log_out eval -qr/incremented c1_Configure at HTTPCallResponse -incremented c2_Configure at HTTPCallResponse -c1_Configure: 1 at HTTPCallResponse -c2_Configure: 1 at HTTPCallResponse\n/ +--- ignore_response_body +--- error_log +increment_metric status: 0 +--- no_error_log +[error] +[crit] + + + +=== TEST 9: proxy_wasm contexts - increment_metric on_log +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } +--- config + location /t { + proxy_wasm context_checks 'on_log=increment_metric'; + return 200; + } +--- ignore_response_body +--- error_log +increment_metric status: 0 --- no_error_log [error] [crit] -[emerg] -[alert] diff --git a/t/03-proxy_wasm/hfuncs/contexts/152-proxy_record_metric.t b/t/03-proxy_wasm/hfuncs/contexts/152-proxy_record_metric.t index 0f35d8244..3ce178c37 100644 --- a/t/03-proxy_wasm/hfuncs/contexts/152-proxy_record_metric.t +++ b/t/03-proxy_wasm/hfuncs/contexts/152-proxy_record_metric.t @@ -6,198 +6,180 @@ use t::TestWasmX; skip_hup(); -our $workers = 2; - -workers($workers); -if ($workers > 1) { - master_on(); -} - -plan_tests(6); +plan_tests(4); run_tests(); __DATA__ -=== TEST 1: proxy_wasm contexts - proxy_record_metric - on_vm_start ---- skip_no_debug ---- valgrind +=== TEST 1: proxy_wasm contexts - record_metric on_vm_start --- main_config wasm { - module hostcalls $TEST_NGINX_CRATES_DIR/hostcalls.wasm 'record_metric'; + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm 'record_metric'; } --- config location /t { - proxy_wasm hostcalls; + proxy_wasm context_checks; return 200; } ---- error_log eval -qr/updating metric "\d+" with 1/ +--- ignore_response_body +--- error_log +record_metric status: 0 --- no_error_log [error] [crit] -[emerg] -[alert] -=== TEST 2: proxy_wasm metrics shm - proxy_record_metric - on_configure ---- workers: 1 ---- valgrind ---- load_nginx_modules: ngx_http_echo_module ---- wasm_modules: hostcalls +=== TEST 2: proxy_wasm contexts - record_metric - on_configure +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } --- config location /t { - proxy_wasm hostcalls 'metrics=g2 \ - on_configure=define_and_toggle_gauges'; - echo ok; + proxy_wasm context_checks 'on_configure=record_metric'; + return 200; } ---- grep_error_log eval: qr/(toggled \w+|\w+: \d+) at \w+/ ---- grep_error_log_out eval -my $check; -$check .= "toggled g1_Configure at Configure(\n|\n.+\n)"; -$check .= "toggled g2_Configure at Configure(\n|\n.+\n)"; -$check .= "g1_Configure: 1 at Configure(\n|\n.+\n)"; -$check .= "g2_Configure: 1 at Configure(\n|\n.+\n)"; -qr/$check/ +--- ignore_response_body +--- error_log +record_metric status: 0 --- no_error_log [error] [crit] -[emerg] -[alert] -=== TEST 3: proxy_wasm metrics - proxy_record_metric - on_tick ---- wasm_modules: hostcalls ---- load_nginx_modules: ngx_http_echo_module ---- config eval -my $filters; - -foreach my $wid (0 .. $::workers - 1) { - my $wait = 100 + ($wid * 500); - $filters .= " - proxy_wasm hostcalls 'on_configure=define_metrics \ - on_tick=set_gauges \ - tick_period=$wait \ - n_sync_calls=1 \ - on_worker=$wid \ - value=$wid \ - metrics=g2';"; -} -qq{ +=== TEST 3: proxy_wasm contexts - record_metric - on_tick +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } +--- config + location /t { + proxy_wasm context_checks 'on_tick=record_metric'; + return 200; + } +--- ignore_response_body +--- error_log +record_metric status: 0 +--- no_error_log +[error] +[crit] + + + +=== TEST 4: proxy_wasm contexts - record_metric on_http_dispatch_response +--- wasm_modules: context_checks +--- config location /t { - $filters + proxy_wasm context_checks 'on_http_dispatch_response=record_metric \ + host=127.0.0.1:$TEST_NGINX_SERVER_PORT'; + return 200; + } - echo ok; + location /dispatch { + return 200; + } +--- ignore_response_body +--- error_log +record_metric status: 0 +--- no_error_log +[error] +[crit] + + + +=== TEST 5: proxy_wasm contexts - record_metric on_request_headers +--- wasm_modules: context_checks +--- config + location /t { + proxy_wasm context_checks 'on_request_headers=record_metric'; + return 200; } -} ---- wait: 1 ---- grep_error_log eval: qr/(record \d+ on \w+|\w+: \d+) at \w+/ ---- grep_error_log_out eval -my $checks; - -foreach my $worker_id (0 .. $::workers - 1) { - $checks .= "record $worker_id on g1_Configure at Tick -record $worker_id on g2_Configure at Tick -g1_Configure: $worker_id at Tick -g2_Configure: $worker_id at Tick -"; -} - -qr/$checks/ +--- ignore_response_body +--- error_log +record_metric status: 0 --- no_error_log [error] [crit] -[emerg] -[alert] -=== TEST 4: proxy_wasm metrics - proxy_record_metric - on: request_headers, request_body, response_headers, response_body, log +=== TEST 6: proxy_wasm contexts - record_metric on_request_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 - log -)); - -qq{ +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } +--- config location /t { - proxy_wasm hostcalls 'on_configure=define_metrics \ - on=$phases \ - test=/t/metrics/toggle_gauges \ - metrics=g2'; + proxy_wasm context_checks 'on_request_body=record_metric'; echo ok; } -} --- request POST /t -hello ---- grep_error_log eval: qr/(toggled \w+|\w+: \d+) at \w+/ ---- grep_error_log_out eval -my $i = 0; -my $checks; -my @phases = qw( - RequestHeaders - RequestBody - ResponseHeaders - ResponseBody - ResponseBody - Log -); - -foreach my $phase (@phases) { - $i = $i ? 0 : 1; - $checks .= " -?toggled g1_Configure at $phase -toggled g2_Configure at $phase -g1_Configure: $i at $phase -g2_Configure: $i at $phase\n"; -} - -qr/$checks/ +payload +--- ignore_response_body +--- error_log +record_metric status: 0 --- no_error_log [error] [crit] -[emerg] -[alert] -=== TEST 5: proxy_wasm metrics - proxy_record_metric - on_http_call_response ---- wasm_modules: hostcalls ---- load_nginx_modules: ngx_http_echo_module ---- http_config eval ---- config eval -qq{ - listen unix:$ENV{TEST_NGINX_UNIX_SOCKET}; - - location /dispatched { - return 200 "Hello back"; +=== TEST 7: proxy_wasm contexts - record_metric on_response_headers +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; } +--- config + location /t { + proxy_wasm context_checks 'on_response_headers=record_metric'; + return 200; + } +--- ignore_response_body +--- error_log +record_metric status: 0 +--- no_error_log +[error] +[crit] + + +=== TEST 8: proxy_wasm contexts - record_metric on_response_body +--- load_nginx_modules: ngx_http_echo_module +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } +--- config location /t { - proxy_wasm hostcalls 'on_configure=define_metrics \ - test=/t/dispatch_http_call \ - host=unix:$ENV{TEST_NGINX_UNIX_SOCKET} \ - path=/dispatched \ - on_http_call_response=toggle_gauges \ - metrics=g2'; + proxy_wasm context_checks 'on_response_body=record_metric'; echo ok; } -} ---- grep_error_log eval: qr/(toggled \w+|\w+: \d+) at \w+/ ---- grep_error_log_out eval -qr/toggled g1_Configure at HTTPCallResponse -toggled g2_Configure at HTTPCallResponse -g1_Configure: 1 at HTTPCallResponse -g2_Configure: 1 at HTTPCallResponse\n/ +--- ignore_response_body +--- error_log +record_metric status: 0 +--- no_error_log +[error] +[crit] + + + +=== TEST 9: proxy_wasm contexts - record_metric on_log +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } +--- config + location /t { + proxy_wasm context_checks 'on_log=record_metric'; + return 200; + } +--- ignore_response_body +--- error_log +record_metric status: 0 --- no_error_log [error] [crit] -[emerg] -[alert] diff --git a/t/03-proxy_wasm/hfuncs/contexts/153-proxy_get_metric.t b/t/03-proxy_wasm/hfuncs/contexts/153-proxy_get_metric.t new file mode 100644 index 000000000..61e80e4f8 --- /dev/null +++ b/t/03-proxy_wasm/hfuncs/contexts/153-proxy_get_metric.t @@ -0,0 +1,185 @@ +# vim:set ft= ts=4 sts=4 sw=4 et fdm=marker: + +use strict; +use lib '.'; +use t::TestWasmX; + +skip_hup(); + +plan_tests(4); +run_tests(); + +__DATA__ + +=== TEST 1: proxy_wasm contexts - get_metric on_vm_start +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm 'get_metric'; + } +--- config + location /t { + proxy_wasm context_checks; + return 200; + } +--- ignore_response_body +--- error_log +get_metric status: 0 +--- no_error_log +[error] +[crit] + + + +=== TEST 2: proxy_wasm contexts - get_metric - on_configure +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } +--- config + location /t { + proxy_wasm context_checks 'on_configure=get_metric'; + return 200; + } +--- ignore_response_body +--- error_log +get_metric status: 0 +--- no_error_log +[error] +[crit] + + + +=== TEST 3: proxy_wasm contexts - get_metric - on_tick +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } +--- config + location /t { + proxy_wasm context_checks 'on_tick=get_metric'; + return 200; + } +--- ignore_response_body +--- error_log +get_metric status: 0 +--- no_error_log +[error] +[crit] + + + +=== TEST 4: proxy_wasm contexts - get_metric on_http_dispatch_response +--- wasm_modules: context_checks +--- config + location /t { + proxy_wasm context_checks 'on_http_dispatch_response=get_metric \ + host=127.0.0.1:$TEST_NGINX_SERVER_PORT'; + return 200; + } + + location /dispatch { + return 200; + } +--- ignore_response_body +--- error_log +get_metric status: 0 +--- no_error_log +[error] +[crit] + + + +=== TEST 5: proxy_wasm contexts - get_metric on_request_headers +--- wasm_modules: context_checks +--- config + location /t { + proxy_wasm context_checks 'on_request_headers=get_metric'; + return 200; + } +--- ignore_response_body +--- error_log +get_metric status: 0 +--- no_error_log +[error] +[crit] + + + +=== TEST 6: proxy_wasm contexts - get_metric on_request_body +--- load_nginx_modules: ngx_http_echo_module +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } +--- config + location /t { + proxy_wasm context_checks 'on_request_body=get_metric'; + echo ok; + } +--- request +POST /t +payload +--- ignore_response_body +--- error_log +get_metric status: 0 +--- no_error_log +[error] +[crit] + + + +=== TEST 7: proxy_wasm contexts - get_metric on_response_headers +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } +--- config + location /t { + proxy_wasm context_checks 'on_response_headers=get_metric'; + return 200; + } +--- ignore_response_body +--- error_log +get_metric status: 0 +--- no_error_log +[error] +[crit] + + + +=== TEST 8: proxy_wasm contexts - get_metric on_response_body +--- load_nginx_modules: ngx_http_echo_module +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } +--- config + location /t { + proxy_wasm context_checks 'on_response_body=get_metric'; + echo ok; + } +--- ignore_response_body +--- error_log +get_metric status: 0 +--- no_error_log +[error] +[crit] + + + +=== TEST 9: proxy_wasm contexts - get_metric on_log +--- main_config + wasm { + module context_checks $TEST_NGINX_CRATES_DIR/context_checks.wasm; + } +--- config + location /t { + proxy_wasm context_checks 'on_log=get_metric'; + return 200; + } +--- ignore_response_body +--- error_log +get_metric status: 0 +--- no_error_log +[error] +[crit] diff --git a/t/03-proxy_wasm/hfuncs/contexts/153-proxy_record_metric_histogram.t b/t/03-proxy_wasm/hfuncs/contexts/153-proxy_record_metric_histogram.t deleted file mode 100644 index 3c46fd11f..000000000 --- a/t/03-proxy_wasm/hfuncs/contexts/153-proxy_record_metric_histogram.t +++ /dev/null @@ -1,188 +0,0 @@ -# vim:set ft= ts=4 sts=4 sw=4 et fdm=marker: - -use strict; -use lib '.'; -use t::TestWasmX; - -skip_hup(); - -our $workers = 2; - -workers($workers); -if ($workers > 1) { - master_on(); -} - -plan_tests(7); -run_tests(); - -__DATA__ - -=== TEST 1: proxy_wasm metrics shm - record_metric, histogram - sanity ---- skip_no_debug ---- valgrind ---- load_nginx_modules: ngx_http_echo_module ---- wasm_modules: hostcalls ---- config eval -my $filters; - -foreach my $exp (0 .. 17) { - my $v = 2 ** $exp; - $filters .= " - proxy_wasm hostcalls 'on_configure=define_metrics \ - test=/t/metrics/record_histograms \ - metrics=h1 \ - value=$v';"; -} -qq{ - location /t { - $filters - - echo ok; - } -} ---- error_log eval -[ - "growing histogram", - qr/histogram "\d+": 1: 1; 2: 1; 4: 1; 8: 1; 16: 1; 32: 1; 64: 1; 128: 1; 256: 1; 512: 1; 1024: 1; 2048: 1; 4096: 1; 8192: 1; 16384: 1; 32768: 1; 65536: 1; 4294967295: 1;/ -] ---- no_error_log -[error] -[crit] -[emerg] -[alert] - - - -=== TEST 2: proxy_wasm metrics shm - record_metric, histogram - on_configure ---- skip_no_debug ---- workers: 2 ---- valgrind ---- load_nginx_modules: ngx_http_echo_module ---- wasm_modules: hostcalls ---- config - location /t { - proxy_wasm hostcalls 'on_configure=define_and_record_histograms \ - test=/t/metrics/record_histograms \ - metrics=h1 \ - value=10'; - echo ok; - } ---- grep_error_log eval: qr/histogram "\d+":( \d+: \d+;)+/ ---- grep_error_log_out eval -qr/histogram "\d+": 16: $::workers; 4294967295: 0;/ ---- no_error_log -[error] -[crit] -[emerg] -[alert] -[stub] - - - -=== TEST 3: proxy_wasm metrics - record_metric(), histogram - on_tick ---- skip_no_debug ---- wasm_modules: hostcalls ---- load_nginx_modules: ngx_http_echo_module ---- config eval -my $filters; - -foreach my $wid (0 .. $::workers - 1) { - $filters .= " - proxy_wasm hostcalls 'on_configure=define_metrics \ - on_tick=record_histograms \ - tick_period=100 \ - n_sync_calls=1 \ - on_worker=$wid \ - value=1 \ - metrics=h2';"; -} -qq{ - location /t { - $filters - - echo ok; - } -} ---- grep_error_log eval: qr/histogram "\d+":( \d+: \d+;)+/ ---- grep_error_log_out eval -qr/histogram "\d+": 1: $::workers; 4294967295: 0;/ ---- no_error_log -[error] -[crit] -[emerg] -[alert] -[stub] - - - -=== TEST 4: proxy_wasm metrics - record_metric(), histogram - on: request_headers, request_body, response_headers, response_body ---- skip_no_debug ---- 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_configure=define_metrics \ - on=$phases \ - test=/t/metrics/record_histograms \ - value=100 \ - metrics=h1'; - echo ok; - } -} ---- request -POST /t -hello ---- grep_error_log eval: qr/histogram "\d+":( \d+: \d+;)+/ ---- grep_error_log_out eval -qr/histogram "\d+": 128: 4; 4294967295: 0;/ ---- no_error_log -[error] -[crit] -[emerg] -[alert] -[stub] - - - -=== TEST 5: proxy_wasm metrics - record_metric(), histogram - on_http_call_response ---- skip_no_debug ---- wasm_modules: hostcalls ---- load_nginx_modules: ngx_http_echo_module ---- http_config eval ---- config eval -qq{ - listen unix:$ENV{TEST_NGINX_UNIX_SOCKET}; - - location /dispatched { - return 200 "Hello back"; - } - - location /t { - proxy_wasm hostcalls 'on_configure=define_metrics \ - test=/t/dispatch_http_call \ - host=unix:$ENV{TEST_NGINX_UNIX_SOCKET} \ - path=/dispatched \ - on_http_call_response=record_histograms \ - value=1000 \ - metrics=h2'; - echo ok; - } -} ---- grep_error_log eval: qr/histogram "\d+":( \d+: \d+;)+/ ---- grep_error_log_out eval -qr/histogram "\d+": 1024: 1; 4294967295: 0;/ ---- no_error_log -[error] -[crit] -[emerg] -[alert] -[stub] diff --git a/t/03-proxy_wasm/hfuncs/metrics/001-define_metric.t b/t/03-proxy_wasm/hfuncs/metrics/001-define_metric.t new file mode 100644 index 000000000..8fcad5bdd --- /dev/null +++ b/t/03-proxy_wasm/hfuncs/metrics/001-define_metric.t @@ -0,0 +1,64 @@ +# vim:set ft= ts=4 sts=4 sw=4 et fdm=marker: + +use strict; +use lib '.'; +use t::TestWasmX; + +our $workers = 2; + +workers($workers); +if ($workers > 1) { + master_on(); +} + +plan_tests(6); +run_tests(); + +__DATA__ + +=== TEST 1: proxy_wasm - define_metric() sanity +--- valgrind +--- wasm_modules: hostcalls +--- load_nginx_modules: ngx_http_echo_module +--- config + location /t { + proxy_wasm hostcalls 'on_configure=define_metrics \ + metrics=c1,g1,h1'; + echo ok; + } +--- grep_error_log eval: qr/\["hostcalls" \#\d\] defined metric \w+ as \d+ .*/ +--- grep_error_log_out eval +qr/.*? defined metric c1_Configure as \d+ .* +.*? defined metric g1_Configure as \d+ .* +.*? defined metric h1_Configure as \d+ .*/ +--- no_error_log +[error] +[crit] +[emerg] +[alert] + + + +=== TEST 2: proxy_wasm - define_metric() redefinition +Definition happens only once, a second call defining an existing metric simply +returns its id. + +--- skip_no_debug +--- valgrind +--- wasm_modules: hostcalls +--- load_nginx_modules: ngx_http_echo_module +--- config + location /t { + proxy_wasm hostcalls 'on_configure=define_metrics \ + metrics=c1,g1,h1'; + proxy_wasm hostcalls 'on_configure=define_metrics \ + metrics=c1,g1,h1'; + echo ok; + } +--- error_log +wasm returning existing metric +--- no_error_log +[error] +[crit] +[emerg] +[alert] diff --git a/t/03-proxy_wasm/hfuncs/metrics/001-define_metric_edge_cases.t b/t/03-proxy_wasm/hfuncs/metrics/002-define_metric_edge_cases.t similarity index 89% rename from t/03-proxy_wasm/hfuncs/metrics/001-define_metric_edge_cases.t rename to t/03-proxy_wasm/hfuncs/metrics/002-define_metric_edge_cases.t index 442da35cf..2f20b6455 100644 --- a/t/03-proxy_wasm/hfuncs/metrics/001-define_metric_edge_cases.t +++ b/t/03-proxy_wasm/hfuncs/metrics/002-define_metric_edge_cases.t @@ -7,12 +7,12 @@ use t::TestWasmX; skip_hup(); no_shuffle(); -plan_tests(6); +plan_tests(5); run_tests(); __DATA__ -=== TEST 1: proxy_wasm metrics - define_metric() metric name too long +=== TEST 1: proxy_wasm - define_metric() metric name too long In SIGHUP mode, this test fails if executed after a test that defined metrics, as any existing metric whose name exceeds `max_metric_name_length` won't be successfully reallocated causing the reconfiguration to fail. @@ -39,7 +39,6 @@ qq{ --- error_code: 500 --- error_log eval [ - qr/.+on_request_headers.+/, qr/host trap \(internal error\): metric name too long.*/, ] --- no_error_log @@ -49,7 +48,7 @@ qq{ -=== TEST 2: proxy_wasm metrics - define_metric() no memory +=== TEST 2: proxy_wasm - define_metric() no memory In SIGHUP mode, this test fails if executed after a test that defined more metrics than it's possible to fit in `5m`. @@ -78,7 +77,6 @@ qq{ --- error_log eval [ qr/\[crit\] .+ \[wasm\] "metrics" shm store: no memory; cannot allocate pair with key size \d+ and value size \d+/, - qr/.+on_request_headers.+/, qr/host trap \(internal error\): could not define metric.*/, ] --- no_error_log @@ -87,7 +85,7 @@ qq{ -=== TEST 3: proxy_wasm metrics - define_metric() no memory, histogram +=== TEST 3: proxy_wasm - define_metric() no memory, histogram In SIGHUP mode, this test fails if executed after a test that defined more metrics than it's possible to fit in `5m`. @@ -115,7 +113,6 @@ qq{ --- error_log eval [ "cannot allocate histogram", - qr/.+on_request_headers.+/, qr/host trap \(internal error\): could not define metric.*/, ] --- no_error_log diff --git a/t/03-proxy_wasm/hfuncs/metrics/101-increment_metric.t b/t/03-proxy_wasm/hfuncs/metrics/101-increment_metric.t new file mode 100644 index 000000000..6c651ff5b --- /dev/null +++ b/t/03-proxy_wasm/hfuncs/metrics/101-increment_metric.t @@ -0,0 +1,37 @@ +# vim:set ft= ts=4 sts=4 sw=4 et fdm=marker: + +use strict; +use lib '.'; +use t::TestWasmX; + +skip_hup(); + +our $workers = 2; + +workers($workers); +master_on(); + +plan_tests(6); +run_tests(); + +__DATA__ + +=== TEST 1: proxy_wasm - increment_metric() sanity +A counter's value should reflect increments made by all workers. + +--- valgrind +--- load_nginx_modules: ngx_http_echo_module +--- wasm_modules: hostcalls +--- config + location /t { + proxy_wasm hostcalls 'on_configure=define_and_increment_counters \ + metrics=c1'; + echo ok; + } +--- error_log eval +qr/c1_Configure: $::workers at Configure/ +--- no_error_log +[error] +[crit] +[emerg] +[alert] diff --git a/t/03-proxy_wasm/hfuncs/metrics/003-increment_metric_misuse.t b/t/03-proxy_wasm/hfuncs/metrics/102-increment_metric_misuse.t similarity index 90% rename from t/03-proxy_wasm/hfuncs/metrics/003-increment_metric_misuse.t rename to t/03-proxy_wasm/hfuncs/metrics/102-increment_metric_misuse.t index b977a73ad..683ba0d14 100644 --- a/t/03-proxy_wasm/hfuncs/metrics/003-increment_metric_misuse.t +++ b/t/03-proxy_wasm/hfuncs/metrics/102-increment_metric_misuse.t @@ -9,7 +9,7 @@ run_tests(); __DATA__ -=== TEST 1: proxy_wasm metrics - increment_metric() gauge +=== TEST 1: proxy_wasm - increment_metric() gauge --- valgrind --- load_nginx_modules: ngx_http_echo_module --- wasm_modules: hostcalls @@ -37,7 +37,7 @@ qq{ -=== TEST 2: proxy_wasm metrics - increment_metric() invalid metric id +=== TEST 2: proxy_wasm - increment_metric() invalid metric id --- valgrind --- load_nginx_modules: ngx_http_echo_module --- wasm_modules: hostcalls diff --git a/t/03-proxy_wasm/hfuncs/metrics/201-record_metric.t b/t/03-proxy_wasm/hfuncs/metrics/201-record_metric.t new file mode 100644 index 000000000..a91b643ad --- /dev/null +++ b/t/03-proxy_wasm/hfuncs/metrics/201-record_metric.t @@ -0,0 +1,91 @@ +# vim:set ft= ts=4 sts=4 sw=4 et fdm=marker: + +use strict; +use lib '.'; +use t::TestWasmX; + +skip_hup(); + +workers(2); +master_on(); + +plan_tests(7); +run_tests(); + +__DATA__ + +=== TEST 1: proxy_wasm - record_metric() gauge +A gauge's value is equal to the last value set by any of the workers. +The first filter, bound to worker 0, sets g1 to 1; the second one, bound to +worker 1, sets g1 to 2. + +--- wasm_modules: hostcalls +--- load_nginx_modules: ngx_http_echo_module +--- config + location /t { + proxy_wasm hostcalls 'on_configure=define_metrics \ + on_tick=set_gauges \ + tick_period=100 \ + n_sync_calls=1 \ + on_worker=0 \ + value=1 \ + metrics=g1'; + + proxy_wasm hostcalls 'on_configure=define_metrics \ + on_tick=set_gauges \ + tick_period=500 \ + n_sync_calls=1 \ + on_worker=1 \ + value=2 \ + metrics=g1'; + echo ok; + } +--- wait: 1 +--- grep_error_log eval: qr/\["hostcalls" \#\d\] record \d+ on g1_Configure/ +--- grep_error_log_out eval +qr/.*? record 1 on g1_Configure.* +.*? record 2 on g1_Configure.*/ +--- no_error_log +[error] +[crit] +[emerg] +[alert] +[stub] + + + +=== TEST 2: proxy_wasm - record_metric() histogram +Records values to a histogram so that each of its bins has counter equals to 1. + +--- skip_no_debug +--- valgrind +--- load_nginx_modules: ngx_http_echo_module +--- wasm_modules: hostcalls +--- config eval +my $filters; + +foreach my $exp (0 .. 17) { + my $v = 2 ** $exp; + $filters .= " + proxy_wasm hostcalls 'on_configure=define_metrics \ + test=/t/metrics/record_histograms \ + metrics=h1 \ + value=$v';"; +} +qq{ + location /t { + $filters + + echo ok; + } +} +--- error_log eval +[ + "growing histogram", + qr/histogram "\d+": 1: 1; 2: 1; 4: 1; 8: 1; 16: 1; 32: 1; 64: 1; 128: 1; 256: 1; 512: 1; 1024: 1; 2048: 1; 4096: 1; 8192: 1; 16384: 1; 32768: 1; 65536: 1; 4294967295: 1;/ +] +--- no_error_log +[error] +[crit] +[emerg] +[alert] diff --git a/t/03-proxy_wasm/hfuncs/metrics/004-record_metric_misuse.t b/t/03-proxy_wasm/hfuncs/metrics/202-record_metric_misuse.t similarity index 90% rename from t/03-proxy_wasm/hfuncs/metrics/004-record_metric_misuse.t rename to t/03-proxy_wasm/hfuncs/metrics/202-record_metric_misuse.t index 9a09895c7..989c36360 100644 --- a/t/03-proxy_wasm/hfuncs/metrics/004-record_metric_misuse.t +++ b/t/03-proxy_wasm/hfuncs/metrics/202-record_metric_misuse.t @@ -9,7 +9,7 @@ run_tests(); __DATA__ -=== TEST 1: proxy_wasm metrics - record_metric() counter +=== TEST 1: proxy_wasm - record_metric() counter --- valgrind --- load_nginx_modules: ngx_http_echo_module --- wasm_modules: hostcalls @@ -34,7 +34,7 @@ __DATA__ -=== TEST 2: proxy_wasm metrics - record_metric() invalid metric id +=== TEST 2: proxy_wasm - record_metric() invalid metric id --- valgrind --- load_nginx_modules: ngx_http_echo_module --- wasm_modules: hostcalls diff --git a/t/03-proxy_wasm/hfuncs/metrics/005-record_metric_edge_cases.t b/t/03-proxy_wasm/hfuncs/metrics/203-record_metric_edge_cases.t similarity index 82% rename from t/03-proxy_wasm/hfuncs/metrics/005-record_metric_edge_cases.t rename to t/03-proxy_wasm/hfuncs/metrics/203-record_metric_edge_cases.t index fb43a0aea..b7fc812c5 100644 --- a/t/03-proxy_wasm/hfuncs/metrics/005-record_metric_edge_cases.t +++ b/t/03-proxy_wasm/hfuncs/metrics/203-record_metric_edge_cases.t @@ -7,15 +7,21 @@ use t::TestWasmX; skip_hup(); no_shuffle(); -plan_tests(4); +plan_tests(7); run_tests(); __DATA__ -=== TEST 1: proxy_wasm metrics - record_metric() no memory, can't expand histogram +=== TEST 1: proxy_wasm - record_metric() cannot expand histogram In SIGHUP mode, this test fails if executed after a test that defined more metrics than it's possible to fit in `5m`. +This test creates 16256 histograms and records on them the values 1, 2, 4, 8 +and 16. Histograms cannot be expanded to include the bin associated with the +value 16, so the its occurrance is recorded in the bin whose upper bound is +4294967295. + +--- skip_no_debug --- valgrind --- load_nginx_modules: ngx_http_echo_module --- main_config eval @@ -67,8 +73,13 @@ qq{ metrics_name_len=115'; echo ok; } ---- error_log -cannot expand histogram +--- error_log eval +[ + "cannot expand histogram", + qr/histogram \"\d+\": 1: 1; 2: 1; 4: 1; 8: 1; 4294967295: 1/, +] --- no_error_log [emerg] [alert] +[error] +[crit] diff --git a/t/03-proxy_wasm/hfuncs/metrics/301-get_metric.t b/t/03-proxy_wasm/hfuncs/metrics/301-get_metric.t new file mode 100644 index 000000000..03300f261 --- /dev/null +++ b/t/03-proxy_wasm/hfuncs/metrics/301-get_metric.t @@ -0,0 +1,54 @@ +# vim:set ft= ts=4 sts=4 sw=4 et fdm=marker: + +use strict; +use lib '.'; +use t::TestWasmX; + +plan_tests(6); +run_tests(); + +__DATA__ + +=== TEST 1: proxy_wasm metrics - get_metric() counter +--- valgrind +--- load_nginx_modules: ngx_http_echo_module +--- wasm_modules: hostcalls +--- config + location /t { + proxy_wasm hostcalls 'on_configure=define_and_increment_counters \ + metrics=c1 \ + on=request_headers \ + test=/t/log/metrics'; + echo ok; + } +--- grep_error_log eval: qr/\["hostcalls" \#\d\] c1_Configure: 1.*/ +--- grep_error_log_out eval +qr/.*? c1_Configure: 1 .*/ +--- no_error_log +[crit] +[emerg] +[alert] +[stub] + + + +=== TEST 2: proxy_wasm metrics - get_metric() gauge +--- valgrind +--- load_nginx_modules: ngx_http_echo_module +--- wasm_modules: hostcalls +--- config + location /t { + proxy_wasm hostcalls 'on_configure=define_and_toggle_gauges \ + metrics=g1 \ + on=request_headers \ + test=/t/log/metrics'; + echo ok; + } +--- grep_error_log eval: qr/\["hostcalls" \#\d\] g1_Configure: 1.*/ +--- grep_error_log_out eval +qr/.*? g1_Configure: 1 .*/ +--- no_error_log +[crit] +[emerg] +[alert] +[stub] diff --git a/t/03-proxy_wasm/hfuncs/metrics/002-get_metric_misuse.t b/t/03-proxy_wasm/hfuncs/metrics/302-get_metric_misuse.t similarity index 100% rename from t/03-proxy_wasm/hfuncs/metrics/002-get_metric_misuse.t rename to t/03-proxy_wasm/hfuncs/metrics/302-get_metric_misuse.t diff --git a/t/lib/proxy-wasm-tests/context-checks/src/hostcalls.rs b/t/lib/proxy-wasm-tests/context-checks/src/hostcalls.rs index b83e7cd8e..66a939603 100644 --- a/t/lib/proxy-wasm-tests/context-checks/src/hostcalls.rs +++ b/t/lib/proxy-wasm-tests/context-checks/src/hostcalls.rs @@ -381,3 +381,77 @@ pub fn dispatch_http_call(_ctx: &TestContext) { info!("dispatch_http_call status: {}", status as u32); } } + +#[allow(improper_ctypes)] +extern "C" { + fn proxy_define_metric( + metric_type: MetricType, + name_data: *const u8, + name_size: usize, + return_id: *mut u32, + ) -> Status; +} + +pub fn define_metric(_ctx: &TestContext) { + let name = "a_counter"; + let metric_type = MetricType::Counter; + let mut return_id: u32 = 0; + + unsafe { + let status = proxy_define_metric(metric_type, name.as_ptr(), name.len(), &mut return_id); + + info!("define_metric status: {}", status as u32); + } +} + +#[allow(improper_ctypes)] +extern "C" { + fn proxy_get_metric(metric_id: u32, return_value: *mut u64) -> Status; +} + +pub fn get_metric(_ctx: &TestContext) { + let name = "a_counter"; + let metric_type = MetricType::Counter; + let mut metric_id: u32 = 0; + let mut value: u64 = 0; + + unsafe { + proxy_define_metric(metric_type, name.as_ptr(), name.len(), &mut metric_id); + let status = proxy_get_metric(metric_id, &mut value); + info!("get_metric status: {}", status as u32); + } +} + +#[allow(improper_ctypes)] +extern "C" { + fn proxy_record_metric(metric_id: u32, value: u64) -> Status; +} + +pub fn record_metric(_ctx: &TestContext) { + let name = "a_gauge"; + let metric_type = MetricType::Gauge; + let mut metric_id: u32 = 0; + + unsafe { + proxy_define_metric(metric_type, name.as_ptr(), name.len(), &mut metric_id); + let status = proxy_record_metric(metric_id, 1); + info!("record_metric status: {}", status as u32); + } +} + +#[allow(improper_ctypes)] +extern "C" { + fn proxy_increment_metric(metric_id: u32, offset: i64) -> Status; +} + +pub fn increment_metric(_ctx: &TestContext) { + let name = "a_counter"; + let metric_type = MetricType::Counter; + let mut metric_id: u32 = 0; + + unsafe { + proxy_define_metric(metric_type, name.as_ptr(), name.len(), &mut metric_id); + let status = proxy_increment_metric(metric_id, 1); + info!("increment_metric status: {}", status as u32); + } +} diff --git a/t/lib/proxy-wasm-tests/context-checks/src/lib.rs b/t/lib/proxy-wasm-tests/context-checks/src/lib.rs index 084b19ad9..eea6eae50 100644 --- a/t/lib/proxy-wasm-tests/context-checks/src/lib.rs +++ b/t/lib/proxy-wasm-tests/context-checks/src/lib.rs @@ -19,6 +19,10 @@ impl TestContext { let (name, arg) = name.split_once('|').unwrap_or_else(|| (name, "")); match name { "proxy_log" => log_something(self), + "define_metric" => define_metric(self), + "increment_metric" => increment_metric(self), + "record_metric" => record_metric(self), + "get_metric" => get_metric(self), "set_tick_period" => set_tick_period(self), "add_request_header" => add_request_header(self, arg), "add_response_header" => add_response_header(self, arg), diff --git a/t/lib/proxy-wasm-tests/hostcalls/src/filter.rs b/t/lib/proxy-wasm-tests/hostcalls/src/filter.rs index 0da33c0ba..bea8a9391 100644 --- a/t/lib/proxy-wasm-tests/hostcalls/src/filter.rs +++ b/t/lib/proxy-wasm-tests/hostcalls/src/filter.rs @@ -1,4 +1,4 @@ -use crate::{test_http::*, tests::*, types::*}; +use crate::{test_http::*, types::*}; use http::StatusCode; use log::*; use proxy_wasm::{traits::*, types::*}; @@ -102,10 +102,6 @@ impl Context for TestHttp { Some(format!("called {} times", self.n_sync_calls + 1).as_str()), ); } - "define_metrics" => test_define_metrics(self, TestPhase::HTTPCallResponse), - "increment_counters" => test_increment_counters(self, TestPhase::HTTPCallResponse, None), - "toggle_gauges" => test_toggle_gauges(self, TestPhase::HTTPCallResponse, None), - "record_histograms" => test_record_metric(self, TestPhase::HTTPCallResponse), _ => {} } diff --git a/t/lib/proxy-wasm-tests/hostcalls/src/lib.rs b/t/lib/proxy-wasm-tests/hostcalls/src/lib.rs index c394985db..9ab39b68b 100644 --- a/t/lib/proxy-wasm-tests/hostcalls/src/lib.rs +++ b/t/lib/proxy-wasm-tests/hostcalls/src/lib.rs @@ -127,17 +127,10 @@ impl RootContext for TestRoot { test_define_metrics(self, TestPhase::Tick); self.n_sync_calls += 1; } - "increment_counters" => test_increment_counters(self, TestPhase::Tick, None), - "toggle_gauges" => test_toggle_gauges(self, TestPhase::Tick, None), "set_gauges" => { test_record_metric(self, TestPhase::Tick); self.n_sync_calls += 1; } - "record_histograms" => { - test_record_metric(self, TestPhase::Tick); - self.n_sync_calls += 1; - } - "log_metrics" => test_log_metrics(self, TestPhase::Tick), "set_property" => test_set_property(self), "dispatch" => { if self.n_sync_calls >= n_sync_calls { diff --git a/t/lib/proxy-wasm-tests/hostcalls/src/types/test_http.rs b/t/lib/proxy-wasm-tests/hostcalls/src/types/test_http.rs index bd9ce7ae2..6ef74034f 100644 --- a/t/lib/proxy-wasm-tests/hostcalls/src/types/test_http.rs +++ b/t/lib/proxy-wasm-tests/hostcalls/src/types/test_http.rs @@ -134,7 +134,7 @@ impl TestHttp { "/t/metrics/increment_invalid_counter" => increment_metric(0, 1).unwrap(), "/t/metrics/set_invalid_gauge" => record_metric(0, 1).unwrap(), "/t/metrics/get_invalid_metric" => { - info!("[hostcalls] retrieving invalid metric in \"{:?}\"", cur_phase); + info!("retrieving invalid metric in \"{:?}\"", cur_phase); get_metric(0).unwrap(); }