Skip to content

Commit

Permalink
hotfix(ffi) correct usage of 'n_bins' when parsing a histogram
Browse files Browse the repository at this point in the history
Previously, when parsing a histogram without uninitialized bins,
`parse_cmetric` would incorrectly build an extra bin from
invalid memory.

Histograms with uninitialized bins were correctly parsed due to
`parse_cmetric` stop parsing bins when an uninitialized one is found.
  • Loading branch information
casimiro authored Oct 9, 2024
1 parent 6142197 commit c46ff1c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/resty/wasmx/shm.lua
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ local function parse_cmetric(cmetric)
local ch = ffi_cast("ngx_wa_metrics_histogram_t *", hbuf)
local h = { type = "histogram", value = {}, sum = tonumber(ch.sum) }

for i = 0, ch.n_bins do
for i = 0, (ch.n_bins - 1) do
local cb = ch.bins[i]
if cb.upper_bound == 0 then
break
Expand Down
23 changes: 16 additions & 7 deletions t/04-openresty/ffi/shm/024-metrics_get_by_name.t
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,19 @@ h1: {sum=100,type="histogram",value={{count=1,ub=128},{count=0,ub=4294967295}}}
=== TEST 2: shm_metrics - get_by_name() sanity non-FFI-defined metrics
prefix: pw.hostcalls.*
--- wasm_modules: hostcalls
--- config
--- config eval
my $record_histograms;

foreach my $exp (0 .. 17) {
my $v = 2 ** $exp;
$record_histograms .= "
proxy_wasm hostcalls 'on_configure=define_metrics \
test=/t/metrics/record_histograms \
metrics=h1 \
value=$v';";
}

qq{
location /t {
proxy_wasm hostcalls 'on_configure=define_metrics \
on=request_headers \
Expand All @@ -60,11 +72,7 @@ prefix: pw.hostcalls.*
test=/t/metrics/toggle_gauges \
metrics=g1';

proxy_wasm hostcalls 'on_configure=define_metrics \
on=request_headers \
test=/t/metrics/record_histograms \
metrics=h1 \
value=100';
$record_histograms

content_by_lua_block {
local shm = require "resty.wasmx.shm"
Expand All @@ -75,10 +83,11 @@ prefix: pw.hostcalls.*
ngx.say("h1: ", pretty.write(shm.metrics:get_by_name("pw.hostcalls.h1", { prefix = false }), ""))
}
}
}
--- response_body
c1: {type="counter",value=13}
g1: {type="gauge",value=1}
h1: {sum=100,type="histogram",value={{count=1,ub=128},{count=0,ub=4294967295}}}
h1: {sum=262143,type="histogram",value={{count=1,ub=1},{count=1,ub=2},{count=1,ub=4},{count=1,ub=8},{count=1,ub=16},{count=1,ub=32},{count=1,ub=64},{count=1,ub=128},{count=1,ub=256},{count=1,ub=512},{count=1,ub=1024},{count=1,ub=2048},{count=1,ub=4096},{count=1,ub=8192},{count=1,ub=16384},{count=1,ub=32768},{count=1,ub=65536},{count=1,ub=4294967295}}}
--- no_error_log
[error]
[crit]
Expand Down

0 comments on commit c46ff1c

Please sign in to comment.