From 7e3c5a4119d16768ecfb42208da9248726e49c9f Mon Sep 17 00:00:00 2001 From: Caio Ramos Casimiro Date: Mon, 3 Jun 2024 16:53:32 +0100 Subject: [PATCH] [TMP] review(*) duplicated metrics block --- src/common/metrics/ngx_wa_metrics.c | 2 +- src/common/metrics/ngx_wa_metrics.h | 5 ++-- src/wasm/ngx_wasm_directives.c | 11 ++++++-- t/01-wasm/directives/011-metrics_directives.t | 27 ++++++++++++++----- .../hfuncs/metrics/002-get_metric_misuse.t | 5 ++-- .../metrics/003-increment_metric_misuse.t | 4 +-- .../hfuncs/metrics/004-record_metric_misuse.t | 8 +++--- 7 files changed, 41 insertions(+), 21 deletions(-) diff --git a/src/common/metrics/ngx_wa_metrics.c b/src/common/metrics/ngx_wa_metrics.c index a65a223f5..3cc619a4d 100644 --- a/src/common/metrics/ngx_wa_metrics.c +++ b/src/common/metrics/ngx_wa_metrics.c @@ -199,7 +199,7 @@ ngx_wa_metrics_init_conf(ngx_wa_metrics_t *metrics, ngx_conf_t *cf) ngx_cycle_t *cycle = cf->cycle; ngx_core_conf_t *ccf = (ngx_core_conf_t *) ngx_get_conf(cycle->conf_ctx, ngx_core_module); - ngx_wa_metrics_t *old_metrics = metrics->old_metrics;; + ngx_wa_metrics_t *old_metrics = metrics->old_metrics; if (metrics->config.slab_size == NGX_CONF_UNSET_SIZE) { metrics->config.slab_size = NGX_WA_DEFAULT_METRICS_SLAB_SIZE; diff --git a/src/common/metrics/ngx_wa_metrics.h b/src/common/metrics/ngx_wa_metrics.h index d59ebe49b..f4cb9a0d6 100644 --- a/src/common/metrics/ngx_wa_metrics.h +++ b/src/common/metrics/ngx_wa_metrics.h @@ -9,8 +9,9 @@ typedef struct ngx_wa_metrics_s ngx_wa_metrics_t; typedef struct { - size_t slab_size; - size_t max_metric_name_length; + ngx_uint_t initialized; + size_t slab_size; + size_t max_metric_name_length; } ngx_wa_metrics_conf_t; diff --git a/src/wasm/ngx_wasm_directives.c b/src/wasm/ngx_wasm_directives.c index 0ff42751f..53af0e92a 100644 --- a/src/wasm/ngx_wasm_directives.c +++ b/src/wasm/ngx_wasm_directives.c @@ -52,12 +52,19 @@ ngx_wasm_core_v8_block(ngx_conf_t *cf, ngx_command_t *cmd, void *dummy) char * ngx_wasm_core_metrics_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) { - char *rv; - ngx_conf_t save = *cf; + char *rv; + ngx_conf_t save = *cf; + ngx_wa_conf_t *wacf = (ngx_wa_conf_t *) cf->ctx; cf->cmd_type = NGX_METRICS_CONF; cf->module_type = NGX_WASM_MODULE; + if (wacf->metrics->config.initialized) { + return "is duplicate"; + } + + wacf->metrics->config.initialized = 1; + rv = ngx_conf_parse(cf, NULL); *cf = save; diff --git a/t/01-wasm/directives/011-metrics_directives.t b/t/01-wasm/directives/011-metrics_directives.t index 466fe0ef2..bc0f2299b 100644 --- a/t/01-wasm/directives/011-metrics_directives.t +++ b/t/01-wasm/directives/011-metrics_directives.t @@ -23,7 +23,22 @@ __DATA__ -=== TEST 2: slab_size directive - sanity +=== TEST 2: metrics{} - duplicated block +--- main_config + wasm { + metrics {} + metrics {} + } +--- error_log: is duplicate +--- no_error_log +[error] +[crit] +[stub] +--- must_die + + + +=== TEST 3: slab_size directive - sanity --- main_config wasm { metrics { @@ -38,7 +53,7 @@ __DATA__ -=== TEST 3: slab_size directive - too small +=== TEST 4: slab_size directive - too small --- main_config wasm { metrics { @@ -55,7 +70,7 @@ qr/\[emerg\] .*? \[wasm\] shm size of \d+ bytes is too small, minimum required i -=== TEST 4: slab_size directive - invalid size +=== TEST 5: slab_size directive - invalid size --- main_config wasm { metrics { @@ -72,7 +87,7 @@ qr/\[emerg\] .*? \[wasm\] invalid shm size "1x"/ -=== TEST 5: slab_size directive - duplicate +=== TEST 6: slab_size directive - duplicate --- main_config wasm { metrics { @@ -89,7 +104,7 @@ qr/\[emerg\] .*? \[wasm\] invalid shm size "1x"/ -=== TEST 6: max_metric_name_length directive - sanity +=== TEST 7: max_metric_name_length directive - sanity --- main_config wasm { metrics { @@ -104,7 +119,7 @@ qr/\[emerg\] .*? \[wasm\] invalid shm size "1x"/ -=== TEST 7: max_metric_name_length directive - duplicate +=== TEST 8: max_metric_name_length directive - duplicate --- main_config wasm { metrics { diff --git a/t/03-proxy_wasm/hfuncs/metrics/002-get_metric_misuse.t b/t/03-proxy_wasm/hfuncs/metrics/002-get_metric_misuse.t index 652646e83..7a86c64d0 100644 --- a/t/03-proxy_wasm/hfuncs/metrics/002-get_metric_misuse.t +++ b/t/03-proxy_wasm/hfuncs/metrics/002-get_metric_misuse.t @@ -4,7 +4,7 @@ use strict; use lib '.'; use t::TestWasmX; -plan_tests(7); +plan_tests(6); run_tests(); __DATA__ @@ -22,8 +22,7 @@ __DATA__ --- error_code: 500 --- error_log eval [ - qr/.+on_request_headers.+/, - qr/metric \"0\" not found.*/, + qr/host trap \(internal error\): could not retrieve metric.*/, ] --- no_error_log [crit] diff --git a/t/03-proxy_wasm/hfuncs/metrics/003-increment_metric_misuse.t b/t/03-proxy_wasm/hfuncs/metrics/003-increment_metric_misuse.t index 56b641bb7..b977a73ad 100644 --- a/t/03-proxy_wasm/hfuncs/metrics/003-increment_metric_misuse.t +++ b/t/03-proxy_wasm/hfuncs/metrics/003-increment_metric_misuse.t @@ -26,7 +26,7 @@ qq{ --- error_code: 500 --- error_log eval [ - qr/\[error\] .+ \[wasm\] attempt to call increment_metric on a gauge; operation not supported/, + qr/\[warn\] .+ \[wasm\] attempt to call increment_metric on a gauge; operation not supported/, qr/.+on_request_headers.+/, qr/host trap \(internal error\): could not increment metric.*/, ] @@ -51,10 +51,10 @@ qq{ --- error_log eval [ qr/.+on_request_headers.+/, - qr/metric \"\d+\" not found.*/, qr/host trap \(internal error\): could not increment metric.*/, ] --- no_error_log [crit] [emerg] [alert] +[stub] diff --git a/t/03-proxy_wasm/hfuncs/metrics/004-record_metric_misuse.t b/t/03-proxy_wasm/hfuncs/metrics/004-record_metric_misuse.t index f1e6532d6..9a09895c7 100644 --- a/t/03-proxy_wasm/hfuncs/metrics/004-record_metric_misuse.t +++ b/t/03-proxy_wasm/hfuncs/metrics/004-record_metric_misuse.t @@ -4,7 +4,7 @@ use strict; use lib '.'; use t::TestWasmX; -plan_tests(7); +plan_tests(6); run_tests(); __DATA__ @@ -24,8 +24,7 @@ __DATA__ --- error_code: 500 --- error_log eval [ - qr/\[error\] .+ \[wasm\] attempt to call record_metric on a counter; operation not supported/, - qr/.+on_request_headers.+/, + qr/\[warn\] .+ \[wasm\] attempt to call record_metric on a counter; operation not supported/, qr/host trap \(internal error\): could not record metric.*/, ] --- no_error_log @@ -48,8 +47,7 @@ __DATA__ --- error_code: 500 --- error_log eval [ - qr/.+on_request_headers.+/, - qr/metric \"0\" not found.*/, + qr/host trap \(internal error\): could not record metric.*/, ] --- no_error_log [crit]