code wiki / _hdl_build / nx_apistack_trace.nx

nx_apistack_trace.nx source

↩ module page · 35 lines · 1524 B

1// nx_apistack_trace.nx -- CAP-API-TRACE: distributed tracing for /api via W3C traceparent 2// "00-<32hex trace-id>-<16hex span-id>-01". Trace/span ids are DERIVED DETERMINISTICALLY from a seed (FNV-1a), so a 3// given request context reproduces byte-identical trace ids -- reproducible traces, the sovereign exceed over 4// random-id tracers. Pure integer, no float, no external collector. license_tier: ORIGINAL 5import "nx_syscalls.nx" 6const K_MAGIC_3750763034362895579: i64 = 3750763034362895579 7const K_MAGIC_1099511628211: i64 = 1099511628211 8 9func tr_fnv(x: i64) -> i64 { 10 var h: i64 = 0 - K_MAGIC_3750763034362895579 11 var b: i64 = 0 12 while b < 8 { h = (h ^ ((x >> (b * 8)) & 0xff)) * K_MAGIC_1099511628211; b = b + 1 } 13 return h 14} 15func tr_hex16(h: i64, out: *u8, off: i64) -> i64 { 16 let hx: *u8 = "0123456789abcdef" as *u8 17 var k: i64 = 0 18 while k < 16 { out[off + k] = hx[(h >> (60 - 4 * k)) & 0xf]; k = k + 1 } 19 return off + 16 20} 21func tr_cat(out: *u8, off: i64, s: *u8) -> i64 { var i: i64=0; while s[i]!=(0 as u8){out[off+i]=s[i];i=i+1} return off+i } 22 23// build a deterministic W3C traceparent from a seed. Returns length (55). 24func tr_traceparent(out: *u8, seed: i64) -> i64 { 25 let t1: i64 = tr_fnv(seed) 26 let t2: i64 = tr_fnv(t1) 27 let sp: i64 = tr_fnv(seed + 7) 28 var o: i64 = tr_cat(out, 0, "00-" as *u8) 29 o = tr_hex16(t1, out, o) 30 o = tr_hex16(t2, out, o) 31 o = tr_cat(out, o, "-" as *u8) 32 o = tr_hex16(sp, out, o) 33 o = tr_cat(out, o, "-01" as *u8) 34 return o 35}