Skip to content

Commit

Permalink
[WIP] feat(proxy-wasm/metrics) histograms support
Browse files Browse the repository at this point in the history
  • Loading branch information
casimiro committed May 9, 2024
1 parent f90f362 commit 91e1862
Show file tree
Hide file tree
Showing 9 changed files with 776 additions and 87 deletions.
448 changes: 363 additions & 85 deletions src/common/ngx_wa_metrics.c

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/common/ngx_wa_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ typedef struct ngx_wa_metrics_s ngx_wa_metrics_t;
typedef enum {
NGX_WA_METRIC_COUNTER,
NGX_WA_METRIC_GAUGE,
NGX_WA_METRIC_HISTOGRAM,
} ngx_wa_metric_type_e;


typedef struct {
size_t slab_size;
size_t max_metric_name_length;
size_t slab_size;
size_t max_metric_name_length;
size_t max_histogram_bins;
} ngx_wa_metrics_conf_t;


Expand Down
4 changes: 4 additions & 0 deletions src/common/proxy_wasm/ngx_proxy_wasm_host.c
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,10 @@ ngx_proxy_wasm_hfuncs_define_metric(ngx_wavm_instance_t *instance,
type = NGX_WA_METRIC_GAUGE;
break;

case NGX_PROXY_WASM_METRIC_HISTOGRAM:
type = NGX_WA_METRIC_HISTOGRAM;
break;

default:
return ngx_proxy_wasm_result_trap(pwexec, "unknown metric type",
rets,
Expand Down
193 changes: 193 additions & 0 deletions t/03-proxy_wasm/hfuncs/metrics/400-record_histogram.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
# 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(6);
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
location /t {
proxy_wasm hostcalls 'on_configure=define_metrics \
test=/t/metrics/record_histograms \
metrics=h1 \
value=1';
proxy_wasm hostcalls 'on_configure=define_metrics \
test=/t/metrics/record_histograms \
metrics=h1 \
value=2';
proxy_wasm hostcalls 'on_configure=define_metrics \
test=/t/metrics/record_histograms \
metrics=h1 \
value=3';

proxy_wasm hostcalls 'on_configure=define_metrics \
test=/t/metrics/record_histograms \
metrics=h1 \
value=10';
proxy_wasm hostcalls 'on_configure=define_metrics \
test=/t/metrics/record_histograms \
metrics=h1 \
value=20';
proxy_wasm hostcalls 'on_configure=define_metrics \
test=/t/metrics/record_histograms \
metrics=h1 \
value=30';
echo ok;
}
--- grep_error_log eval: qr/histogram "\d+":( \d+: \d+;)+/
--- grep_error_log_out eval
qr/histogram "\d+": 1: 1; 2: 1; 4: 1; 16: 1; 32: 2; 4294967295: 0;/
--- 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: 3; 4294967295: 0;/
--- no_error_log
[error]
[crit]
[emerg]
[alert]



=== 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=$wid \
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: 2; 4294967295: 0;/
--- no_error_log
[error]
[crit]
[emerg]
[alert]



=== 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]



=== 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]
Loading

0 comments on commit 91e1862

Please sign in to comment.