code wiki / _hdl_build / nx_apistack_metrics.nx

nx_apistack_metrics.nx source

↩ module page · 26 lines · 1599 B

1// nx_apistack_metrics.nx -- CAP-API-METRICS: request/action counters for /api observability, emitted to the 2// deterministic status snapshot (Prometheus-style text). Pure INTEGER counters (no float, no external agent = the 3// sovereign exceed over StatsD/Prometheus scrapers). Counters live in a caller-held array (the daemon), indexed by a 4// stable metric id; mx_emit serializes "<name> <count>\n" for the snapshot / /api/metrics. license_tier: ORIGINAL 5import "nx_syscalls.nx" 6 7func mx_inc(counters: *i64, id: i64) -> i64 { counters[id] = counters[id] + 1; return counters[id] } 8func mx_add(counters: *i64, id: i64, delta: i64) -> i64 { counters[id] = counters[id] + delta; return counters[id] } 9func mx_get(counters: *i64, id: i64) -> i64 { return counters[id] } 10 11// serialize all counters as "<name> <count>\n". names[i] = pointer to a NUL-terminated metric name. Returns length. 12func mx_emit(counters: *i64, names: *i64, n: i64, out: *u8) -> i64 { 13 var o: i64 = 0; var i: i64 = 0 14 while i < n { 15 let nm: *u8 = names[i] as *u8 16 var k: i64 = 0; while nm[k] != (0 as u8) { out[o] = nm[k]; o = o + 1; k = k + 1 } 17 out[o] = 32 as u8; o = o + 1 18 var t: i64 = counters[i]; if t < 0 { out[o] = 45 as u8; o = o + 1; t = 0 - t } 19 let tm: *u8 = sys_mmap(24); var kk: i64 = 0; if t == 0 { tm[0] = 48 as u8; kk = 1 } 20 while t > 0 { tm[kk] = (48 + (t % 10)) as u8; t = t / 10; kk = kk + 1 } 21 var j: i64 = 0; while j < kk { out[o + j] = tm[kk - 1 - j]; j = j + 1 } o = o + kk 22 out[o] = 10 as u8; o = o + 1 23 i = i + 1 24 } 25 return o 26}